Decision making statements
Decision making statements are used to skip or to execute a group of statements based on the result of some conditions.
Simple if statements
If…else statement
Nested if
Switch statement
If statement
Logical if statement
Logical if statement is used to execute or skip one statement or group of statements for a particular conditions.
The general form is
If(test condition)
{
Statements;
}
Next statement
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int mark;
char grade;
clrscr();
Printf(“enter mark and grade”);
scanf("%d %c",&mark,&grade);
if (grade==’A’)
{
mark=mark+10;
}
printf(“%d”,mark);
getch();
}
output:
enter mark and grade: 75,A
85
if---else statement
if…..else statement is used to execute one group of statements if the test condition is true or other group of statements if the condition is false.
the general form is
if(condition)
{
statement block-1;
}
else
{
statement block2;
}
next statement;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int mark;
clrscr();
printf(“enter mark”);
scanf(“%d”,& mark);
if(mark>=35)
printf(“pass”);
else
printf(“fail”);
getch();
}
output:
enter mark:65
pass
else….if statement
else …if ladder statement is used to take multiway decision. this statement is formed by joing if else statements
the general form is;
if(test condition-1)
{
statement block-1;
}
else if (test condition2)
[
statement block-2
}
…………………….
……………………
else if(test condition-n)
[
statement—n)’
}
else
default statement;
next statement
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf(“Give a number between 1 and 7\n”);
scanf(“%d”,&day);
if(day==1)
printf(“Monday\n”);
else if(day==2)
printf(“Tuesday\n”);
else if(day==3)
printf(“Wednesday\n”);
else if(day==4)
printf(“Thursday\n”);
else if(day==5)
printf(“Friday\n”);
else if(day==6)
printf(“Saturday\n”);
else
printf(“Sunday”);
getch();
}
output:
Give a number between 1 and 7:
5
Friday
OTHER EXAMPLES:
/* BIG NO OF A ANDF B*/
#include stdio.h>
#include<conio.h>
void main()
[
int a,b;
clrscr();
printf(“enter a and b”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“a is big”);
else
printf(“b is big”);
getch();
}
output:
enter a and b: 10 15
b is big
No comments:
Post a Comment