Skip to main content

Command Palette

Search for a command to run...

Understanding C++ Loop Constructs and Output: A Comprehensive Guid

Published
4 min read
Understanding C++ Loop Constructs and Output: A Comprehensive Guid

Exploring C++ Loops: Examples and Explanations

C++ offers various loop constructs and output mechanisms to control the flow of programs and display results. In this blog, we'll explore several examples of C++ programs that utilize loops and analyze their outputs.


1. Sum of Integers Using a Do-While Loop

#include <iostream>
using namespace std;

int main() {
    int s = 0, i = 0; 

    do {
        s += i; 
        i++; 
    } while (i < 5); 

    cout << "s = " << s << endl; 
    return 0;
}

Output:

s = 10

Explanation:

  • Include Directive: #include <iostream> allows the use of input and output streams.

  • Namespace: using namespace std; simplifies code by avoiding the need for std:: prefixes.

  • Variable Initialization: int s = 0, i = 0; initializes s to hold the sum and i as a loop counter.

  • Do-While Loop: Executes at least once and continues while i is less than 5.

    • Inside the Loop: s += i; adds i to s and i++ increments i.
  • Output: Displays the sum of integers from 0 to 4, which is 10.


2. Reversing a Number with a Do-While Loop

#include <iostream> 
using namespace std;

int main() { 
    long number = 7866372, result = 0;

    do {
        result *= 10;              
        int digit = number % 10;   
        result += digit;          
        number /= 10;              
    } while (number != 0);        

    cout << "output = " << result << endl; 

    return 0;
}

Output:

output = 2736687

Explanation:

  • Variable Declarations: Initializes number and result. result is used to build the reversed number.

  • Do-While Loop: Continues until number becomes 0.

    • Inside the Loop: Updates result by shifting digits and adding the last digit of number.
  • Output: Displays the reversed number, 2736687.


3. Counting Switch Case Hits

#include <iostream> 

using namespace std;

int main() { 
    int i = 0, a = 0, b = 0, c = 0, f = 0;

    while (i <= 5) { 
        switch (i++) { 
            case 1:
            case 2: 
                ++a; 
                break;
            case 3:
            case 4: 
                ++b; 
                break;
            case 5: 
                ++c; 
                break;
            default: 
                ++f; 
                break;
        }
    }

    cout << a << "\n";
    cout << b << "\n"; 
    cout << c << "\n"; 
    cout << f << "\n"; 

    return 0; 
}

Output:

1
1
1
1

Explanation:

  • Variable Initialization: Initializes counters a, b, c, and f for tracking switch cases.

  • While Loop: Iterates while i is less than or equal to 5.

  • Switch Statement: Evaluates i and increments corresponding counters.

    • Case Hits: Counts how many times each case is executed.
  • Output: Displays counts for each case, showing results based on the values processed.


4. Counting with a While Loop

#include <iostream> 
using namespace std;

int main() { 
    int counter;
    int digit = 0;
    counter = 1;

    while (digit <= 10) { 
        ++counter; 
        ++digit;  
    }

    cout << counter; 
    return 0; 
}

Output:

12

Explanation:

  • Variable Declaration: Initializes counter and digit.

  • While Loop: Increments counter and digit until digit exceeds 10.

  • Output: Shows the final value of counter, which is 12 after the loop finishes.


5. Doubling Sum Using a Do-While Loop

#include <iostream> 
using namespace std;

int main() {
    int sum, i;
    sum = 1;
    i = 9;

    do {
        i = i - 1;
        sum = 2 * sum;
    } while (i > 0); 

    cout << sum;

    return 0; 
}

Output:

512

Explanation:

  • Variable Declaration: Initializes sum and i.

  • Do-While Loop: Doubles sum each iteration while decrementing i.

  • Output: Displays the final value of sum, which results from the doubling process.


6. Printing Numbers with a Do-While Loop

#include <iostream> 
using namespace std;

int main() {
    int digit = 0;

    do {
        cout << digit++ << "\n"; 
    } while (digit <= 10); 

    return 0;
}

Output:

0 
1
2
3
4
5
6
7
8
9
10

Explanation:

  • Variable Declaration: Initializes digit.

  • Do-While Loop: Prints numbers from 0 to 10, incrementing digit after each print.

  • Output: Displays numbers sequentially from 0 to 10.


7. For Loop with Post-Loop Arithmetic

#include <iostream> 
using namespace std;

int main() {
    int digit;

    for (digit = 0; digit <= 9; digit++) { 
        cout << digit << "\n"; 
    }

    digit = 5 * digit; 
    --digit;

    cout << "Final value of digit: " << digit << "\n"; 
    return 0; 
}

Output:

0
1
2
3
4
5
6
7
8
9
Final value of digit : 49

Explanation:

  • For Loop: Prints numbers from 0 to 9.

  • Post-Loop Operations: Multiplies digit by 5 and then decrements it.

  • Output: Displays the digits followed by the final value of digit, which is 49.


Conclusion

These examples illustrate how C++ loop constructs and variables interact to perform various tasks. By understanding these examples, you gain insight into loop mechanisms and their impact on program output, which is crucial for effective programming.