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.

for Loopwhile LoopNested Loopsbreak / continueRange
Repeat
Tasks
Control
Flow
Process
Lists
Build
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.

Learning Outcome: Develop repetitive processing using loops in C, C++, Java and Python.

5.2 Syntax Comparison

ConceptC / C++ / JavaPython
for loopfor (int i=1; i<=5; i++) { }for i in range(1,6):
while loopwhile (condition) { }while condition:
breakbreak;break
continuecontinue;continue
passNot available as a direct keywordpass

5.3 for Loop

A for loop is used when the number of repetitions is known. For example, printing numbers from 1 to 5.

C
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}
C++
#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 5; i++) {
        cout << i << endl;
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}
Python
for i in range(1, 6):
    print(i)

Online Working Example

Select language and click Run.

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.

C
#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
C++
#include <iostream>
using namespace std;
int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << endl;
        i++;
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println(i);
            i++;
        }
    }
}
Python
i = 1
while i <= 5:
    print(i)
    i += 1

Online Working Example

Select language and click Run.

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.

C
#include <stdio.h>
int main() {
    for (int row = 1; row <= 3; row++) {
        for (int col = 1; col <= 3; col++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C++
#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;
}
Java
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();
        }
    }
}
Python
for row in range(1, 4):
    for col in range(1, 4):
        print("*", end=" ")
    print()

Online Working Example

Select language and click Run.

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.

KeywordMeaningAvailability
breakExit the loop immediatelyC, C++, Java, Python
continueSkip current loop cycleC, C++, Java, Python
passDo nothing; placeholderPython
C
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    if (i == 5) break;
    printf("%d\n", i);
}
C++
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    if (i == 5) break;
    cout << i << endl;
}
Java
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    if (i == 5) break;
    System.out.println(i);
}
Python
for i in range(1, 6):
    if i == 3:
        continue
    if i == 5:
        break
    print(i)

# pass example
if True:
    pass

Online Working Example

Select language and click Run.

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()MeaningOutput
range(5)Start from 0, stop before 50 1 2 3 4
range(1, 6)Start from 1, stop before 61 2 3 4 5
range(2, 11, 2)Start 2, stop before 11, step 22 4 6 8 10

Online Working Example

Select language and click Run.

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

Select language and click Run.

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.

Remember: Loops help programs automate repetitive processing and handle large amounts of repeated work efficiently.