Control Statement
Control Statement
A program is nothing but the execution of sequence of one or more instructions. There are a
number of situations where one has to change the order of the execution of statements based on
the conditions or repeat a group of statements until certain specified conditions are met.
if statement
if - else statement
nested if - else statement
else - if ladder statement
switch statement
goto statement
break statement
These statements are known as decision-making statements. Since these statement ‘control’ the
flow of execution, they are also known as control statement.
If statement:
/* one statement */
if ( test condition)
statement;
(or)
/* group of statements */
if ( test condition )
{
statement block;
}
Eg
..........
if ( a %2 == 0)
printf (“ a is an even number ” );
..........
Program:
#include <stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr ( );
printf (“ Enter the number: ”);
scanf (“%d”, &a);
if ( a < 10 )
printf (“ a is less than 10 ” );
getch ( );
}
Output:
Enter the number: 5
a is less than 10
Enter the number: 11
If - else statement:
Eg .. ........
if (leap%4 == 0 )
printf (“ Given year is a leap year ”);
else
printf (“Given year is not a leap year”);
..........
Syntax:
/* one statement */
if ( test condition )
statement;
else
statement;
(or)
/* group of statements */
if ( test condition )
{
statement block;
}
else
{
statement block;
}
In this kind of statement, number of logical conditions are checked for executing various
statements Here, if any logical condition is true the compiler executes the block followed by if
condition, otherwise it skips and executes else block.
Syntax: (or)
Eg:
.. ........
if ( exp > 10 )
{
if (salary > 10000 )
bonus = 0.5 x salary;
else
bonus = 0.3 x salary;
}
else
{
bonus = 0.2 x salary;
}
..........
Program:/* To find biggest number */
void main ( )
{ int a, b, c;
printf (“ Enter three numbers: ”);
scanf (“%d”, &a, &b, &c);
if (a > b )
{
if ( a > c )
printf (“ a is a biggest number “);
else
printf (“c is a biggest number”);
}
else
{
if (b > c )
printf (“ c is a biggest number ”);
else
printf (“ b is a biggest number ”);
}
}
Output:
Enter three numbers: 24 79 10
b is a biggest number
Else - if ladder statement:
The conditions are evaluated from the ladder, downwards. As soon as a true condition is found,
the statement associated with it is executed and the control is transferred to statement-x
(skipping the rest of the ladder). When all the n conditions become false, then the final else
containing the default statement will be executed.
Syntax:
if ( test condition-1 )
statement-1;
{
else if (test condition-2)
statement-2;
else if (test condition-3)
statement-3;
else if (test condition-n)
statement-n;
else
default statement;
}
Eg:
.. ........
.. ........
if ( code == 1 )
colour == “ Red” ;
else if ( code == 1 )
colour == “ Green” ;
else if ( code == 1 )
colour == “ White” ;
else
colour == “ block” ;
.. ........
. ........
Program:
#include <stdio.h>
#define FIRST 75
#define SECOND 60
#define THIRD 50
void main ( )
{
int m1, m2, m3, tot, avg;
printf ( “Enter each subject mark: ” );
scanf (“ %d %d %d %d %f ”,&m1, &m2, &m3, &tot, &avg);
tot = m1+m2+m3; avg = tot/3;
printf (“ Average mark = %d”, avg);
if (avg >= FIRST)
printf (“ First class”);
{
else if (avg >= SECOND)
printf (“ Second class”);
else if (avg >= THIRD)
printf (“ Third class”);
else
printf (“Fail”);
}
}
Output:
Average mark = 99
First class
Switch statement:
switch ( expression )
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
.......
.......
case value-n:
block-n;
break;
default:
default-block;
Eg:
.. ........
switch (ch)
{
case 1:
printf (“\n Enter your name”);
fopen ( );
break;
case 2:
printf (“\n Enter your reg.no”);
fopen ( );
break;
default:
printf (“\n Invalid Choice”);
.. ........
Program:
#include <stdio.h>
void main ( )
{
int a, b, c, ch ;
printf (“\n \t [1] Addition”);
printf (“\n \t [2] Subtraction”);
printf (“\n \t [3] Multiplication”);
printf (“\n \t [4] Division”);
printf (“\n\n\t Enter your choice:”);
scanf(“%d”, &ch);
if ( ch <= 4 && ch > 1 )
{
printf (“ Enter two numbers :”);
scanf (“%d %d”, &a, &b);
}
switch (ch)
{
case 1:
c = a + b;
printf (“\n Addition: %d”, c);
break;
case 2:
c = a - b;
printf (“\n Subtraction: %d”, c);
break;
case 3:
c = a * b;
printf (“\n Multiplication: %d”, c);
break;
case 4:
c = a / b;
printf (“\n Division: %d”, c);
break;
default:
printf (“\n Invalid Choice”);
}
}
Output:
[1] Addition
[2] Subtraction
[3] Multiplication
[4] Division
Enter your choice: 3
Enter two numbers: 2 7
Multiplication: 14
Break statement:
Syntax:
Eg:
.......
.......
goto add;
else
goto sub;
add:
c = a + b;
sub:
c = a – b;
.......
.......
Program:
void main ( )
int x;
if ( x%2 == 0)
goto even;
else
goto odd;
even:
return;
odd:
Output:
Enter a number: 5
5 is odd number