IF...ELSE Statement
Definition:
- IF...ELSE Statement also used to control the flow of program based on condition because it is the part of IF Statement.
- IF...ELSE Statement's body will execute only when IF Statement condition evaluated as false.
- IF Statement followed by optional ELSE Statement which execute only when IF Statement condition is false.
Syntax:
The syntax of IF...ELSE Statement in C-Language
if(Test Condition)
{
//Statement(s) will execute if the Test Condition is true
}
else
{
//Statement(s) will execute if the Test Condition is false
}
{
//Statement(s) will execute if the Test Condition is true
}
else
{
//Statement(s) will execute if the Test Condition is false
}
How IF...ELSE Statement Works:
If the Test Condition is checked as true:
- Statement(s) inside the body of IF Loop will execute
- Statement(s) inside the body of ELSE will skipped from execution
If the Test Condition is checked as false:
- Statement(s) inside the body of IF Loop will not executed means skipped
- Statement(s) inside the body of ELSE will execute
Illustration of IF...ELSE Statement:
CASE-1: Test Condition is True
int a = 4;
if(a<8)
{
//Statement(s) inside the body of IF Loop will execute
}
else
{
//Statement(s) inside the body of ELSE will skipped from execution
}
CASE-2: Test Condition is False
int a = 4;
if(a>8)
{
//Statement(s) inside the body of IF Loop will not executed means skipped
}
else
{
//Statement(s) inside the body of ELSE will execute
}
Program: Write a C program to demonstrate the IF...ELSE Statement
#include<stdio.h>
int main()
{
int number;
printf("Enter the value of number:");
scanf("%d", &number);
if(number>0)
{
printf("The entered number is positive.");
}
else
{
printf("The entered number is negative.");
}
}
Output:
Enter the value of number: 3
The entered number is positive.
Enter the value of number: -3
The entered number is negative.
0 Comments
Welcome Dear Reader, I am very glad to help you | keep visiting my blog... Thanks !