C 2
C 2
C 2
control
statement
Iteration
statement
Transfer
statement
Decision Control statement:-
Decision control statement disrupt or alter the
sequential execution of the statement of the program
depending on the test condition in program
1. If
statement
Decision
2. If else 3. Switch
statement control statement
statement
4. Go To
statement
The If statement is a powerful decision making statement
and is used to control the flow of execution of statement.
FALSE
condition
TRUE
Block of if
Next statement
STOP
main()
{
int a;
printf(“enter value of a”);
scanf(“%d”,&a);
if(a>25)
{
printf(“no.is greater than 25”);
}
printf(“\n bye”);
getch();
}
If the condition is true the true block is execute otherwise
False block is execute.
FALSE
condition
TRUE
Block of if Block of else
Next statement
STOP
main()
{
int n,c;
printf(“\n enter value of n”);
scanf(“%d”,&n);
c=n%2;
if(c==0)
printf(“no is even”);
else
printf(“no is odd”);
getch();
}
What Is Else If Ladder:
If we are having different - different test conditions with different - different statements,
then for these kind of programming we need else if ladder
Body of the
loop
Test
conditio
n?
Entry controlled loop exit controlled loop
• C language provides three constructs for
perfoming loop operations
• While statement
• Do statements
• For statements
While(test condition)
{
do
{
For(intialization;testcondition;icrement)
{
body of the loop
}
• Intialization of control variable done
first using assignment statement
• The value of control variable tested
using test condition,ie reletional
expression such as i>10,that determine
when the loop will exit
• If the condition is true ,the body of
loop executed,otherwise terminated
int_sum=0;
for(int_n=1;int_n<=10;int_n++)
{
int_sum=int_sum+int_n;
}
printf(“sum=%d\n”,int_sum);
Nesting of for loop
For(i=0;i<n;i++)
{
………………………………
For(j=0;j<n-1;j++)
{
………………………………
}
}
Jumping out of a loop
Syntax :-
switch(expression)
{
case constant : statement;
break;
default : statement;
}
main()
{
char choice;
printf(“enter any alphabet”);
scanf(“%d”,& choice);
switch(choice)
{
case ‘a’:
printf(“this is a vowel \n”);
break;
case ‘e’ :
printf(“this is a vowel \n”);
break;
case ‘i’ :
printf(“this is a vowel \n”);
break;
case ‘o’ :
printf(“this is a vowel \n”);
break;
case ‘u’ :
printf(“this is a vowel \n”);
break;
default :
printf(“this is not a vowel”);
getch();
}
}
Go To statement
A GO TO statement can cause program control to end up anywhere in the
program unconditionally.
Example :-
main()
{
int i=1;
up : printf(“Hello To C”)
i++;
If (i<=5)
goto up
getch();
}