Conditional clauses are statements defining the conditions.At certain times we want our statement to execute when something occurs ie the condition is fulfilled.For conditions we use relational operators to define our condition.
There are three types of conditional clauses
- If statement
- If else if statement
- If else if else statement
- If Statment
In if conditional statement in C Language
- If the condition of if becomes true the block under the condition executes.
- If the condition of if becomes false the block under the if condition does not exexute.
Syntax in C Language:-
if (condition)
{
statements;
}
Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0;
if(a==1)
{
printf(“a is 1″);
}
getch();
}
Result:-
In this code i have written the if conditional clause with condition a==1 but since a is 0 in this c language code this condition becomes false and the block under it does not execute.
Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0;
if(a!=1)
{
printf(“a is not equal to 1″);
}
getch();
}
Result:-
In this code i have written the if conditional clause with condition a!=1 since a is 0 in this c language code this condition becomes true because 0 is not equal to one and its a true condition so the block under if executes and the output will be.
a is not equal to 1
- If else statement
In if else conditional statement in C Language
- If the condition of if becomes true the block under it executes.and the block of else does not execute.
- If the condition of if becomes false the block under the else condition executes.
- In else statement we do not write any condition it is assumed opposite to the if condition written.
Syntax in C Language:-
if (condition)
{
statements;
}
else
{
statements;
}
- Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0;
if(a==1)
{
printf(“a is equal to 1″);
}
else
{
printf(“a is not equal to 1″);
}
getch();
}
Result:-
In the if condition of the above source code in C Language the condition in if ie a==1 is false so the block under if does not execute and then the else condition executes.The output of this c language program will be
a is not equal to 1
Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=1;
if(a==1)
{
printf(“a is equal to 1″);
}
else
{
printf(“a is not equal to 1″);
}
getch();
}
Result:-
In the if condition of the above source code in C Language the condition in if ie a==1 becomes true so the block under if executes and since the if condition becomes true the else will not execute and the output of this c language program will be
a is equal to 1
- If ,else if ,else statement:-
In if , else if and else statement in C Language
- First the condition of if executes if it becomes true the block under it executes and no other block executes ie else if and else block.
- If the condition of if becomes false the condition of else if executes if it becomes true the block under it executes and else block don’t execute.
- If the condition of both if and if else becomes false the block under else executes.
- Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=2;
if(a==1)
{
printf(“a is equal to 1″);
}
else if(a==2)
{
printf(“a is equal to 2″);
}
else
{
printf(“a is not equal to 1 or 2″);
}
getch();
}
Result:-
In the if condition of the above source code in C Language the condition in if ie a==1 is false so the block under if does not execute it then checks the else if condition ie a==2 this condition is true because a is 2 so the block under it executes.The output of this c language program will be
a is equal to 2
- Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=3;
if(a==1)
{
printf(“a is equal to 1″);
}
else if(a==2)
{
printf(“a is equal to 2″);
}
else
{
printf(“a is not equal to 1 or 2″);
}
getch();
}
Result:-
In the if condition of the above source code in C Language the condition in if ie a==1 is false so the block under it doesn’t execute it then checks the else if condition ie a==2 this condition is also false because a is 3 so the block under it will not execute.Finally the else block executes and the output of this c language program will be
a is not equal to 1 or 2
In If condition we can write as many condition and we can also connect two condition using logical operators described before.
Syntax will be
if (condition && condition)
{
statements;
}
Now i am writing a code which takes input from the user between number 1 to 5 and print the output on the screen.
Code in C Language:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(“Write a number between 1 and 5″);
scanf(“%d”,&a);
if(a==1)
{
printf(“a is equal to 1″);
}
else if(a==2)
{
printf(“a is equal to 2″);
}
else if(a==3)
{
printf(“a is equal to 3″);
}
else if(a==4)
{
printf(“a is equal to 4″);
}
else if(a==5)
{
printf(“a is equal to 5″);
}
else
{
printf(“a is not between 1 and 5″);
}
getch();
}
In if else conditions you can also write a character also
Example
char ch=’a';
if (ch== ‘a’)
{
printf(“Hello”);
}
This is all for the if ,else if and else conditions