Prev Next
C Decision Control statement:
- In decision control statements (if-else and nested if), group of statements are executed when condition is true. If condition is false, then else part statements are executed.
- There are 3 types of decision making control statements in C language. They are,
- if statements
- if else statements
- nested if statements
“If”, “else” and “nested if” decision control statements in C:
Syntax for each C decision control statements are given in below table with description.
Decision control statements |
Syntax/Description
|
if | Syntax: if (condition) { Statements; }Description: In these type of statements, if condition is true, then respective block of code is executed. |
if…else | Syntax: if (condition) { Statement1; Statement2; } else { Statement3; Statement4; }Description: In these type of statements, group of statements are executed when condition is true. If condition is false, then else part statements are executed. |
nested if | Syntax: if (condition1){ Statement1; } else_if(condition2) { Statement2; } else Statement 3;Description: If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed. |
Example program for if statement in C:
In “if” control statement, respective block of code is executed when condition is true.
1 2 3 4 5 6 7 8 |
int main() { int m=40,n=40; if (m == n) { printf("m and n are equal"); } } |
Output:
m and n are equal
|
Example program for if else statement in C:
In C if else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); } } |
Output:
m and n are not equal
|
Example program for nested if statement in C:
- In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true.
- If condition 2 also gets failure, then else part is executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int m=40,n=20; if (m>n) { printf("m is greater than n"); } else if(m<n) { printf("m is less than n"); } else { printf("m is equal to n"); } } |
Output:
m is greater than n
|