C Programming Unit-3 Decision Making and Looping
C Programming Unit-3 Decision Making and Looping
INTRODUCTION
It is possible to execute a segment of a program repeatedly by introducing a counter and
later testing it using the If statement. This method is quite satisfactory for all practical
purposes, we need to initialize and increment a counter and test its value at an appropriate
place in the program for the completion of the loop. On such occasions where the exact
number of repetitions are know, there are more convenient methods of looping in C.
These looping capabilities enable us to develop concise programs containing repetitive
processes without the use of goto statements.
In looping, a sequence of statements are executed until some conditions for termination
of the loop are satisfied. A program loop consists of two segments, known as Body of the
loop and Control Statement. The control statement tests certain conditions and then
directs the repeated execution of the statements contained in the body of the loop.
Depending on the position of the control statement in the loop, a control structure may be
classified either as the entry-controlled loop or as the exit-controlled-loop.
In the entry-controlled loop (Pre-Test), the control conditions are tested before the start of
the loop execution. If the conditions are not satisfied, then the body of the loop will not
be executed. In the case of an exit-controlled-loop (Post-Test), the test performed at the
end of the body of the loop and therefore the body id executed unconditionally for the
first time.
Entry
False
True
True
False
Page 1 of 10
C Programming
The test conditions should be carefully stated in order to perform the desired number of
loop executions. In looping process, in general, would include the following four steps:
1. Setting and Initialization of a Condition Variable.
2. Execution of the Statements in the Loop.
3. Test for a Specified Value of the Condition Variable for Execution of the Loop.
4. Incrementing or Updating the Condition Variable.
The test may be either to determine whether the loop has been repeated the specified
number of times or to determine whether a particular condition has been met.
The C language provides three constructs for performing loop operations. They are:
The DO Statement.
Sentinel Loops
Based on the nature of control variable and the kind of value assigned to it for testing the
control expression, the loops may be classified into:
Counter-Controlled Loops: (or Definite Repetition Loop) We know in advance exactly
how many times the loop will be executed.
Sentinel-Controlled Loops: (or Indefinite Repetition Loop) A sentinel value is used to
change the loop control expression from true to false, EX: -1 and 999 to indicate “End of
Data”. The number of repetitions in not known before the loop begins executing.
Page 2 of 10
C Programming
The test-condition is evaluated and if the condition is true, then the body of the loop is
executed. After execution of the body, the test-condition is once again evaluated and if it
is true, the body is executed once again. This repetition process continues until the
test-condition finally becomes false and the control is transferred out of the loop. On exit,
the program continues with the statement immediately after the body of the loop.
The body of the loop may have one or more statements. The braces are needed only if the
body contains two or more statements. However, it is a good practice to use braces even
if the body has only one statement.
Example-1:
statement-y;
sum = 0;
n = 1; /* Initialization */
while ( n <= 10 ) /* Testing */
{
sum = sum + n * n ; Loop
n = n + 1; /* loop */
}
printf(“\nSum of N numbers Square = %d”,sum);
Example-2:
statement-y;
character = ‘ ‘;
while ( character != ‘Y’)
{
character = getchar( );
}
statement-x;
Page 3 of 10
C Programming
or
On reaching the do statement, the program evaluates the body of the loop first. At the
end, the test-condition in the while statement is evaluated. If the condition is true, the
program continues to evaluate the body of the loop once again and again until the
condition becomes false. The loop will be terminated on false and the control goes to the
statement immediately after the while statement.
Since the test-condition is evaluated at the bottom of the loop, the do…while construct
provides an exit-control loop and therefore the body of the loop is always executed at
least once.
Ex: .....
do
{
clrscr();
printf(“\nEnter only non-zero value for A \n”); loop
scanf(“%d %d %d, &a, &b, &c);
}
while ( a == 0 );
.....
Page 4 of 10
C Programming
Page 5 of 10
C Programming
An important about FOR loop is that all the three actions ( initialization, test-condition
and increment ) are placed in the FOR statement itself, thus making them visible to the
programmers and users, in one place.
The for statement and its equivalent of while and do statements are shown below.
FOR WHILE DO
for ( n = 1 ; n <= 10 ; ++n ) n = 1; n = 1;
{ while ( n <= 10 ) do
..... { {
..... ..... .....
} ..... .....
n = n + 1; n=n+1;
} }
while ( n < = 10 );
Table : Comparison of the Three Loops
Page 6 of 10
C Programming
The nesting may continue up to any desired level. (ANSI C allows up to 15 levels)
Page 7 of 10
C Programming
Jumps in Loops:
Loops performs a set of operations repeatedly unit the control variable fails to satisfy the
test-condition. The number of times a loop is repeated is required is decided in advanced
and the test condition is written to achieve this. Sometimes, when executing a loop it
becomes desired to skip a part of the loop or to leave the loop as soon as a certain
condition occurs.
Jumping Out of a Loop:
An early exiting from a loop can be accomplished by the break statement or the goto
statement. These can also be used within while, do or for loops.
When a break statement is encountered inside a loop, the loop is immediately exited and
the program continues with the statement immediately following the loop. The break
will exit only a single loop when the loops are nested i.e., the loop containing it.
A goto statement transfer the control to any place in a program. Another important use of
goto is to exit from the deeply nested loops when an error occurs. (A simple break
statement would not work here.)
Example: BREAK
Example: GOTO
The use of null (;) statement at the end due to the program should not end with a label.
Page 8 of 10
C Programming
STRUCTURED PROGRAMMING
It is an approach to design and develop good programs by making program’s logic easy
to understand by using the basic three control structures:
● Sequence (Straight-line) Structure: sufficient to meet all the requirements of
programming.
● Selection (Branching) Structure: provides to be more convenient in some
situations.
● Repetition (Looping) Structure: sufficient to meet all the requirements of
programming.
The use of structured programming techniques helps ensure well-designed program that
are easier to write, read, debug and maintain compared to those that are structured.
“SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION”.
Page 9 of 10
C Programming
Avoiding GOTO:
It is a good practice to avoid using goto’s. When it is used, many compilers generate a
less efficient code, makes a program logic complicated and renders the program
readability. In case it is absolutely necessary, it should be documented.
Points o be Remember
Page 10 of 10