ITE 112 Module 7
ITE 112 Module 7
Module No. 7
LOOPS
I. Topic:
1. while loop
2. for loop
3. do-while loop
4. nested loops
III. Introduction:
The versatility of the computer lies in its ability to perform as set of instructions repeatedly.
This involves repeating some portion of the program either a specified number of times or until
a particular condition is being satisfied. This repetitive operation is done through a loop control
instruction.
IV. Objectives:
At the end of the lessons the student can:
V. Pretest
Module 7 Pretest
2. Construct program that will display numbers from 1 to 9 in vertical direction using
while loop.
main() {
int i=0;
for(i=1;i<=5;print(“\n%d”,i));
i++;
}
int y, x=0;
do
{
y=x;
} while (x==0);
DECISION MAKING
A loop statement allows us to execute a statement or group of statements multiple times. Given
below is the general form of a loop statement in most of the programming languages.
Flow Diagram
C programming language provides the following types of loops to handle looping requirements.
while Loop
Syntax
while(condition) {
statement(s);
}
Flow Diagram
Here, the key point to note is that a while loop might not execute at all. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.
Example
#include <stdio.h>
int main () {
return 0;
}
Ouput
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15s
value of a: 16
value of a: 17
value of a: 18
value of a: 19
for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.
Syntax
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.
Flow Diagram
Example
#include <stdio.h>
int main () {
int a;
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do-while Loop
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at
least one time.
Syntax
do {
statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in
the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the
loop executes again. This process repeats until the given condition becomes false.
Flow Diagram
Example
#include <stdio.h>
int main () {
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Nested Loops
C programming allows to use one loop inside another loop. The following section shows a
few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement.
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
do {
statement(s);
}while( condition );
}while( condition );
Final note on loop nesting is that you can put any type of loop inside any other type of loop.
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100
#include <stdio.h>
int main () {
return 0;
}
Output
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
while loop tests the condition before executing any of the statements within the loop.
for loop allows programmer to specify three things about a loop in a single line.
do-while loop tests the condition after having executed the statements within the loop
has been exuted
2. Construct program that will display numbers from 1 to 9 in vertical direction using
while loop.
main() {
int i=0;
for(i=1;i<=5;print(“\n%d”,i));
i++;
}
int y, x=0;
do
{
y=x;
} while (x==0);
IX: References
https://www.tutorialspoint.com/cprogramming/c_loops.htm