You are on page 1of 35

Control structures

Flow control statements


• The order in which the program statements are executed is known as
“flow of program control” or just “flow of control”. They are of two
types:
• A) Branching statements
(Selection statements & Jump statement)
• B) Iteration statements
Branching statements
• They are used to transfer the program control from
one point to another. They are categorized as 1)
conditional branching 2) unconditional branching.
• Conditional branching is based upon the outcome of a
certain condition.(if statement, if-else statement and
switch statement).
• Unconditional branching has no condition.(goto
statement, break statement, continue statement and
return statement).
‘if’ statement
‘if else’ statement
Nested if else statement
‘switch’ statement
‘switch’ statement
Program
Program
Program
Iteration statements
• Iteration is a process of repeating the same set of
statements again and again until the specified
condition holds true.
• Computers execute the same set of statements again
and again by putting them in a loop.
• A portion of program that is executed repeatedly is
called a loop.
• The C language provides the following three iteration
statements: 1) for 2) while 3) do-while
Classification of loops
• In general loops can be classified as:
1) Counter- controlled loops
• Counter controlled loops is a form of looping in which the number of
iterations to be performed is known in advance.
• It is so named because they use a control variable, known as the loop
counter, to keep a track of loop iterations.
• It starts with an initial value of the loop counter and
terminates when the final value of the loop counter is
reached.
• Since it iterates a fixed number of times, which is
known in advance, they are also known a definite
repetition loops.
• Three main ingredients are:
1) Initialization.
2) An expression (specifically a condition) determining
whether the loop body should be executed or not.
3) An expression that manipulates the value of the loop
counter so that the condition in step 2 eventually
becomes false and the loop terminates.
2) Sentinel- controlled loops
• In sentinel controlled looping, the number of times
the iteration is to be performed is not known
beforehand.
• The execution or termination of the loop depends
upon a special value called the sentinel value.
• If the sentinel value is true, the loop body will be
executed, otherwise it will not.
• Since the number of times a loop will iterate is not
known in advance, this type of loop is known as
indefinite repetition loop.
‘for’ loop
Program
Exercise Programs:
•Program 1: Program to Check Vowel or consonant: Get a character as input from the user. Check and display
whether the input character is vowel or consonant.
•Program 2- extension: To display error message when non-alphabetical character is entered, use isalpha()
function from ctype.h header file.
•Program 3: Sum of Natural Numbers Using for Loop. Get a positive integer as input ‘n’ from the user. Print the
sum upto that ‘n’.
•Program 3 – Extension: Repeat the same task using while loop
‘while’ loop
Program
‘do-while’ loop
Program
#include <stdio.h>
int main ()
{ Output:
value of a: 10
/* local variable definition */ value of a: 11
int a = 10; value of a: 12
/* do loop execution */ value of a: 13
value of a: 14
do value of a: 15
{ value of a: 16
printf("value of a: %d\n", a); value of a: 17
value of a: 18
a = a + 1; value of a: 19
}while( a < 20 );
return 0;
}
Nested loops
• If the body of a loop is, or contains another iteration statement, then
we say that the loops are nested.
Program for Nested loops
#include <stdio.h>
int main()
Output:
{
int i,j;
for(i=1;i<=4;i++)
{
for (j=1;j<=4;j++)
printf("*");
printf("\n");
}
return 0;
}
goto statement
• This statement doesn’t require any condition
• This statement passes control anywhere in the program ie.. control is
transferred to another part of the program without testing any
condition (within the function).
• Forward jump and Backward jump.
• Expression is : goto label
Program
#include<stdio.h> even:
#include<conio.h> printf(“%d is even number”);
#include<stdlib.h> return;
void main() odd:
{ printf(“%d is odd number”);
int x; return; }
printf(“enter number:”);
scanf(“%d”,&x);
if(x%2==0)
{
goto even;
}
else
{
goto odd;
}
break statement
• It can appear only inside or as a body of a switch
statement or a loop.
• This keyword allows the programmers to terminate
the execution of the nearest enclosing switch or the
nearest enclosing loop.
• It skips from the loop or block in which it is defined.
• The control then automatically goes to the first
statement after the loop or block
continue statement
• This statement is exactly opposite to break.
• It can appear only inside, or as the body of, a loop.
• It terminates the current iteration of the nearest enclosing loop.
Difference between break and continue
Break:
1) Exits from current block or loop
2) Control passes to next statement
Continue:
1) Loop takes next iteration
2) Control passes at the beginning of loop

You might also like