CONTROL STATEMENTS
Decision making or
conditional statements
Looping statements
Jumping statements
Decision making or conditional statements
If or simple if
statements
If else statements
Else if ladder
statements
Nested if
statements
Switch case
statement
If or simple if
statements
Syntax
If(condition)
{
Statements
}
Example
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
printf("Enter
the values=");
scanf("%d%d",&i,&j);
if(i>j)
{
printf("I is
big");
}
if(j>i)
{
printf("J is
big");
}
getch();
}
If
else statements
Syntax
If(condition)
{
True statements
}
else
{
False statements
}
Example 1
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
clrscr();
printf("Enter
the values=");
scanf("%d%d",&i,&j);
if(i>j)
{
printf("I is
big");
}
else
{
printf("J is
big");
}
getch();
}
Example 2
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
int fact=1;
clrscr();
printf("Enter
the value=");
scanf("%d",&n);
if(n>0)
{
for(i=1;i<=n;i++)
fact*=i;
printf("Factorial=%d",fact);
}
else
printf("Negative
Number=");
getch();
}
Nested
if statements
Syntax
If(condtion1)
{
if(condition2)
statement1;
else
statement2;
}
else
if(condition3)
Statement3;
else
Statement4;
Example
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
clrscr();
printf("Enter
the values=");
scanf("%d%d%d",&i,&j,&k);
if(i>j)
{
if(i>k)
{
printf("I is
big");
}
else
{
printf("K is
big");
}
}
else
if(k>j)
{
printf("K is
big");
}
else
printf("J is
big");
getch();
}
Student
Marklist
#include<stdio.h>
#include<conio.h>
main()
{
int
rno,m1,m2,m3,tot;
char sname[20];
float avg;
clrscr();
printf("Enter
Eno=");
scanf("%d",&rno);
printf("Enter
Student Name=");
scanf("%s",&sname);
printf("Enter
the marks=");
scanf("%d%d%d",&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3.0;
printf("***************************\n");
printf(" STUDENT MARKLIST\n");
printf("***************************\n");
printf("Student
Regitser No=%d",rno);
printf("\nStudent
Name=%s",sname);
printf("\nStudent
Marks=%d %d %d",m1,m2,m3);
printf("\nTotal=%d",tot);
printf("\nAverage=%f",avg);
getch();
}
Else
if ladder statements
Syntax
If(condtion1)
{
statement1;
}
Elseif(condition2)
{
Statement2;
}
……….
………..
Else
{
Default statement;
}
Example
1
#include<stdio.h>
#include<conio.h>
main()
{
float avg;
clrscr();
printf("Enter
Average is=");
scanf("%f",&avg);
if(avg>=80)
{
printf("A");
}
else if(avg>=60
&& avg<80)
{
printf("B");
}
else if(avg>=35
&& avg<60)
{
printf("C");
}
else
{
printf("No
Grade");
}
getch();
}
Example
2
#include<stdio.h>
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 if(day==7)
printf("Sunday
\n");
else
printf(No
days");
getch();
}
Switch
case statement
Syntax
switch(expression)
{
case 1:
case 2:
……………….
case n:
default:
Statements
}
Example
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
clrscr();
printf("Enter
the value=");
scanf("%s",&ch);
switch(ch)
{
case 'r':
printf("Red");
break;
case 'g':
printf("Green");
break;
case 'b':
printf("Blue");
break;….
default:
printf("Invalid
Code");
}
getch();
}
Looping
statements
for
while
do while
for
statement
Syntax
for(instant
variable; condition; increment or
decrement)
Example
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
printf("\n
%d",i);
getch();
}
Fibbonacci
Series
#include<stdio.h>
#include<conio.h>
main()
{
int f1,f2,f3,i,n;
clrscr();
f1=-1;
f2=1;
printf("Enter
Total Numbers\n");
scanf("%d",&n);
printf("The
Fibonacci Series is \n");
for(i=1;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf("%d\n",f3);
}
getch();
}
While
Syntax
while(condition)
{
Statements;
}
Example
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("Value=
%d",i);
i++;
printf("\n");
}
getch();
}
Count
the No of Chars, Words, Lines
#include<stdio.h>
main()
{
char c;
int nw,nl,nc;
clrscr();
nw=nc=nl=0;
while((c=
getchar())!=EOF)
{
if(c=='\n')++nl;
if(c==''||c=='\n'||c=='\t')
++nw;
}
printf(" No
of chars=%d\n No of Words %d\n No of Lines=%d\n",nc-1,nw,nl);
getch();
}
Do
while
Syntax
Do
{
Statements
}while(condition);
Example
1
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
do
{
printf("Value=
%d",i);
i++;
printf("\n");
}while(i<=5);
getch();
}
Example
2
Multiplication
Table
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,r=1;
clrscr();
printf("Multiplication
table & Number of ties");
scanf("%d%d",&i,&j);
printf("Multiplication\n");
do
{
r=r+1;
printf("%d *
%d=%d\n",r,i,r*i);
}
while(r<j);
}
Nested
for
Syntax
for(instant
variable; condition; increment or decrement)
{
for(instant
variable; condition; increment or decrement)
{
Statements
}
}
Example
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("Enter
your choice=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
Star
Triangle
#include<stdio.h>
main()
{
int i,j,k,n;
clrscr();
n=1;
for(i=5;i>0;i--)
{
k=1;
while(k<i)
{
printf("
");
k++;
}
for(j=1;j<=n;j++)
{
printf("*
");
}
printf("\n");
n++;
}
getch();
}
Number
Triangle
#include<stdio.h>
#include<conio.h>
main()
{
int p,m,q,n;
clrscr();
printf("Enter
the number of lines=\n\n");
scanf("%d",&n);
for(p=1;p<=n;p++)
{
for(q=1;q<=n-p;q++)
printf(" ");
m=p;
for(q=1;q<=p;q++)
printf("%4d
",m++);
m=m-2;
for(q=1;q<p;q++)
printf("%4d",m--);
printf("\n\n");
}
getch();
}
Jumping
statements
Conditional
Jumping statements
Unconditional
Jumping statements
Conditional
Jumping statements
Break Statement
Continue Statement
Break statement
#include<stdio.h>
#include<conio.h>
main()
{
int i=1;
clrscr();
while(i<=10)
{
printf("\n
%d",i);
if(i==5)
break;
i++;
getch();
}
}
Continue
Statement
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
clrscr();
printf("Enter
the value=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
continue;
printf("%d\n",i);
}
printf("Done");
getch();
}
Unconditional
Jumping statement
Return statement
Goto statement
Return
statement
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("Welcome
guys");
return;
}
Goto
Statement
#include<stdio.h>
#include<conio.h>
main()
{
int x=16,y=11;
clrscr();
if(x==y)
x++;
else
goto ERROR;
ERROR:
printf(
"Error
Code");
getch();
}

ty
ReplyDelete