- In decision control statements (C if else and nested if), group of statements are executed when condition is true. If condition is false, then else part statements are executed.
- If statement
- If-else statement
- Nested if statement
NOTE- PLEASE FIND THE ATTACHMENT WITH THIS NOTES FOR SYNTAX AND DESCRIPTION OF DECISION CONTROL STATEMENT
Example program for if statement in C:
In “if” control statement, respective block of code is executed when condition is true.
#include<stdio.h>
#include<conio.h>
void main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
getch();
}
Output:
m and n are equal
Example program for if else statement in C:
In C if else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed.
#include <stdio.h>
#include<conio.h>
void main()
{
int m=40,n=20;
if (m == n) {
printf("m and n are equal");
}
else {
printf("m and n are not equal");
}
getch();
}
Output:
m and n are not equal
Example program for nested if statement in C:
- In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true.
- If condition 2 also gets failure, then else part is executed.
#include <stdio.h>
#include<conio.h>
void main()
{
int m=40,n=20;
if (m>n)
{
printf("m is greater than n");
}
else if(m<n>
{
printf("m is less than n");
}
else
{
printf("m is equal to n");
}
getch();
}
Output:
m is greater than n
No comments:
Post a Comment