Lesson 2
Delving into C++ Do-While Loops
Introduction

Welcome! In today's lesson, we are focusing on the do-while loop in C++. Unique among loops, the do-while loop runs its code block once before checking the condition. This exclusive feature can be incredibly useful in certain code constructs. Through this lesson, you will understand the syntax, operations, and practical applications of the do-while loop.

Understanding the Do-While Loop Syntax

The do-while loop has a straightforward syntax as follows:

C++
1do { 2 // code block to be executed 3} while (condition);

Here, do initiates the loop. Following that, there lies a code block enclosed within {}. Subsequently, we have while with the condition in (). Remember, the condition is checked only after the loop's code block has been executed at least once. Also, don't forget the semicolon ; at the end.

Here's a simple example:

C++
1#include <iostream> 2 3int main() { 4 int counter = 0; 5 do { 6 counter++; 7 std::cout << "This is loop iteration number: " << counter << std::endl; // This line will be printed three times 8 } while (counter < 3); 9 10 return 0; 11}
Do-While Loop Executing at Least Once

An important characteristic of the do-while loop is that it always executes the code block at least once, regardless of the condition. Even if the condition is initially false, the loop will still run the body one time before evaluating the condition.

Here's an example demonstrating this behavior where the condition is false from the beginning:

C++
1#include <iostream> 2 3int main() { 4 int start = 10; 5 do { 6 std::cout << "This will print at least once, even if the loop condition is false." << std::endl; 7 } while (start < 5); // Condition is false from the beginning 8 9 return 0; 10} 11// Output: This will print at least once, even if the loop condition is false.

In this example, the message inside the loop will be printed exactly once because the condition (start < 5) is false from the beginning. Despite the false condition, the code block within the do statement executes before the condition is evaluated.

Real-life Examples of Do-While Loop

To demonstrate the do-while loop in a real-life example, let's use a number guessing game:

C++
1#include <iostream> 2 3int main() { 4 int number = 5; 5 int guess; 6 do { 7 std::cout << "Guess the number (between 1 and 10): "; // Ask the user for input 8 std::cin >> guess; // Get user input 9 } while (guess != number); 10 11 std::cout << "Congrats! You guessed it right."; // Congratulate the user on guessing the number correctly 12 13 return 0; 14}

In this scenario, the user is asked to input their guess until they guess the correct number.

Loop Control Variables in Do-While Loop

In loop control, updating variables is crucial to prevent an infinite loop. If not updated, an infinite loop may occur. Here’s an instructive code snippet illustrating how this problem could arise:

C++
1#include <iostream> 2 3int main() { 4 int number = 5; 5 int guess = 0; 6 do { 7 std::cout << "Guess the number (between 1 and 10): "; // missing cin command, which will result in an infinite loop 8 } while (guess != number); 9 10 std::cout << "Congrats! You guessed it right."; // This line will never be printed due to an infinite loop 11 12 return 0; 13}
Catching Common Mistakes

When working with do-while loops, it is common to forget to place a semicolon at the end. Unlike other loops, a do-while loop always ends with a semicolon:

C++
1do { 2 // code block 3} while (condition); // note the trailing semicolon here
Lesson Summary and Practice

Great work! You've learned about the syntax, operation, and real-world applications of the do-while loop. Now, you’re ready to practice do-while loops in the upcoming exercises. Practicing is essential for mastering C++ loop structures. So, let's move on to your practice exercises!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.