C

If…Else, Nested If…Else…And Else… Example Of A Statement

We learned how to use the if statement in C in the previous tutorial. We will learn how to use if else, nested if else, and else if statements in a C program in this guide.

C If else clause

If else statement syntax:
If the condition is true, the statements in the body of “if” are executed, while the statements in the body of “else” are skipped.
If the condition returns false, the statements in the body of the “if” are skipped and the statements in the “else” are executed.

if(condition) {
   // Statements inside body of if
}
else {
   //Statements inside body of else
}

If else statement illustration

The user is asked to enter their age in this program, and the if…else statement checks whether the entered age is greater than or equal to 18. If this condition is met, the message “You are eligible to vote” is displayed; otherwise, the message “You are not eligible to vote” is displayed.

#include <stdio.h>
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age >=18)
   {
	/* This statement will only execute if the
	 * above condition (age>=18) returns true
	 */
	printf("You are eligible for voting");
   }
   else
   {
	/* This statement will only execute if the
	 * condition specified in the "if" returns false.
	 */
	printf("You are not eligible for voting");
   }
   return 0;
}

Output:

Enter your age:14
You are not eligible for voting

Note: If the “if” or “else” body contains only one statement, you do not need to use braces (parenthesis). For example, the preceding program can be rewritten as follows:

#include <stdio.h>
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age >=18)
	printf("You are eligible for voting");
   else
	printf("You are not eligible for voting");
   return 0;
}

If…else statement in C

When an if else statement appears within the body of another “if” or “else” statement, this is referred to as nested if else.
Nested if else statement syntax:

if(condition) {
    //Nested if else inside the body of "if"
    if(condition2) {
       //Statements inside the body of nested "if"
    }
    else {
       //Statements inside the body of nested "else"
    }
}
else {
    //Statements inside the body of "else"
}

Example of nested if..else

#include <stdio.h>
int main()
{
   int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 != var2)
   {
	printf("var1 is not equal to var2\n");
	//Nested if else
	if (var1 > var2)
	{
		printf("var1 is greater than var2\n");
	}
	else
	{
		printf("var2 is greater than var1\n");
	}
   }
   else
   {
	printf("var1 is equal to var2\n");
   }
   return 0;
}

Output:

Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1

C – if…else statement

The else..if statement is useful when you need to check multiple conditions within a program; it avoids nesting of if-else blocks.
Otherwise..if statement syntax:

if (condition1) 
{
   //These statements would execute if the condition1 is true
}
else if(condition2) 
{
   //These statements would execute if the condition2 is true
}
else if (condition3) 
{
   //These statements would execute if the condition3 is true
}
.
.
else 
{
   //These statements would execute if all the conditions return false.
}

Another example of an else..if statement

Let’s use the same example as before when discussing nested if…else. The same program will be rewritten using else…if statements.

#include <stdio.h>
int main()
{
   int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 !=var2)
   {
	printf("var1 is not equal to var2\n");
   }
   else if (var1 > var2)
   {
	printf("var1 is greater than var2\n");
   }
   else if (var2 > var1)
   {
	printf("var2 is greater than var1\n");
   }
   else
   {
	printf("var1 is equal to var2\n");
   }
   return 0;
}

Output:

Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2

As you can see, only the statements contained within the body of “if” are executed. This is because, in this statement, when a condition is met, the statements within that block are executed while the rest of the blocks are ignored.
Important Notes:
1. else and else..if are optional statements; a program with only the “if” statement would function normally.
2. else and else..if cannot be used without the conjunction “if.”
3. A if else..if block can contain any number of else..if statements.
4. The statements in the else block are executed if none of the conditions are met.
5. Logical operators such as AND (&&), OR (||), and NOT (!) can be used in the same way that relational operators can.

Related Articles

Leave a Reply

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

Back to top button