Module 5: Looping Structures
Develop repetitive processing using for loops, while loops, nested loops, break, continue, pass, range functions and loop-based applications in C, C++, Java and Python.
Tasks
Flow
Lists
Apps
5.1 Module Overview
Looping structures allow a program to repeat a task multiple times without rewriting the same code. Loops are used for counting, processing records, validating input, calculating totals, printing patterns and automating repetitive operations.
This module explains looping structures in C, C++, Java and Python. The logic is similar across all four languages, but the syntax differs.
5.2 Syntax Comparison
| Concept | C / C++ / Java | Python |
|---|---|---|
| for loop | for (int i=1; i<=5; i++) { } | for i in range(1,6): |
| while loop | while (condition) { } | while condition: |
| break | break; | break |
| continue | continue; | continue |
| pass | Not available as a direct keyword | pass |
5.3 for Loop
A for loop is used when the number of repetitions is known. For example, printing numbers from 1 to 5.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << i << endl;
}
return 0;
}public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}for i in range(1, 6):
print(i)Online Working Example
5.4 while Loop
A while loop repeats as long as a condition is true. It is useful when the number of repetitions may depend on a condition.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << endl;
i++;
}
return 0;
}public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}i = 1
while i <= 5:
print(i)
i += 1Online Working Example
5.5 Nested Loops
A nested loop is a loop inside another loop. It is commonly used for tables, grids, patterns and matrix-style processing.
#include <stdio.h>
int main() {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
printf("* ");
}
printf("\n");
}
return 0;
}#include <iostream>
using namespace std;
int main() {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
cout << "* ";
}
cout << endl;
}
return 0;
}public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
System.out.print("* ");
}
System.out.println();
}
}
}for row in range(1, 4):
for col in range(1, 4):
print("*", end=" ")
print()Online Working Example
5.6 break, continue and pass
break stops a loop completely. continue skips the current iteration and moves to the next iteration. pass is used in Python as an empty placeholder statement.
| Keyword | Meaning | Availability |
|---|---|---|
| break | Exit the loop immediately | C, C++, Java, Python |
| continue | Skip current loop cycle | C, C++, Java, Python |
| pass | Do nothing; placeholder | Python |
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
if (i == 5) break;
printf("%d\n", i);
}for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
if (i == 5) break;
cout << i << endl;
}for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
if (i == 5) break;
System.out.println(i);
}for i in range(1, 6):
if i == 3:
continue
if i == 5:
break
print(i)
# pass example
if True:
passOnline Working Example
5.7 Range Function
Python uses the range() function to generate a sequence of numbers. C, C++ and Java do not use range(); they use loop initialization, condition and increment.
| Python range() | Meaning | Output |
|---|---|---|
range(5) | Start from 0, stop before 5 | 0 1 2 3 4 |
range(1, 6) | Start from 1, stop before 6 | 1 2 3 4 5 |
range(2, 11, 2) | Start 2, stop before 11, step 2 | 2 4 6 8 10 |
Online Working Example
5.8 Loop Applications
Loops are used in applications such as total calculation, average calculation, search operations, pattern printing, menu systems and repeated input validation.
Application: Sum of Numbers
5.9 Practical Activities
Activity 1: Number Printing
Print numbers from 1 to 20 using for loops in all four languages.
Activity 2: Countdown
Use while loops to count down from 10 to 1.
Activity 3: Multiplication Table
Create a multiplication table program for any number.
Activity 4: Star Pattern
Use nested loops to print a rectangle pattern.
Mini Project
Create a marks processing program that asks for several marks, calculates total, average and highest mark using loops.
5.10 Interactive Final Assessment Quiz
Each correct answer gives +1 mark. Each wrong answer gives -0.5 mark. Correct answers are intentionally mixed.
1. Which loop is usually best when the number of repetitions is known?
2. A while loop repeats while its condition is true.
3. Which keyword stops a loop immediately?
4. Nested loop means a loop inside another loop.
5. Which keyword skips the current iteration and continues with the next one?
6. Python range(1, 6) generates numbers 1 to 5.
7. Which keyword is mainly a Python placeholder that does nothing?
8. Loops cannot be used to calculate totals.
9. In C, C++ and Java, a common for loop includes initialization, condition and update.
10. Which Python code prints 0, 1, 2?
Your Score: 0
5.11 Module Summary
This module explained for loops, while loops, nested loops, break, continue, pass, Python range function and loop applications using C, C++, Java and Python.