Lesson 4
Conditional Looping, Break, and Continue in C#
Topic Overview

Welcome! Today, we're exploring C#'s special instructions: Conditional Looping, along with the break and continue statements. As we know, loops enable code to execute multiple times. Conditional looping, enhanced with break and continue, bolsters loop control, leading to flexible, efficient code. Grab your explorer hat, and let's get started!

The 'if' Statement

C#'s if statement sets condition-based actions for our code. Consider this simple example where the if statement decides which message to print based on the value of temperature:

C#
1int temperature = 15; 2if (temperature > 20) 3{ 4 Console.WriteLine("Wear light clothes."); // This will print if the temperature is over 20. 5} 6else 7{ 8 Console.WriteLine("Bring a jacket."); // This will print otherwise. 9}

We can also evaluate multiple conditions using else if. In other words, "If the previous condition isn't true, then check this one":

C#
1if (temperature > 30) 2{ 3 Console.WriteLine("It's hot outside!"); // Prints if the temperature is over 30. 4} 5else if (temperature > 20) 6{ 7 Console.WriteLine("The weather is nice."); // Prints if the temperature is between 21 and 30. 8} 9else 10{ 11 Console.WriteLine("It might be cold outside."); // Prints if the temperature is 20 or below. 12}

Note that if an else if condition evaluates to true, the subsequent blocks are not checked. The first true condition's block executes, and the entire chain is bypassed.

The 'break' Statement

We use the break statement whenever we want to exit a loop prematurely once a condition is met:

C#
1int[] numbers = {1, 3, 7, 9, 12, 15}; 2 3for (int i = 0; i < numbers.Length; i++) 4{ 5 if (numbers[i] % 2 == 0) 6 { 7 Console.WriteLine("The first even number is: " + numbers[i]); // Prints the first even number. 8 break; // Stops the loop after printing the first even number. 9 } 10 Console.WriteLine("Number: " + numbers[i]); 11} 12// Prints: 13// Number: 1 14// Number: 3 15// Number: 7 16// Number: 9 17// The first even number is: 12
The 'continue' Statement

The continue statement bypasses the rest of the loop's code for the current iteration only:

C#
1for (int i = 0; i < 6; i++) 2{ 3 if (i == 3) 4 { 5 continue; // Skips the print command for '3'. 6 } 7 Console.WriteLine(i); // Prints the numbers 0 to 5 except 3. 8} 9// Prints: 10// 0 11// 1 12// 2 13// 4 14// 5
Use-case with a For Loop

By combining the tools we've learned so far, we can write more flexible loops. Here's a snippet where we conclude the loop once we find "Charlie":

C#
1string[] names = {"Alice", "Bob", "Charlie", "David"}; 2 3for (int i = 0; i < names.Length; i++) 4{ 5 if (names[i] == "Charlie") 6 { 7 Console.WriteLine("Found Charlie!"); // Prints when 'Charlie' is found. 8 break; // Stops the loop after finding Charlie. 9 } 10}
Lesson Summary and Practice

Well done! You are now more familiar with C#'s if statement, break and continue statements, and their applications with loops. It's time to refine your learning with the upcoming practice exercises. Happy coding!

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