Welcome! Today, we're exploring special instructions in the C++ language: Conditional Statements, along with the break
and continue
statements. As we've learned, loops allow us to execute a block of code numerous times. By combining these loops with conditional statements and incorporating the useful break
and continue
instructions, we achieve more robust and efficient code. Let's dive in!
In C++, the if
statement triggers actions in our code based on a specific condition. Consider this straightforward example where the if
statement determines which message to print based on the value of temperature
:
C++1#include <iostream> 2 3int main() { 4 int temperature = 15; 5 if (temperature > 20) { 6 std::cout << "Wear light clothes."; // This message will print if the temperature is over 20. 7 } else { 8 std::cout << "Bring a jacket."; // This message will print otherwise. 9 } 10 return 0; 11}
We can evaluate multiple conditions using else if
. This phrase means, "If the previous condition isn't true, then check this one":
C++1#include <iostream> 2 3int main() { 4 int temperature = 15; 5 6 if (temperature > 30) { 7 std::cout << "It's hot outside!"; // This will print if the temperature is over 30. 8 } else if (temperature > 20) { 9 std::cout << "The weather is nice."; // This will print if the temperature is between 21 and 30. 10 } else { 11 std::cout << "It might be cold outside."; // This will print if the temperature is 20 or below. 12 } 13 return 0; 14}
We use the break
statement whenever we want to exit a loop prematurely once a condition is met:
C++1#include <iostream> 2 3int main() { 4 int numbers[] = {1, 3, 7, 9, 12, 15}; 5 6 for(int i = 0; i < 6; i++) 7 { 8 if(numbers[i] % 2 == 0){ 9 std::cout << "The first even number is: " << numbers[i] << std::endl; // This prints the first even number. 10 break; // This stops the loop after printing the first even number. 11 } 12 std::cout << "Number: " << numbers[i]; 13 } 14 return 0; 15}
The continue
statement bypasses the rest of the loop code for the current iteration only:
C++1#include <iostream> 2 3int main() { 4 for(int i = 0; i < 6; i++){ 5 if(i == 3){ 6 continue; // This skips the print command for '3'. 7 } 8 std::cout << i << std::endl; // This prints the numbers from 0 to 5 except 3. 9 } 10 return 0; 11}
By utilizing the tools we've covered so far, we can craft more flexible loops. Here's a snippet where we terminate the loop once we find "Charlie":
C++1#include <iostream> 2 3int main() { 4 std::string names[] = {"Alice", "Bob", "Charlie", "David"}; 5 6 for(int i = 0; i < 4; i++) { 7 if(names[i] == "Charlie"){ 8 std::cout << "Found Charlie!"; // This prints when 'Charlie' is found. 9 break; // This stops the loop after finding 'Charlie'. 10 } 11 } 12 13 return 0; 14}
Congratulations! You are now familiar with C++'s if
statement, as well as the break
and continue
statements and their applications in loops. We encourage you to reinforce your learning through the upcoming practice exercises. Happy coding!