IF Else in C:
If-else in c:
If else is the most used condition in our life. Whenever we need to make any decision we use this condition in our daily life. if-else in c is used when we are required to execute a specific block of code and that block is executed when it met the condition given to it. We call this process Decision making.
In this article, we will discuss how if-else statement are used in programming and also look at some examples of it.
Types of if-else statement:
If else statements are of following types:
1. If statement
2. If-else statement
3. Else-if statement
4. Nested if-else statement.
If statement:
First let’s take a look at the syntax of the if statement:
Syntax:
If (condition is given)
{
// Any statement to execute in its body
}
// Any Statement outside its body.
Here we can see that if the condition given at the entrance is turned true then the body will execute otherwise it will execute the statement outside of the body.
If-else statement:
In the if-else statement, there are two conditions one at the entrance of if is given if it turns to false then the code inside its body is not executed then the code inside the body of else is executed.
Syntax:
If (condition is given)
{
//statement in the body of if
}
Else
{
//Any statement inside the body of else
}
Example of if-else in c:
Let’s look at a simple flow diagram example of if-else statement.
Example of Else if in c:
Let’s look at another example of if else statement.
A simple C program to display the greater number using the if else statement.
Source code:
#include<stdio.h>
Int main ()
{
Int x=15;
If(x>20)
{
Printf(“ x is greater than 20”);
Else
{
Printf(“ x is not greater than 20”);
}
return 0;
}
Output:
X is not greater than 20.
Else-if Statement:
Let’s look at the syntax of the else-if statement.
Syntax:
If (condition 1 given)
{
First Statement;
}
Else if( condition 2 given)
{
Second statement;
}
Else if ( condition 3 given)
{
Third Statement;
}
Else
default statement;
Example of else if in c:
Let’s look at a simple example of else if statement.
A simple program to check whether the number is divisible by 4 and 5 or not.
Source Code:
#include<stdio.h>
Int main()
{
Int x;
Printf(“enter the of x”);
Scanf(“%d”, &x);
If(x%4==0 && x%5==0)
{
Printf(“x is divisible by both 4 and 5”);
}
Else if(x%4==0)
{
Printf(“x is divisible by 4”)
}
Else if(x%5==0)
{
Printf(“x is divisible by 5”)
}
Else
{
Printf(“x is not divided by both 4 and 5”);
}
}
Nested If-else in c:
Nested if-else means when another if or else statement is used in the body of the if-else statement. Then that if-else is said to be nested if-else in c.
Let’s take a look at the syntax of nested if-else in c:
Syntax:
If (condition is given)
{
If (any expression)
{
Any statement no 1;
}
Else
{
Statement number 2;
}
}
Else
{
Statement number 3;
}
Example of Nested if-else in c:
Let’s take a look at a simple example of nested if-else statement in which we will check which number s greater among the three numbers.
Source Code:
#include<stdio.h>
Int main()
{
Int x,y,z;
Printf(“Enter any three values”);
Scanf(“%d%d%d, &x,&y,&z);
If(x>y)
{
If(x>z)
{
Printf(“ x is the greatest among all”)
Else
{
Printf(“ z is the greatest among all”);
}
Else
{
If (y>z)
{
Printf(“y is greater among all”);
Else
{
Printf(“z is greater among all”);
}
}
The basic concept in if-else statement is that in case if the condition given at the entrance of the body of IF turns to be wrong then its body will not executed and the statement that we have written in the body of else will be executed.
Thanks for reading the article

Nice
ReplyDelete