Mada za sehemu hiiImplementing control structures in programmingMada 3
- Implementing condition structures
- Implementing loop structures
- Implementing nested structures
Implementing Loop Structures
Iteration in programming means repeating a block of code multiple times. This is often called a loop. Loop structures allow you to execute the same block of code several times without writing it over and over again. For example, if you need to print numbers from 1 to 100, instead of writing the print command 100 times, you can use a loop to do it in just a few lines.
Why Use Loop Structures?
- Reduces code length – When a program needs to repeat actions many times, loops let you write fewer lines.
- Solves complex problems – Loops help handle large amounts of data or repeat operations for many items.
- Saves time – Instead of repeating code manually, the computer does it automatically.

There are three main types of loops in C++:
- Do...While Loop
- For Loop
- While Loop
The do...while loop executes the code block at least once, then checks the condition. If the condition is true, it repeats the loop.
Syntax
do
{
// executable statements
// increment or decrement
} while (condition);
Worked Example
Program to print integers from 1 to 6:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << ",";
i++;
} while (i <= 6);
return 0;
}
Output:
1,2,3,4,5,6,
Another Example
Program to compute the sum of integers from 1 to 9:
#include <iostream>
using namespace std;
int main() {
int i = 1;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 9);
cout << sum << endl;
return 0;
}
Output:
45
The for loop is used when you know in advance how many times to repeat the code. It has three parts: initialization, condition, and increment/decrement.
Syntax
for (initialization; condition; increment)
{
// executable statements
}
Worked Example
Program to print numbers from 0 to 4:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << i << ",";
}
return 0;
}
Output:
0,1,2,3,4,
Another Example
Program to compute factorial of a number entered by the user:
#include <iostream>
using namespace std;
int main() {
int x, num, factorial = 1;
cout << "Type positive number: ";
cin >> num;
for (x = 1; x <= num; ++x) {
factorial = factorial * x;
}
cout << "Factorial of " << num << " = " << factorial;
return 0;
}
Output:
Type positive number: 4
Factorial of 4 = 24
The while loop checks the condition first. If it is true, it executes the code block. If the condition is false initially, the loop body may not run at all.
Syntax
while (condition)
{
// executable statements
// increment or decrement
}
Worked Example
Program to print numbers from 1 to 5:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}
Output:
1 2 3 4 5
Another Example
Program to find the sum of positive numbers. If the user enters a negative number, the loop ends:
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
cout << "Enter a number: ";
cin >> number;
while (number >= 0) {
sum += number;
cout << "Enter a number: ";
cin >> number;
}
cout << "The sum is " << sum << endl;
return 0;
}
Sample Input/Output:
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: -4
The sum is 12
- Use for loop when you know exactly how many times to repeat.
- Use while loop when you are not sure how many times, but you check the condition first.
- Use do...while loop when you must execute the code at least once before checking the condition.
In Tanzania, shop owners can use loop structures in their point-of-sale software to calculate the total sales for each day of the week. For example, a duka in Arusha can program the system to prompt the shopkeeper to enter sales for Monday through Friday, and the loop automatically adds up all the amounts and displays the total weekly sales in Tanzanian shillings, saving time and reducing calculation errors.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Write syntactically correct loop structures.
- Identify and apply the appropriate type of loop for specific problems.
- Use break and continue statements effectively to control loop execution.
Tathmini ya matokeo
Programs that execute loops correctly with the desired looping behaviour are created.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Use different types of loops (for, while, do-while) effectively.
- Implement nested loops for solving complex problems.
- Debug looprelated issues such as infinite loops or incorrect iteration.
Principles: The student should explain the principle of:
- Iteration in programming for repetitive tasks.
- Efficiency in designing loops to minimize unnecessary iterations.
- Logical structure of loop conditions and initialization.
Theories: The trainee should explain: The trainee should explain:
- The role of loops in program automation and efficiency.
- Differences between pre-test (while, for) and post-test (do-while) loops.
Detailed knowledge about:
- Common errors in loop implementation and debugging strategies.
- Practical use cases of loops in data processing, gaming, and algorithm design.
Swali
What is a key characteristic of a do-while loop in C++?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado