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 forstd::prefixes.Variable Initialization:
int s = 0, i = 0;initializessto hold the sum andias a loop counter.Do-While Loop: Executes at least once and continues while
iis less than 5.- Inside the Loop:
s += i;addsitosandi++incrementsi.
- Inside the Loop:
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
numberandresult.resultis used to build the reversed number.Do-While Loop: Continues until
numberbecomes 0.- Inside the Loop: Updates
resultby shifting digits and adding the last digit ofnumber.
- Inside the Loop: Updates
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, andffor tracking switch cases.While Loop: Iterates while
iis less than or equal to 5.Switch Statement: Evaluates
iand 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
counteranddigit.While Loop: Increments
counteranddigituntildigitexceeds 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
sumandi.Do-While Loop: Doubles
sumeach iteration while decrementingi.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
digitafter 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
digitby 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.


