Master Control Flow in C#: Your Guide to Loops, Conditional Statements, and More
Control Flow Statements
Control flow statements in C# allow you to direct the execution of your program based on specific conditions or repetitive tasks. These statements are the backbone of decision-making and iterative processes in any application. This section covers the key types of control flow statements: conditional statements, switch statements, loops, and special keywords like break and continue. Let’s dive in!
Conditional Statements (if, else if, else)
Conditional statements enable your program to make decisions based on specific conditions. They evaluate a condition and execute a block of code only if the condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
} else if (anotherCondition) {
// Code to execute if anotherCondition is true
} else {
// Code to execute if none of the above conditions are true
}
Example:
int temperature = 30;
if (temperature > 35) {
Console.WriteLine("It’s very hot!");
} else if (temperature > 25) {
Console.WriteLine("It’s warm.");
} else {
Console.WriteLine("It’s cool.");
}
In this example, the program evaluates the temperature and prints a message based on the conditions.
Switch Statements
The switch statement is an alternative to multiple if-else conditions. It evaluates a variable or expression and matches it with one of the predefined cases.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if none of the cases match
break;
}
Example:
int day = 3;
switch (day) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Other Day");
break;
}
This code checks the value of day and prints the corresponding day of the week.
Loops (for, while, do-while, foreach)
Loops execute a block of code repeatedly based on a condition or for a fixed number of iterations. They’re essential for tasks like iterating over data or performing repeated calculations.
Syntax:
for (initialization; condition; iteration) {
// Code to execute
}
Example:
for (int i = 1; i <= 5; i++) {
Console.WriteLine($"Iteration: {i}");
}
Syntax:
while (condition) {
// Code to execute as long as condition is true
}
Example:
int count = 1;
while (count <= 5) {
Console.WriteLine($"Count: {count}");
count++;
}
领英推荐
This loop guarantees at least one execution before checking the condition.
Syntax:
do {
// Code to execute
} while (condition);
Example:
int num = 1;
do {
Console.WriteLine($"Number: {num}");
num++;
} while (num <= 5);
This loop is ideal for iterating over collections like arrays or lists.
Syntax:
foreach (var item in collection) {
// Code to execute for each item
}
Example:
string[] colors = { "Red", "Green", "Blue" };
foreach (string color in colors) {
Console.WriteLine(color);
}
Break and Continue Statements
The break and continue statements allow more control over loop execution.
Example:
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
Console.WriteLine(i);
}
This loop stops as soon as i equals 5.
Example:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
Console.WriteLine(i);
}
This loop prints only odd numbers between 1 and 10.
Practical Examples
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
Console.WriteLine($"The sum of numbers from 1 to 10 is: {sum}");
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
foreach (string name in names) {
if (name == "Bob") {
continue; // Skip Bob
}
Console.WriteLine(name);
}
These practical examples demonstrate how conditional statements and loops can solve real-world problems efficiently.
References and Citations
Senior Software Engineer at WatchGuard Technologies
2 个月Go have take this 100 day quiz https://youtube.com/shorts/I8VaW4bKbI0