Control Flow: Selection Structures
Control Flow: Selection Structures
Control Flow: Selection Structures
Control Flow
The control-flow of a language specifies the order in which computations are performed. Control
structures formthe basic entities of a structured programming language. Control structures are used
to alter the flow of execution of the program. We use control structures to make decisions and alter
the direction of programflow in one or the other path(s) available.
There are three types of control structures available in C
1) Sequence structure (straight line paths)
2) Selection structure (one or many branches)
3) Loop structure (repetition of a set of activities)
Selection structures are used to perform decision making and then branch the programflow
based on the outcome of decision making. Selection structures are implemented in C with if, if else
and switch statements. if and if else statements are 2 way branching statements where as Switch is a
multi branching statement.
The simple If statement
The syntax of a simple if statement is as shown below.
if (expression) // This expression is evaluated. If expression is TRUE statements inside the braces will
be executed
{
statement1;
statement2;
}
statement 3; // Programcontrol is transferred directly to this line, if the expression is FALSE
The expression given inside the brackets after if is evaluated first. If the expression is true, then
statements inside the curly braces that follow if(expression) will be executed. If the expression is
false, the statements inside curly braces will not be executed and programcontrol goes directly to
statements after curly braces.
Example program to demo If statement
Problem:-A simple example program to demo the use of If, If Else and Switch is shown here. An
integer value is collected from user. If the integer entered by user is 1 output on screen UNITED
STATES. If the integer is 2 output SPAIN, If the integer is 3 output INDIA. If the user enters
some other value output WRONG ENTRY.
if (expression)
{
statement1;
statement2;
}
statement 3;
2
#include<stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
if(num==1)
{
printf("UNITED STATES");
}
if(num==2)
{
printf("SPAIN");
}
if(num==3)
{
printf("INDIA");
}
}
The if else statement.
Syntax for If Else statement is shown below.
if(expression 1)// Expression 1 is evaluated. If TRUE, statements inside the curly braces are executed.
{ //If FALSE programcontrol is transferred to immediate else if statement.
statement1;
statement2;
}
else if(expression 2)// If expression 1 is FALSE, expression 2 is evaluated.
{
statement1;
statement2;
}
else if(expression 3) // If expression 2 is FALSE, expression 3 is evaluated
{
statement 1;
statement 2;
}
else // If all expressions (1, 2 and 3) are FALSE, the statements that follow this else (inside curly
braces) is executed.
{
statement 1;
statement 2;
}
other statements;
if(expression 1)
{
statement1;
statement2;
}
else if(expression 2)
{
statement1;
statement2;
}
else
{
statement 1;
statement 2;
}
other statements;
3
The execution begins by evaluation expression 1. If it is TRUE, then statements inside the immediate
curly braces are evaluated. If it is FALSE, program control is transferred directly to immediate else if
statement. Here expression 2 is evaluated for TRUE or FALSE. The process continues. If all
expressions inside the different if and else if statements are FALSE, then the last else statement
(without any expression) is executed along with the statements 1 and 2 inside the curly braces of
last else statement.
Example program to demo If Else
#include<stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
if(num==1)
{
printf("UNITED STATES");
}
else if(num==2)
{
printf("SPAIN");
}
else if(num==3)
{
printf("INDIA");
}
else
{
printf("WRONG ENTRY"); // See how else is used to output "WRONG ENTRY"
}
}
Switch statement
Switch is a multi branching control statement. Syntax for switch statement is shown below.
switch (expression)// Expression is evaluated. The outcome of the expression should be an integer or a
character constant
{
case value1: // case is the keyword used to match the integer/character constant fromexpression.
//value1, value2 ... are different possible values that can come in expression
statement 1;
statement 2;
break; // break is a keyword used to break the programcontrol fromswitch block.
case value2:
statement 1;
statement 2;
break;
default: // default is a keyword used to execute a set of statements inside switch, if no case values
match the expression value.
statement 1;
statement 2;
break;
}
4
Execution of switch statement begins by evaluating the expression inside the switch keyword
brackets. The expression should be an integer (1, 2, 100, 57 etc ) or a character constant
like a, b etc. This expressions value is then matched with each case values. There can be any
number of case values inside a switch statements block. If first case value is not matched with the
expression value, programcontrol moves to next case value and so on. When a case value matches
with expression value, the statements that belong to a particular case value are executed.
If no case values are matched with expression value, the set of statements that follow default: will get
executed.
Example program to demo working of switch
#include<stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
switch(num)
{
case 1:
printf("UNITED STATES");
break;
case 2:
printf("SPAIN");
break;
case 3:
printf("INDIA");
default:
printf("WRONG ENTRY");
}
}
switch(expression)
{
case value1:
statement 1;
statement 2;
break;
case value2:
statement 1;
statement 2;
break;
default:
statement 1;
statement 2;
break;
}
5
Loop structures
A loop structure is used to execute a certain set of actions for a predefined number of times or until a
particular condition is satisfied. There are 3 control statements available in C to implement loop
structures. while, do while and for statements.
The while statement
Syntax for while loop is shown below:
while(condition)// This condition is tested for TRUE or FALSE. Statements inside curly braces are
executed as long as condition is TRUE
{
statement 1;
statement 2;
statement 3;
}
The condition is checked for TRUE first. If it is TRUE then all statements inside curly braces are
executed. Then program control comes back to check the condition has changed or to check if it is
still TRUE. The statements inside braces are executed repeatedly, as long as the condition is TRUE.
When the condition turns FALSE, programcontrol exits fromwhile loop.
Example program to demo working of while loop
An example programto collect a number from user and then print all numbers fromzero to that
particular collected number is shown below. That is, if user enters 10 as input, then numbers from0 to
10 will be printed on screen.
#include<stdio.h>
void main()
{
int num;
int count=0; // count is initialized as zero to start printing fromzero.
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
while(count<=num) // Checks the condition - if value of count has reached value of numor not.
{
printf("%d",count);
count=count+1; // value of count is incremented by 1 to print next number.
}
}
while(condition)
{
statement 1;
statement 2;
statement 3;
}
6
The do while statement
Syntax for do while loop is shown below:
do
{
statement 1;
statement 2;
statement 3;
}
while(condition);
Unlike while, do while is an exit controlled loop. Here the set of statements inside braces are executed
first. The condition inside while is checked only after finishing the first time execution of statements
inside braces. If the condition is TRUE, then statements are executed again. This process continues as
long as condition is TRUE. Programcontrol exits the loop once the condition turns FALSE.
Example program to demo working of "do while"
#include<stdio.h>
void main()
{
int num;
int count=0; // count is initialized as zero to start printing fromzero.
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
do
{
printf("%d",count); // Here value of count is printed for one time intially and then only condition is checked.
count=count+1; // value of count is incremented by 1 to print next number.
}while(count<=num);
}
The for statement
Syntax of for statement is shown below:
for(initialization statements; test condition; iteration statements)
{
statement 1;
statement 2;
statement 3;
}
do
{
statement 1;
statement 2;
statement 3;
}
while(condition);
7
Working of for loop:
The program control enters the for loop. At first it executes the statements given as initialization
statements. Then the condition statement is evaluated. If conditions are TRUE, then the block of
statements inside curly braces is executed. After executing curly brace statements fully, the control
moves to the "iteration" statements. After executing iteration statements, control comes back to
condition statements. Condition statements are evaluated again for TRUE or FALSE. If TRUE the
curly brace statements are executed. This process continues until the condition turns FALSE.
Example program to demo working of "for loop"
#include<stdio.h>
void main()
{
int num,count;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number fromuser
for(count=0;count<=num;count++)// count is initialized to zero inside for statement. The condition is checked
and statements are executed.
{
printf("%d",count); // Values from 0 are printed.
}
}
Jump Statements
J ump statements can be used to modify the behavior of conditional and iterative statements. J ump
statements allow you to exit a loop, start the next iteration of a loop, or explicitly transfer program
control to a specified location in your program.
goto: The goto statement transfers programexecution to some label within the program.
Syntax:
goto label;
....
label:
Example:
goto skip_point;
printf("This part was skipped.\n");
skip_point: printf("Hi there!\n");
Output: Only the text "Hi there!" is printed.
for(initialization statements; test condition; iteration statements)
{
statement 1;
statement 2;
statement 3;
}
8
continue: The continue statement can only appear in a loop body. It causes the rest of the
statement body in the loop to be skipped.
Syntax:
continue;
Example:
for(loop=0;loop<100;loop++)
{
if(loop==50)
continue;
printf("%i\n",loop);
}
Output: The numbers 0 through 99 are printed except for 50.
break: The break statement can only appear in a switch body or a loop body. It causes the
execution of the current enclosing switch or loop body to terminate.
Syntax:
break;
Examples:
for(loop=0;loop<50;loop++)
{
if(loop==10)
break;
printf("%i\n",loop);
}
Output: Only numbers 0 through 9 are printed.