Table of Contents
show
if-else statement
Most of the problems require one set of actions to be performed if a particular condition is true, and another set of actions to be performed if the particular condition is false.
Syntax
if(expression)
{
Statement 1
}
else
{
Statement 2
}
Flowchart
An if-else statement is executed as follows
- The if-else controlling expression is evaluated
- If the if else controlling expression evaluates to true, the statement constituting the body is executed and else body is skipped
- If the if else controlling expression evaluates to false, the statement constituting the body is skipped and else body is executed
Example
#include<stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is even number",n);
}
else
{
printf("%d is odd number",n);
}
return 0;
}
Output 1
Enter a number: 4
4 is even number
Output 2
Enter a number: 5
5 is odd number
Views: 0