if else statement in c

  • The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true.
  • It does nothing when the expression evaluates to false.

Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement.

Let’s have an example:

Problem Statement:

In a company an employee is paid as under:

If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee’s salary is input through the keyboard write a program to find his gross salary.


/* Calculation of gross salary */
main( )
float bs, gs, da, hra ;
printf ( “Enter basic salary ” ) ;
scanf ( “%f”, &bs ) ;
if ( bs < 1500 )
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
hra = 500 ;
da = bs * 98 / 100 ;
gs = bs + hra + da ;
printf ( “gross salary = Rs. %f”, gs ) ;

Flow Diagram :

if else statement in c
if else statement in c

Point to be Noted

  • The group of statements after the if up to and not including the else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’.
  • Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right. This formatting convention is followed throughout the book to enable you to understand the working of the program better.
  • Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
  • As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above example must be used.

Related Posts

  • 75
    Problem Statement: Write a program to calculate the salary as per the following table: Solution: /* C program to calculate salary */ main( ) { char g ; int yos, qual, sal ; printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" )…
    Tags: salary, program, example, write, learn, tutorial, programming, basic
  • 72
    Write a C Program for the following :- Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary. Answer:  /* To Calculate the gross salary of Ramesh…
    Tags: salary, basic, gross, program, write, hra, da, tutorial, learn, example
  • 67
    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
    Tags: statement, program, example, learn, programming, write, tutorial
  • 64
    In this article we will learn about the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates…
    Tags: expression, example, evaluates, program, learn, programming, write, tutorial
  • 62
    Problem Statement: If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. Solution: /* Sum of 1st and last digit of a four digit number */ #include<stdio.h> #include<conio.h> main() { int n,a,sum=0; clrscr(); printf("\Enter a…
    Tags: program, write, learn, example, tutorial, programming, basic

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: