. . .

What is the use of if-else statement in the C language? give a suitable example!

The if statement by itself will execute a single statement or a
group of statements, when the expression following it 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.

A few points

  • 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.

Nested if-else

It is perfectly all right if we write an entire if-else construct within
either the body of the if statement or the body of an else statement.
This is called ‘nesting’ of ifs. This is shown in the following program.


void main(){

	int sub1,sub2,sub3,sub4,sub5,sum,avg;

	printf("enter the marks of five subjects\n");




		printf("good marks\n");

		printf("medium marks\n");

		printf("low marks\n");



Please share the article

Leave a Reply

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

18 + two =