Condition Statement In C Language
Condition statement are used to make decision
based certain conditions. Conditions are specify by a set of conditional statement having Boolean expression ,which are evaluated
to a Boolean value true and false.
There are following types of
conditional statement in c.
1.if statement
2.if-else statement
3.nested if-else
statemen
4.ladder if-else
statement
5. switch statement
1.if statement
The signal if statement
in c language is used to execute the
code if a condition is true then statement will be execute.
Syntax:
If(expression)
{
//Statement;
}
Fig: If statement |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf(“enter one
number”);
scanf(“%d”,&num);
if(num%2==0)
{
Printf(“%d print that
number is even “, num);
}
getch();
}
Output:
Enter one number
2
Print the number is
even
2. if-else statement
The if –else statement in c language is used to execute the code if a condition is
true or false then statement will check the condition is true then execute the
true block and otherwise execute false block.
Syntax:
if(expression)
{
//Statement;
}
else
{
Statements;
}
Fig: if-else statement |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf(“enter one
number”);
scanf(“%d”,&num);
if(num%2==0)
{
Printf(“%d print that
number is even “, num);
}
else
{
Printf(“%d print the
number is odd”, num);
getch();
}
Output:
Enter one number
5
Print the number is odd
3. nested if-else statement
The nested if-else statement is used when a program
requires more than one test expression.when a series of the decision are
involved in a statement, we use if else statement in nested form.
Syntax:
if(rexpression)
{
if(expression)
{
//Statement-block1;
}
else
{
//Statement-block2;
}
}
else
{
//Statement- block3;
}
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2,num3;
printf(“enter 3 number”);
scanf(“%d%d%d”,&num1,&num2,&num3);
if(num1>num2)
{
if(num1>num3)
{
printf(“num1 is
greatest”);
}
else
{
printf(“num3 is
greatest”);
}
}
else
{
if(num2>num3)
{
printf(“num2 is
greatest “);
}
else
{
printf(“num3 is
greatest”);
}
}
}
getch();
}
Output:
Enter 3 number
77
87
90
C is greatest
4.ladder if-else statement
The if-else-if
statement is used to execute one code from multiple conditions. It is a chain
of if..else statement in which each statement is associated with else if statement
and last would be an else statement.
Syntax:
if(condition)
{
//statement;
}
else if(condition2)
{
//statement;
}
else if(condition3)
{
//statement;
}
else
{
//statement;
}
Fig: if-else-if ladder |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf(“enter a number”);
scanf(“%d “,&num);
if(num%5==0 &&
num%8==0)
{
printf(“division by
both 5 and 8”);
}
else if(num%8==0)
{
printf(“division by 8”);
}
else if(num%5==0)
{
printf(“division by 5”);
}
else
{
printf(“division by none”);
}
}
getch();
}
Output:
Enter a number
40
Division by 5 and 8
5.switch statement
Switch statement acts
as a substitute for a long if-else-if ladder that is used to test a list of
case . A switch statement contain one or more case labels which are tested
against the switch expression .when the expression match to a case then the associated
statement with that case would be executed.
Syntax:
Switch(expression)
{
case value 1:
//statement
break;
case value 2:
//statement
break;
case value 3:
//statement
break;
case value n:
//statement
break;
Default:
//statement
}
Fig: switch statement |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char operator;
double n1,n2;
printf(“enter an operator
(+,-,*,/): ”);
scanf(“%c”,&operator);
printf(“enter two
operands : “);
scanf(“%lf %lf”,&n1,&n2);
switch(operator)
{
case’+’:
printf(“%1lf + %1lf= %1lf”, n1,n2 , n1+n2);
break;
case’-’:
printf(“%1lf + %1lf= %1lf”, n1,n2 , n1-n2);
break;
case’*’:
printf(“%1lf + %1lf= %1lf”, n1,n2 , n1*n2);
break;
case’/’:
printf(“%1lf + %1lf= %1lf”, n1,n2 , n1/n2);
break;
default:
printf(“Error! Operator
is not correct”);
}
getch();
}
Output:
Enter an operator
(+,-,*,/) : -
Enter two
operands: 32.5
10.5
32.5-10.5=22
No comments:
Post a Comment