Chapter 10: Control
Statement
Fig: 1 Classification of control statements
SELECTION STATEMENTS
• The selection statements are also known as Branching or Decision
Control Statements.
Decision making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Situation:
• Sometime we come across situations where we have to make a
decision. E.g. If the weather is sunny, I will go out & play, else I will be
at home. Here my course of action is governed by the kind of weather.
If it’s sunny, I can go out & play, else I have to stay indoors. I choose
an option out of 2 alternate options. Likewise, we can find ourselves
in situations where we have to select among several alternatives. We
have decision control statements to implement this logic in computer
programming.
if Statement
• The keyword if tells the compiler that what follows is a decision control instruction. The if
statement allows us to put some decision -making into our programs. The general form of the if
statement is shown Fig 2:
Syntax of if statement:
if (condition )
{
Statement 1;
………….. Statement n;
}
//Rest of the code
If the condition is true(nonzero), the statement will be executed. If the
condition is false(0), the statement will not be executed.
For example, suppose we are writing a billing
program.
if (total_purchase >=1000)
printf("You are gifted a pen drive.\n");
Multiple statements may be grouped by putting them inside curly braces {}. For example:
if (total_purchase>=1000)
{
gift_count++;
printf("You are gifted a pen drive.\n");
}
Seatwork:
1.Write a program to print a message if negative no is entered.
Output:
Enter a no: 6 value of no is 6
Output:
Enter a no: -2 value of no is 2
2. Write a program to perform division of 2 nos
Output:
Enter 2 nos: 6 2
quotient is 3
Output:
Enter 2 nos: 6 0 Division is not possible
if-else Statement
• The if statement by itself will execute a single statement, or a group of
statements, when the expression following if evaluates to true. By
using else we execute another group of statements if the expression
evaluates to false.
Example:
if (a > b)
{ z = a;
printf(“value of z is :%d”,z);
}
else
{ z = b;
printf(“value of z is :%d”,z);
}
The group of statements after the if is called an ‘if block’. Similarly, the statements after the
else form the ‘else block’.
Write a program to check whether the given no is even or odd
Output:
• Enter an integer 3 Odd
• Output:
• Enter an integer 4 Even
• #include<stdio.h> int main()
•{
• int n;
• printf("Enter an integer\n"); scanf("%d",&n);
• if ( n%2 == 0 )
• printf("Even\n");
• else
printf("Odd\n"); return 0;
}
Seatwork
Write a program to check whether a given year is leap year or not
Output:
Enter a year to check if it is a leap year 1996 1996 is a leap year
Output:
Enter a year to check if it is a leap year 2015 2015 is not a leap year
switch case:
This structure helps to make a decision from the number of choices. The switch statement is a
multi−way decision that tests whether an expression matches one of a number of constant
integer values, and branches accordingly.
switch( integer expression)
{
case constant 1 :
do this; case constant 2 :
do this ;
case constant 3 :
do this ; default :
do this ;
}
Flowchart
• The integer expression following the keyword switch is any C
expression that will yield an integer value. It could be an integer
constant like 1, 2 or 3, or an expression that evaluates to an integer. If
a case matches the expression value, execution starts at that case. All
case expressions must be different. The case labelled default is
executed if none of the other cases are satisfied. A default is optional;
if it isn't there and if none of the cases match, no action at all takes
place. Cases and the default clause can occur in any order.
Example:
The output of this program would be:
I am in case 2
I am in case 3
I am in default
Program:
main( )
{ int i = 2; switch ( i )
{
case 1:
printf ( "I am in case 1 \n" ) ; case 2:
printf ( "I am in case 2 \n" ) ; case 3:
printf ( "I am in case 3 \n" ) ; default :
printf ( "I am in default \n" ) ; }
}
Here the program prints case 2 and 3 and the default case. If you want that only case 2 should get
executed, it is up to you to get out of the switch then and there by using a break statement.
main( )
{
int i = 2 ; switch ( i )
{
case 1:
printf ( "I am in case 1 \n" ) ;
break ; case 2:
printf ( "I am in case 2 \n" ) ; break ;
case 3:
printf ( "I am in case 3 \n" ) ; break ;
default:
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 2
Seatwork:
WAP to enter a grade & check its corresponding remarks
Grade Remarks
A: Outstanding
B: Excellent
C: Well Done
D: You Passed
E: Better Try Again