CHAPTER-3
Control statement
DECISION MAKING
*If
syntax
if(condition)
{
statement;
}
if(condition)
{
statement;
}
m>=50 pass
m<50 fail
PROGRAM
#include
<stdio.h>
#include<conio.h>
main()
{
int m;
clrscr();
printf("enter
your mark");
scanf("%d",&m);
if(m>=50)
{
printf("pass");
}
if(m<50)
{
printf("fail");
}
getch();
}
output:
enter your mark
65
pass
15
fail
if else
syntax
if(condition)
{
statement;
}
else
{
statement;
}
m>=50 pass
m<50 fail
PROGRAM
#include
<stdio.h>
#include<conio.h>
main()
{
int m;
clrscr();
printf("enter
your mark");
scanf("%d",&m);
if(m>=50)
{
printf("pass");
}
else
{
printf("fail");
}
getch();
}
output:
enter your mark
65
pass
15
fail
Nested if
syntax
if(condition)
{
if(condition)
{
statement;
}
else
{
statement;
}
}
else{
if(condition)
{
statement;
}
else
{
statement;
}
}
#include<stdio.h>
#include<conio.h>
main()
{
int m;
clrscr();
printf("enter your mark");
scanf("%d",&m);
if (m>=50)
{
if (m>=80)
{
printf("a grade");
}
else{
printf("b grade");
}
}
else
{
if(m>=30)
{
printf("c grade");
}
else{
printf("fail");
}
}
getch();
}
output
65-b grade
80-a grade
23-fail
else if ladder
syntax
if(condition)
{
statement;
}
else if (condition)
{
statement;
}
else if (condition)
{
statement;
}
else{
statement;
}
program
#include<stdio.h>
#include<conio.h>
main()
{
int m;
clrscr();
printf("enter
your mark");
scanf("%d",&m);
if(m>=80&&m<=100)
{
printf("a
grade");
}
else
if(m>=50&&m<=80)
{
printf("b
grade");
}else
if(m>=30&&m<=50)
{
printf("c
grade");
}
else{
printf("fail");
}
getch();
}
output
enter your mark
65-b grade
90-a grade
40-c grade
20-d grade
switch case
syntax
switch (condition)
{
case <value>:
statement;
break;
case <value>:
statement;
break;
default:
statement;
break;}
program
#include<stdio.h>
#include<conio.h>
main()
{
int d;
clrscr();
printf("enter day");
scanf("%d",&d);
switch(d)
{
case 1:
printf("sunday");
break;
case 2:
printf("monday");
break;
case 3:
printf("tuesday");
break;
case 4:
printf("wednesday");
break;
case 5:
printf("thursday"));
break;
case 6:
printf("friday");
break;
case 7:
printf("saturday");
break;
default:
printf("enter valid number");
}
getch();
}
output
1
sunday
7
saturday
LOOPING
For loop
syntax
for(initialization;condition
checking;increment/decrement)
{
statement;
}
program
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=0;i<=10,i++)
{
printf("%d\n",i);
}
getch();
}
output
0
1
2
3
4
5
6
7
8
9
10
while loop
(entry
controlled loop)
syntax:
while (condition)
{
statement;
}
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i=5;
clrscr();
while(i<5)
{
printf("%d",i);
i++;
}
getch();
}
Output:
_
Do - While (Exit controlled loop)
syntax:
do{
statement;
}
while(condition);
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i=5;
clrscr();
do(
printf("%d",i);
i++;
}
while(i<5);
getch();
}
output:
5
Jumping
BREAK
Syntax
break;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int;
clrscr();
for(i=0;i<=10;i++)
{
if(i==5)
break;
printf("%d",i)
}
getch();
}
output
0
1
2
3
4
Continue
Syntax:
continue
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
if(i==5)
continue;
printf("%d",i)
}
getch();
}
output:
0
1
2
3
4
5
6
7
8
9
10
Return
return statement used to return from a function
syntax
return expression;
eg.return
o;
program
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("hi hello");
return o;
}
To view output click Alt+F5(user
screen view)
output
hi hello
Goto
syntax
goto statement-lable;
Program
#include<stdio.h>
#include<conio.h>
main()
{
int x=5,y=3;
if(x==y)
{
printf("EQUAL");
}
else{
goto ERROR;
ERROR:
printf("not equal")
}
getch();
}
output
not equal
0 comments:
Post a Comment