‘if’ Statement in C
What is Decision Making Statement?
In ‘c’ language, execution flow of the program is, line by line from top to bottom. That means the c program is executed line by line from the main method. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code.
Consider a situation, where we write a program to check whether a student has passed or failed in a particular subject. Here, we need to check whether the marks are greater than the pass marks or not. If marks are greater, then we take the decision that the student has passed otherwise failed. To solve such kind of problems in c language we use the statements called decision making statements.
Decision making statements are the statements which are used to verify a given condition and decides whether a block of statements gets executed or not based on the condition result.
In c programming language, there are two decision making statements they are as follows…
- if statement
- switch statement
if statement in c
In c, if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements are executed or not based on the condition result.
In c language, ‘if’ statement is classified into four types as follows…
- Simple if statement
- if – else statement
- Nested if statement
- if-else-if statement (if-else ladder)
1)Simple if statement
Simple if statement is used to clarify the given condition and executes the block of statements based on the condition result. The simple if statement evaluates specified condition. If it is TRUE, it executes(starts) the next statement or block of statements. If the condition is FALSE, it skips the execution of the next statement or block of statements. The general syntax and execution flow of the simple if statement is given below:-
Simple if statement is used when we have only one option that is executed or skipped based on a condition.
Example Program | Write a program to test whether given number is divisible by 5.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf(“Enter any integer number: “) ;
scanf(“%d”, &n) ;
if ( n%5 == 0 )
printf(“Given number is divisible by 5\n”) ;
printf(“statement does not belong to if!!!”) ;
}
Output 1:
Enter any integer number: 25
Given number is divisible by 5
statement does not belong to if ! ! !
Output 2:
Enter any integer number: 33
statement does not belong to if ! ! !
if – else statement
The if – else statement is used to verify the given condition and executes only one out of the two blocks of statements based on the condition result. The if-else statement evaluates the specified condition. If it is TRUE, it executes a block of statements (True block). If the condition is FALSE, it executes another block of statements (False block). The general syntax and execution flow of the if-else statement is as follows…
The if-else statement is used when we have two options and only one option has to be executed based on a condition result (TRUE or FALSE).
Example Program | Test whether given number is even or odd.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf(“Enter any integer number: “) ;
scanf(“%d”, &n) ;
if ( n%2 == 0 )
printf(“Given number is EVEN\n”) ;
else
printf(“Given number is ODD\n”) ;
}
Output 1:
Enter any integer number: 10
Given number is EVEN
Output 2:
Enter any integer number: 13
Given number is ODD
Nested if statement
Whenever we write an if statement inside the another if statement is called nested if statement. The general syntax of the nested if statement is as follows…
The nested if statement can be defined using any combination of simple if & if-else statements.
Example Program | Test whether given number is even or odd if it is below 100.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf(“Enter any integer number: “) ;
scanf(“%d”, &n) ;
if ( n < 100 )
{
printf(“Given number is below 100\n”) ;
if( n%2 == 0)
printf(“And it is EVEN”) ;
else
printf(“And it is ODD”) ;
}
else
printf(“Given number is not below 100”) ;
}
Output 1:
Enter any integer number: 75
Given number is below 100
And it is ODD
Output 2:
Enter any integer number: 200
Given number is not below 100
if – else – if statement (if-else ladder)
Writing a if statement inside else of a if statement is called if – else – if statement. The general syntax of the if-else-if statement is as follows…
The if-else-if statement can be defined using any combination of simple if & if-else statements.
Example Program | Find the largest of three numbers.
#include<stdio.h>
#include<conio.h>
void main(){
int a, b, c ;
clrscr() ;
printf(“Enter any three integer numbers: “) ;
scanf(“%d%d%d”, &a, &b, &c) ;
if( a>=b && a>=c)
printf(“%d is the largest number”, a) ;
else if (b>=a && b>=c)
printf(“%d is the largest number”, b) ;
else
printf(“%d is the largest number”, c) ;
}
Output:
Enter any three integer number: 10 20 30
30 is the largest number
MOST IMPORTANT POINTS TO BE REMEMBERED
When we use
conditional control statement like if statement, condition might be an
expression evaluated to a numerical value, a variable or a direct numerical
value. If the expression value or direct value is zero the conditon
becomes FALSE otherwise becomes TRUE.
To understand more consider the following statements…
- if(10) – is TRUE
- if(x) – is FALSE if x value is zero otherwise TRUE
- if(a+b) – is FALSE if a+b value is zero otherwise TRUE
- if(a = 99) – is TRUE because a value is non-zero
- if(10, 5, 0) – is FALSE because it considers last value
- if(0) – is FALSE
- if(a=10, b=15, c=0) – is FALSE because last value is zero