Control or Iterative Structures
Control or Iterative Structures
CONTROL/ITERATIVE STRUCTURES:
Loops in programming are used to repeat a block of code un l the specified condi on is met. A loop
statement allows programmers to execute a statement or group of statements mul ple mes
without repe on of code.
1. Entry Controlled loops: In Entry controlled loops the test condi on is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condi on is evaluated at the end of
the loop body. The loop body will execute at least once, irrespec ve of whether the
condi on is true or false. do-while Loop is Exit Controlled loop.
first Ini alizes, then condi on check, then executes the body and at last, the update is
for loop
done.
first Ini alizes, then condi on checks, and then executes the body, and upda ng can
while loop
be inside the body.
do-while
do-while first executes the body and then the condi on check is done.
loop
for Loop
for loop in C programming is a repe on control structure that allows programmers to write a loop
that will be executed a specific number of mes. for loop enables programmers to perform n number
of steps together in a single line.
Syntax:
RJ-SIES-2024
2|Page
//
// body of for loop
//
}
Example:
In for loop, a loop variable is used to control the loop. Firstly we ini alize the loop variable with some
value, then check its test condi on. If the statement is true then control will move to the body and
the body of for loop will be executed. Steps will be repeated ll the exit condi on becomes true. If
the test condi on will be false then it will stop.
Ini aliza on Expression: In this expression, we assign a loop variable or loop counter to
some value. for example: int i=1;
Test Expression: In this expression, test condi ons are performed. If the condi on evaluates
to true then the loop body will be executed and then an update of the loop variable is done.
If the test expression becomes false then the control will exit from the loop. for example,
i<=9;
Update Expression: A er execu on of the loop body loop variable is updated by some value
it could be incremented, decremented, mul plied, or divided by any value.
While Loop
While loop does not depend upon the number of iterations. In for loop the number of
iterations was previously known to us but in the While loop, the execution is
terminated on the basis of the test condition. If the test condition will become false
then it will break from the while loop else body will be executed.
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
Flow Diagram for while loop:
RJ-SIES-2024
3|Page
/ C program to illustrate
// while loop
#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
do-while Loop
The do-while loop is similar to a while loop but the only difference lies in the do-while loop
test condition which is tested at the end of the body. In the do-while loop, the loop body will
execute at least once irrespective of the test condition.
The do-while loop is used to execute a set of statements repeatedly as long as a certain
condition is true. The difference between the while loop and the do-while loop is that the do-
while loop executes the code inside the loop at least once before checking the condition. The
code inside the loop is executed first, and then the condition is checked. If the condition is
true, the loop continues, otherwise, it terminates.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
RJ-SIES-2024
4|Page
// C program to illustrate
// do-while loop
#include <stdio.h>
// Driver code
int main()
int i = 2;
do
// loop body
// Update expression
i++;
// Test expression
RJ-SIES-2024
5|Page
return 0;
Above program will evaluate (i<1) as false since i = 2. But s ll, as it is a do-while loop the body will be
executed once.
Here is an example of a do-while loop that prints the numbers from 1 to 10:
1. #include <stdio.h>
2. int main() {
3. int i = 1;
4. do {
6. i++;
8. return 0;
9. }
Loop control statements in C programming are used to change execu on from its normal sequence.
Name Descrip on
break the break statement is used to terminate the switch and loop statement. It transfers
statement the execu on to the statement immediately following the loop or switch.
con nue con nue statement skips the remainder body and immediately resets its condi on
statement before reitera ng it.
goto
goto statement transfers the control to the labeled statement.
statement
Infinite Loop
An infinite loop is executed when the test expression never becomes false and the body of the loop is
executed repeatedly. A program is stuck in an Infinite loop when the condi on is always true. Mostly
this is an error that can be resolved by using Loop Control statements.
RJ-SIES-2024
6|Page
#include <stdio.h>
// Driver code
int main ()
int i;
// is blank
for ( ; ; )
return 0;
// C program to demonstrate
// loop
#include <stdio.h>
// Driver code
int main()
while (1)
return 0;
RJ-SIES-2024
7|Page
// C program to demonstrate
// loop
#include <stdio.h>
// Driver code
int main()
do
} while (1);
return 0;
Conclusion
Itera on statements are an essen al part of any programming language, and C provides three types
of itera on statements: for, while, and do-while. The for loop is used to execute a set of statements
repeatedly for a fixed number of mes. The while loop is used to execute a set of statements
repeatedly if a certain condi on is true, and the do-while loop is used to execute a set of statements
repeatedly if a certain condi on is true, but the code inside the loop is executed at least once.
When choosing which type of loop to use, it is important to consider the specific requirements of the
program. The for loop is o en used when the number of itera ons is known beforehand, whereas
the while and do-while loops are useful when the number of itera ons is not known or when the
loop needs to be executed at least once.
In addi on, it is essen al to ensure that the loop's condi on eventually becomes false, or else the
loop will con nue indefinitely, resul ng in an infinite loop, which can cause the program to crash or
become unresponsive. To avoid infinite loops, it is essen al to include a mechanism to ensure that
the loop terminates, such as upda ng the loop variable or breaking out of the loop when a certain
condi on is met.
In summary, itera on statements are an integral part of C programming, and for, while, and do-
while loops provide different ways to execute a set of statements repeatedly un l a certain condi on
is met. It is important to choose the appropriate loop for the specific requirements of the program
and ensure that the loop's condi on eventually becomes false to avoid infinite loops.
RJ-SIES-2024