When we need to execute a block of statements only when a given condition is true then we use if statement.
C programming language provides the following types of decision making statements.
- If statement
- If-else statement
- If else-if ladder
- Nested if
If statement
The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.
if(expression){
//code to be executed
}
Example of C language if statement-
#include <stdio.h>
int main()
{
int x = 40;
int y = 52;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output-
Variable x is less than y
The condition (x<y) specified in the “if” returns true for the value of x and y, so the statement inside the body of if is executed.
Multiple if statement
Program to find the largest number of the three.
#include <stdio.h>
|
int main()
{
int a, b, c;
printf("Enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
Output-
Enter three numbers
34 65 23
65 is largest
If-else statement
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
Syntax-
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Example-Check whether number is even or odd.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
If else-if ladder Statement
The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement.
Syntax
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Program to calculate the grade of the student according to the specified marks.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Nested If..else statement
When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Example-
#include <stdio.h>
int main () {
/* local variable definition */
int a = 1000;
int b = 2000;
/* check the boolean condition */
if( a == 1000 ) {
/* if condition is true then check the following */
if( b == 2000) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}