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 −
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.
#includeint main() { int x = 10; int y = 25; if (x < y) // condition x OUTPUT
Sum of x+y = 35 after if statementIn the above program if(x>y) then the condition will be false and ‘if’ block will never execute.
#include < stdio.h >
int main() {
int x = 10;
int y = 25;
if (x > y) // condition x>y is false therefore if block will not execute
{
printf("Sum of x+y = %i", (x + y));
}
printf("\n after if statement");
return 0;
}OUTPUT
after if statementif-else statement
Syntax of an 'if-else' statement −
if(expression) { statements } else { statements }In a ‘if-else’ statement if the expression is non zero, then the block of statements of the 'if' statement will be executed. If the statement expression is zero, then the block of code of the 'else' statement will executed.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.
#includeint 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
enter 1st number 34 enter 2 nd number 2 grater value= 34Nested If else
In a C language, we can use if statement inside another if statement(s).
Syntax of an 'nested -if' statement −
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.
# 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
enter 1st number3 enter 2nd number2 GoodNested 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 −
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.
#includeint 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
Enter three numbers: 20 10 30 The largest number= 30If else ladder
some times if can be nested inside else as below
if (expression1) { statements; } else { if (expression2) { statements; } else { if (expression3) { statements; } } } }above statements can be written as
if (expression1) { statements; } else if (expression2) { statements; } else if (expression3) { statements; } else { statements; }#includeint 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
Enter your percentage of marks:91 Your Gread is O