Simple If Statement In C
Syntax
if (condition ) { True Statement; } Next Statement; Simple If statement first check the condition. If condition is true then true statment
block is executed and then Next Statement after the simple if ststement is executed.
If condition is false then true statement block is not executed and directly next
statement after simple if statment is executed. Example
int mark = 10; if (mark < 35) { printf("Fail"); } |
If Else Statement In C
Syntax
if (condition ) { True Statement; } else { False Statement; } Next Statement; If...else statement first check the condition. If condition is true then
true statment block is executed and then Next Statement after the if...else ststement
is executed. Example:
int mark=20; if (mark < 35) { printf("Fail"); } else { printf("Pass"); } |
Nested If Statement in C
Syntax
if (OuterCondition) { if (InnerCondition) { True Statement Block for InnerCondition } else { Fasle Statement Block for InnerCondition } } else { False Statement Block for OuterCondition } Next Statement; When
one if statement is contained within another if statement then it is known as Nested
If statement. Example
int temp; temp=38; if(temp>20) { if(temp>40) printf("Very Hot....\n"); else printf("Hot...\n"); } else { printf("Cool...."); } |
If ElseIf Ladder In C
Syntax
if (Condition1) { True Statement Block1; } else if (Condition2) { True Statement Block2; } ......................... ......................... else if (Condition N) { True Statement BlockN; } else { Default Statement Block; } Next Statement; If...else
if ladder statement is used when multiple conditions has to be checked in the program. Example
float per=45; if (per >= 66) { printf("Distinction\n"); } else if (per <66 && per >=56) { printf("First Class\n"); } else if (per <56 && per >= 44) { printf("Second Class\n"); } else if (per <44 && per >= 35) { printf("Pass Class\n"); } else { printf("Sorry Fail"); } |