If Statements in C
Decision Making/Control Statements In C
Introduction
Sometimes, while writing code, programmer has to take decision like, which statements or block of code to be executed and which statements or block of code to be skipped during execution. Such kind of decisions programmer can take using Decision Making Statements in C.
for example
- if user age is above or equal to 18, user will be eligible for voting.
- if account balance is below minimum, user cannot withdraw money.
- user having multiple options and one of option has to be executed.
- and so on....
Decision Making Statements in C can be divided into following types
1. if statement
2. switch case statement
3. Conditional/Ternary Operator
4. goto Statement.
In this Article we will see if Statements in c
if Statement in C
if statements are divided into following types
1. Simple if
2. if else statement
3. Nested if else statement
4. if else ladder
1. Simple if Statement :
Simple if General Syntax :
------------------------------------
if(condition)
{
// Block of Statements to be executed
// if condition becomes true;
}
------------------------------------
Working Flow of Simple if Statement :
1. First condition gets execute.
2. If Condition is True / If Condition returns Non Zero, Control enters if block and executes, block of code.
3. If Condition is False / If Condition returns Zero, Control skips execution of if block and continue executing immediate statements following if block.
2. If Condition is True / If Condition returns Non Zero, Control enters if block and executes, block of code.
3. If Condition is False / If Condition returns Zero, Control skips execution of if block and continue executing immediate statements following if block.
Simple if Flow Chart :
Example of Simple if Statement :
#include<stdio.h>
int main()
{
int age=17;
if(age>=18) // Condition is False
{
printf("YOU ARE ELIGIBLE FOR VOTING");
}
printf("SIMPLE IF STATEMENT IN C");
return 0;
}
In above Example you can see that, When Condition is True, if block is getting execute and when condition is False, if block is not getting execute.
2. if else Statement :
if else General Syntax :
------------------------------------
if(condition)
{
Statements-x;
// Block of Statements to be executed if condition becomes true;
}
else
{
Statements-y;
// Block of Statements to be executed if condition becomes false;
}
------------------------------------
Working Flow of if else Statement :
1. First condition gets execute.
2. If Condition is True / If Condition returns Non Zero, Control enters if block and executes Statements-x;
3. If Condition is False / If Condition returns Zero, Control enters else block and executes Statements-y;
2. If Condition is True / If Condition returns Non Zero, Control enters if block and executes Statements-x;
3. If Condition is False / If Condition returns Zero, Control enters else block and executes Statements-y;
Either of the block "if" or "else" will definitely get execute, where flow of control is depends on condition getting execute.
if else statement Flow Chart :
Example of if else Statement :
In if else statement we can use else block, where we can decide which statements to be executed when condition becomes false, this we cant do in simple if statement. So compare to Simple if statement if else statement gives us alternative block to be executed when condition become false.
Let's Convert above example using if else statement in C, here we can display "You are not eligible for voting" if condition is false.
------------------------------------
#include<stdio.h>
int main()
{
int age=20;
if(age>=18) // Condition is True
{
printf(" ELIGIBLE FOR VOTING ");
}
else
{
printf(" NOT ELIGIBLE FOR VOTING ");
}
printf(" SIMPLE IF STATEMENT IN C ");
return 0;
}
------------------------------------
Let's take one more example where we can check whether entered number is even or odd.
#include<stdio.h>
int main()
{
int num;
printf("ENTER NUMBER : ");
scanf("%d",&num);
if(num%2==0)
{
printf(" %d IS EVEN",num);
}
else
{
printf(" %d IS ODD",num);
}
return 0;
}
3. Nested if else Statement :
Programmer cannot always decide flow of program on only one condition, there may occur a situation when programmer has to check few more conditions after checking primary condition. In such case Nested if else statement can be used.
Nested if else Statement General Syntax :
------------------------------------
if(condition 1)
{
if(condition 2)
{
Statement x1;
}
else
{
Statement x2;
}
}
else
{
Statement y;
}
------------------------------------
In Nested if else statement we may have "if else" statement within "if else" statement as shown in above syntax. Actually we can't predict syntax of Nested if else statement as it may take many form like "if block " may contain "simple if statement" or "else block" may contain "if else statement" within it, and so on....
Working Flow of Nested if else Statement (Explanation for Syntax given above) :
1. Condition 1 will get execute first.
2. If Condition 1 becomes True, Control will enter if block where it will encounter condition 2
2. If Condition 1 becomes True, Control will enter if block where it will encounter condition 2
a. If condition 2 becomes true, Statement x1 will get execute.
b. If condition 2 becomes false, Statement x2 will get execute.
3. If Condition 1 becomes False, Control will enter else block and will execute Statement y.
Nested if else statement Flow Chart :
------------------------------------
#include<stdio.h>
int main()
{
int n1,n2;
printf("ENTER TWO NUMBERS : ");
scanf("%d%d",&n1,&n2);
if(n1!=n2)
{
if(n1>n2)
{
printf("%d IS LARGEST",n1);
}
else
{
printf("%d IS LARGEST",n2);
}
}
else
{
printf("BOTH NUMBERS ARE EQUAL");
}
return 0;
}
------------------------------------#include<stdio.h>
int main()
{
int n1,n2,n3;
printf("ENTER THREE NUMBERS : ");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d IS LARGEST",n1);
}
else
{
printf("%d IS LARGEST",n3);
}
}
else
{
if(n2>n3)
{
printf(" %d IS LARGEST",n2);
}
else
{
printf("%d IS LARGEST",n3);
}
}
return 0;
}
------------------------------------4. if else Ladder
When user has "n" number of options and one of the option need to opted for execution, if else ladder can be used in such situation.
In if else ladder we can check sequence of conditions following each other as shown in following syntax.
if else ladder General Syntax :
------------------------------------
if(condition 1)
{
Statements 1;
}
else if(condition 2)
{
Statements 2;
}
else if(condition 3)
{
Statements 3;
}
.
.
.
else if(condition n)
{
Statements n;
}
else
{
Default Statements;
// Block to be executed if all n conditions becomes false.
}
------------------------------------
Working Flow of if else ladder :
1. Condition 1 will get execute first.
2. if Condition 1 becomes true, it will execute Statements 1 and skip execution of all other conditions including default else block.3. if condition 1 becomes false, it continues executing condition 2.4. This process gets repeated till n conditions following each other.5. Whichever condition becomes true, block associated with that condition gets execute and skip execution of all other conditions including default else block.6. When all "n" conditions becomes false, default else block gets execute.
1. Condition 1 will get execute first.
2. if Condition 1 becomes true, it will execute Statements 1 and skip execution of all other conditions including default else block.
3. if condition 1 becomes false, it continues executing condition 2.
4. This process gets repeated till n conditions following each other.
5. Whichever condition becomes true, block associated with that condition gets execute and skip execution of all other conditions including default else block.
6. When all "n" conditions becomes false, default else block gets execute.
if else ladder Flow Chart :
Examples of if else Statement :
------------------------------------
Click here to view related articles :
.
.
.
Thanks and Regards
Prof. Shivling G. Swami.
Head of Computer Science and Engineering,
Sant Gajanan Maharaj College of Engineering, Mahagaon.
Whats app and Contact Number : 99 22 970 696
Click here to view my You Tube Profile
Nice sir as usual understandable.....👌👍👍
ReplyDeleteSir KDK🔥
ReplyDelete👏👍
ReplyDeleteNice sir 👍
ReplyDeleteExplanation in easiest way👍
ReplyDelete