In the C language decision making statement is executes if the given condition is true otherwise conditional block will never execute.
In C language non-zero and non-null values are consider as true, and zero or null values are consider false.
if statement
Syntax of an if statement −
1 2 3 4 | if(expression) { statements; } |
In a if statement if the expression result is non zero true, then the block of statement of the ‘if’ statement will be executed. If the expression result is zero, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.
Example: Write a program to demonstrate the ‘if’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main() { int x = 10; int y = 25; if (x < y) // condition x<y is true it returns 1 therefore if block will execute { printf("Sum of x+y = %i", (x + y)); } printf("\n after if statement %d",z); return 0; } |
OUTPUT
1 2 | Sum of x+y = 35 after if statement |
In the above program if(x>y) then the condition will be false and ‘if’ block will never execute.
1 | #include < stdio.h ><br> int main() {<br> int x = 10;<br> int y = 25;<br> if (x > y) // condition x>y is false therefore if block will not execute <br> {<br> printf("Sum of x+y = %i", (x + y));<br> }<br> printf("\n after if statement");<br> return 0;<br> }<br> |
OUTPUT
1 | after if statement |
if-else statement
Syntax of an ‘if-else’ statement −
1 2 3 4 5 6 7 8 | if(expression) { statements } else { statements } |
1 | In a ‘if-else’ statement if the expression is <strong>non zero, </strong>then the block of statements of the 'if' statement will be executed. If the statement expression is <strong>zero,</strong> then the block of code of the 'else' statement will executed.<br><br> |
if else c programming Examples
Example: Write a program to take two numbers from user and find the grater between them using ‘if-else’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> int main() { int x; int y; printf("enter 1st number"); scanf("%d", & x); // 10 printf("enter 2 nd number"); scanf("%d", & y); // 30 if (x > y) // check condition x>y { printf("grater value= %d", x); } else { printf("\n grater value= %d", y); } return 0; } |
OUTPUT
1 2 3 | enter 1st number 34 enter 2 nd number 2 grater value= 34 |
Nested If else
In a C language, we can use if statement inside another if statement(s).
Syntax of an ‘nested -if’ statement −
1 2 3 4 5 6 7 8 | if(expression1) { statements If (expression2) { statements } } |
Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # include < stdio.h > int main() { int x; int y; printf("enter 1st number"); scanf("%d", & x); // 5 printf("enter 2nd number"); scanf("%d", & y); // 7 if (x < 10) // check condition x<10 { if (y < 10) // nested-if, check condition y<10 { printf("Good"); } } return 0; } |
OUTPUT
1 2 3 | enter 1st number3 enter 2nd number2 Good |
Nested if else statement
In a C language we can use if or if-else statement inside another if or if-else statement(s).
Syntax of an ‘if-else’ statement −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if(expression1) { statements if (expression2) { statements } } else { if (expression3) { statements } } |
Example: Write a program to take three numbers from user and find the grater among them using ‘nested if-else’ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #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("The largest number= %d", n1); } else { printf("The largest number = %d ", n3); } } else { if (n2 >= n3) { printf("The largest number= %d", n2); } else { printf("The largest number=%d", n3); } } return 0; } |
OUTPUT
1 2 3 4 5 | Enter three numbers: 20 10 30 The largest number= 30 |
If else ladder
some times if can be nested inside else as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if (expression1) { statements; } else { if (expression2) { statements; } else { if (expression3) { statements; } } } } |
above statements can be written as
1 2 3 4 5 6 7 8 9 10 11 12 | if (expression1) { statements; } else if (expression2) { statements; } else if (expression3) { statements; } else { statements; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> int main() { int per; printf("Enter your percentage of marks: "); scanf("%d", &per); if (per>90) { printf("Your Gread is O"); } else if (per>80) { printf("Your Gread is E"); } else if (per>70) { printf("Your Gread is A"); } else if (per>60) { printf("Your Gread is B"); } else if (per>50) { printf("Your Gread is C"); } else if (per>40) { printf("Your Gread is D"); } else if (per>30) { printf("Your Gread is F"); } return 0; } |
OUTPUT
1 2 | Enter your percentage of marks:91 Your Gread is O |