0% found this document useful (0 votes)
18 views

Iterative Control Structure

The document discusses iteration and loop constructs in C programming. It covers while, do-while and for loops as well as nested loops. Examples and exercises are provided to illustrate each concept.

Uploaded by

gacc4sites
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Iterative Control Structure

The document discusses iteration and loop constructs in C programming. It covers while, do-while and for loops as well as nested loops. Examples and exercises are provided to illustrate each concept.

Uploaded by

gacc4sites
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Programming 2

C Iterative Control Structure


At the end of the lesson, the students should be able to:
1. illustrate the repetition control structures,
2. evaluate and apply the different repetition control structures in a given problem,
3. illustrate the use of break and continue in control structures, and
4. apply the different iteration statements in a complex level of iteration.

Iteration/Loop
Loop is a sequence of instructions that is continually repeated until a certain condition is reached. It is used to
repeat execution of a block of code.

Execution of Loops
Execution of a loop is done inside a loop statement using a variable as a counter to keep track of which character is to be
checked. The counter variable may be incremented by one, each time through, so that the first element is examined the
first time through the loop, the second element the second time, and so forth.
Flowchart of a Loop

A loop has the following components:


1. Initialization of variables – setting an initial value of zero before the loop statement is reached.
2. Condition/testing – (that would evaluate to either true or false) the condition to check is
usually made on the current value of the variable initialized in (1) for it controls the loop
repetition.
3. Body – statements that are repeated in the loop.
4. Updating – a statement inside the body of the loop that updates the value of the variable that is
initialized during each repetition.

Types of Loops in C
1. Entry controlled loop
In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-
checking loop.
2. Exit controlled loop
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking
loop.

C Loop Constructs
The while loop
- The while loop is an entry control loop in C.
- In a while loop, a condition is evaluated before processing the body of the loop. If a
condition is true then and only then the body of a loop is executed.

Flowchart

Syntax:
while (condition) {
statement 1
statement 2
statement 3
...
statement n
}
Sample Problem 1: Implement using while
Make a C program that will print numbers from 1 to 10.

Output:

Sample Problem 2: Implement using while


Make a C program that will ask the user to enter 5 integers. Display the sum.
Output:

Practice 1:
Write C program that will ask the user to enter 10 integers and display the sum of all odd
integers.
Sample Outputs:
Test Case 1: Test Case 2:
The do-while loop
In a do…while loop, the condition is always executed after the body of a loop. It is also called
an exit-controlled loop.
Syntax:
do
{
statement/s;
} while(loop repetition condition);

Sample Problem 3: Implement using do-while


Make a C program that will ask the user to enter 5 integers. Display the sum.

Output:

Sample Problem 4: Implement using do while


Make a C program to input 10 integers and will output the sum and average of all positive
integers.

Output:

Practice 2:
Make a C program to input an integer. Output all the factors of the number excluding itself.
Sample outputs:
Test Case 1:
Test Case 3:

Test Case 2:
Flowcharts of while and do while loops

The for loop


- In a for loop, the initial value is performed only once, then the condition tests and compares
the counter to a fixed value after each iteration, stopping the for loop when false is returned.
Syntax:
for (initialization expression; loop repetition condition; update expression)
{
statement/s;
}

Sample Program 5:
Make a C program that will print numbers from 1 to 10.
while loop implementation

for loop implementation

Output:
Practice 3:
Write C program that will ask the user to enter 10 integers and display the sum of all odd
integers.
Sample Outputs:

Test Case 1: Test Case 2:

Nested Loops in C
Syntax for nested for loop:
for (init; condition; increment) {
for (init; condition; increment) {
statement/s;
} //inner for loop
statement/s;
} //outer for loop

Syntax for nested while loop:


while (condition) {
while (condition) {
statement/s;
} //inner while loop
statement/s;
} //outer while loop
Syntax for nested do - while loop:
do {
statement/s;
do {
statement/s;
} while (condition); //inner do-while loop
} while (condition); //outer do-while loop
Note:
You can combine different loops for your nested loop.

Sample Problem 5: Implement using nested loop


Make a C program to output the following figure:
*
**
***
****
*****
Output:
Practice 4: Assignment 2:
Make a C program to output the following Make a C program that will display the cube
figure: of a number using a while loop.
*****
****
***
**
*

Outputs:
Note: You may use any loop you want.
Assignment 1: Assignment 3:
Make a C program that will display the Make a C program that will ask for the
factorial of a number. Note: Use a for loop. length and width of a rectangle and display
Definition: the perimeter. Ask the user to try again, if
n! (read as n factorial) the answer yes, enter the length and width. If
n! = n (n – 1)(n – 2) … (2) (1) the answer is no, the program terminates.
Example: Hint: use a do-while loop.
5! = 5(4)(3)(2)(1) = 120 Output:
0! = 1
-7! = invalid
Sample Outputs:

Assignment 4:
Make a C program to output the following
figure:
1
12
123
1234
12345
Hint: Use nested loop.

You might also like