Welcome to our final class on C++ loops and conditional statements. This lesson reinforces the concepts of loops (while
, do-while
, for
) and control statements (if
, if-else
). We will also introduce some advanced topics and illustrate their collective use in problem-solving. Let's proceed, connecting these concepts with practical references for a better understanding.
To start, let's review each type of loop and conditional statement. A while
loop iterates until its condition becomes false. A do-while
loop ensures at least one execution before evaluating the condition. The for
loop is beneficial when we know the exact number of iterations. For conditional statements, if
tests a condition and executes statements if the condition is true
. The if-else
statement enables one block for the true
condition and another for the false
condition. A switch
tests for multiple conditions. These constructs are essential for managing our program's flow.
We often require the combination of loops and conditional statements for complex scenarios. For example, consider a situation where we calculate the sum of the first n
natural numbers, but only even numbers are taken into account. Here's how you may do it:
C++1#include <iostream> 2 3int main() { 4 int n = 10; 5 int sum = 0; 6 for (int i = 0; i <= n; ++i) { 7 if (i % 2 == 0) { 8 sum += i; 9 } 10 } 11 std::cout << "Sum is " << sum << std::endl; // This outputs: Sum is 30 12 13 return 0; 14}
Here, the for
loop iterates from 0
to n
, checking if the number is even within the if
statement. The expression i % 2
evaluates to the remainder when i
is divided by 2
, commonly used to determine if i
is even (remainder 0) or odd (remainder 1).
The break
and continue
statements finely control loop executions in C++. break
exits the current loop abruptly, halting the ongoing execution. In contrast, continue
skips the remaining code in the current loop iteration, moving to the next iteration.
C++1#include <iostream> 2 3int main() { 4 for (int i = 0; i < 10; ++i) { 5 if (i > 5) { 6 break; // As soon as i > 5, the loop breaks 7 } 8 std::cout << i << " "; // Prints: 0 1 2 3 4 5 9 } 10 for (int i = 0; i < 10; ++i) { 11 if (i % 2 == 0) { 12 continue; // Skips the iteration when i is even 13 } 14 std::cout << i << " "; // Prints: 1 3 5 7 9 15 } 16 17 return 0; 18}
Efficient loops can improve the overall performance of your program. If you're performing the same calculation multiple times, consider moving that operation outside the loop to make the loop more efficient. Also, give meaningful names to your loop control variables, maintain proper spacing and indentation, and provide comments if necessary.
C++1#include <iostream> 2 3int main() { 4 int n = 10; 5 int multiplier = 5 * 5 * 5; 6 for (int i = 1; i <= n; ++i) { 7 std::cout << multiplier * i << " "; // Outputs: 125 250 375 ... 8 } 9 10 return 0; 11}
We covered loops and conditional statements in C++, combining them, and we learned about break
and continue
. We also highlighted some best practices for loop implementation. These foundations enable you to experiment with these concepts and solve advanced problems. Up next, there are practice exercises to reinforce your understanding. Happy coding!