ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Repetitive Statements …
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    4 min read
    Topic 10 of 19
    Topic Study Note Illustration
    10

    Repetitive Statements and Execution Flow

    Repetitive statements, or loops, are constructs that allow you to execute a block of code multiple times. They are essential for performing repetitive tasks efficiently. In C++,...

    Repetitive Statements and Execution Flow in C++

    Repetitive statements, or loops, are constructs that allow you to execute a block of code multiple times. They are essential for performing repetitive tasks efficiently. In C++, there are several types of loops: for, while, and do-while. Each type serves specific use cases and has its own syntax.

    1. The for Loop

    The for loop is used when the number of iterations is known beforehand. It consists of three main components: initialization, condition, and increment/decrement.

    Syntax:

    for (initialization; condition; increment) {
        // Code to execute on each iteration
    }
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            cout << "Iteration " << i << endl;
        }
        return 0;
    }
    

    Flow:

    1. Initialization (int i = 1).
    2. Condition check (i <= 5).
    3. Execute loop body if true.
    4. Increment (i++).
    5. Repeat until the condition is false.

    2. The while Loop

    The while loop is used when the number of iterations is not known in advance and continues until a specified condition becomes false.

    Syntax:

    while (condition) {
        // Code to execute as long as condition is true
    }
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int i = 1;
        while (i <= 5) {
            cout << "Iteration " << i << endl;
            i++;
        }
        return 0;
    }
    

    Flow:

    1. Check condition (i <= 5).
    2. Execute loop body if true.
    3. Increment (i++).
    4. Repeat until the condition is false.

    3. The do-while Loop

    The do-while loop is similar to the while loop but guarantees that the loop body is executed at least once, as the condition is checked after the loop body.

    Syntax:

    do {
        // Code to execute
    } while (condition);
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int i = 1;
        do {
            cout << "Iteration " << i << endl;
            i++;
        } while (i <= 5);
        return 0;
    }
    

    Flow:

    1. Execute loop body.
    2. Check condition (i <= 5).
    3. Repeat until the condition is false.

    4. Nested Loops

    Loops can be nested, meaning you can place one loop inside another. This is useful for working with multi-dimensional data structures.

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 2; j++) {
                cout << "i: " << i << ", j: " << j << endl;
            }
        }
        return 0;
    }
    

    Flow:

    • The outer loop runs for i from 1 to 3.
    • For each iteration of i, the inner loop runs for j from 1 to 2.

    5. Control Statements within Loops

    Control statements like break and continue can alter the flow of loops:

    • break: Exits the loop immediately.
    • continue: Skips the rest of the current iteration and proceeds to the next iteration.

    Example of break:

    #include <iostream>
    using namespace std;
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                break; // Exit the loop when i is 3
            }
            cout << "Iteration " << i << endl;
        }
        return 0;
    }
    

    Example of continue:

    #include <iostream>
    using namespace std;
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue; // Skip the iteration when i is 3
            }
            cout << "Iteration " << i << endl;
        }
        return 0;
    }
    

    Summary

    Repetitive statements (loops) allow for efficient execution of code blocks multiple times, which is crucial for tasks requiring iteration. Understanding the for, while, and do-while loops, along with control statements like break and continue, enables you to create flexible and powerful programs that can handle repetitive tasks effectively. By mastering these constructs, you can control the execution flow of your programs, making them more dynamic and responsive to user inputs and conditions.

    ← Previous topic 9
    Conditional Statements and Execution Flow
    Next topic 11 →
    Lists and Memory Organization

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    711words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals