0
The if, if...else and nested if...else statement are used to make one-time decisions in C Programming, that is, to execute some code/s and ignore some code/s depending upon the test expression.

C if Statement

if (test expression) {
statement/s to be executed if test expression is true;
}
The if statement checks whether the text expression inside parenthesis ( ) is true or not. If the test expression is true, statement/s inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored.

Flowchart of if statement

 
Branching in C programming language using if statement

Example 1: C if statement

Write a C program to print the number entered by user only if the number entered is negative.

#include <stdio.h>
int main(){
int num;
printf
("Enter a number to check.\n");
scanf
("%d",&num);
if(num<0) { /* checking whether number is less than 0 or not. */
printf
("Number = %d\n",num);
}
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
printf
("The if statement in C programming is easy.");
return 0;
}
Output 1
Enter a number to check.
-2
Number = -2
The if statement in C programming is easy.
When user enters -2 then, the test expression (num<0) becomes true. Hence, Number = -2 is displayed in the screen.
Output 2
Enter a number to check.
5
The if statement in C programming is easy.
When the user enters 5 then, the test expression (num<0) becomes false. So, the statement/s inside body of if is skipped and only the statement below it is executed.

C if...else statement

The if...else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false.

Syntax of if...else

if (test expression) {
statements to be executed if test expression is true;
}
else {
statements to be executed if test expression is false;
}

Flowchart of if...else statement

Flowchart of if...else statement in C Programming

Example 2: C if...else statement

Write a C program to check whether a number entered by user is even or odd
#include <stdio.h>
int main(){
int num;
printf
("Enter a number you want to check.\n");
scanf
("%d",&num);
if((num%2)==0) //checking whether remainder is 0 or not.
printf
("%d is even.",num);
else
printf
("%d is odd.",num);
return 0;
}
Output 1
Enter a number you want to check.
25
25 is odd.
Output 2
Enter a number you want to check.
2
2 is even.

Nested if...else statement (if...elseif....else Statement)

The nested if...else statement is used when program requires more than one test expression.

Syntax of nested if...else statement.

if (test expression1){
statement/s to be executed if test expression1 is true;
}
else if(test expression2) {
statement/s to be executed if test expression1 is false and 2 is true;
}
else if (test expression 3) {
statement/s to be executed if text expression1 and 2 are false and 3 is true;
}
.
.
.
else {
statements to be executed if all test expressions are false;
}
How nested if...else works?
The nested if...else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, it executes the statement/s inside the braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if...else
The ANSI standard specifies that 15 levels of nesting may be continued.

Example 3: C nested if else statement

Write a C program to relate two integers entered by user using = or > or < sign.
#include <stdio.h>
int main(){
int numb1, numb2;
printf
("Enter two integers to check".\n);
scanf
("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf
("Result: %d = %d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf
("Result: %d > %d",numb1,numb2);
else
printf
("Result: %d > %d",numb2,numb1);
return 0;
}
Output 1
Enter two integers to check.
5
3
Result: 5 > 3
Output 2
Enter two integers to check.
-4
-4
Result: -4 = -4

More Examples on if...else Statement

 

Post a Comment

 
Top