ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Repetitive Statements and Execution Flow
    Programming FundamentalsTopic 10 of 19

    Repetitive Statements and Execution Flow

    4 minread
    711words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count711
      Code examples0
      DifficultyBeginner