Control Flow in .NET Core
Control flow statements are fundamental to programming, allowing you to dictate the order in which code is executed. They enable your programs to make decisions, repeat actions, and branch execution paths based on certain conditions.
Conditional Statements
Conditional statements allow you to execute different blocks of code based on whether a condition is true or false.
The if
Statement
The if
statement executes a block of code only if a specified condition evaluates to true
.
if (condition) { // Code to execute if condition is true }
The if-else
Statement
The if-else
statement provides an alternative block of code to execute if the if
condition is false
.
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
The if-else if-else
Statement
This structure allows for checking multiple conditions sequentially. Execution stops at the first condition that evaluates to true
.
if (condition1) { // Code for condition1 } else if (condition2) { // Code for condition2 } else { // Code if no conditions are true }
The switch
Statement
The switch
statement provides a way to execute different code blocks based on the value of an expression. It's often more readable than long if-else if
chains.
switch (expression) { case value1: // Code for value1 break; case value2: // Code for value2 break; default: // Code if no case matches break; }
Looping Statements
Looping statements allow you to execute a block of code repeatedly.
The for
Loop
The for
loop is typically used when you know the number of times you want to iterate.
for (initialization; condition; iterator) { // Code to repeat }
Example:
for (int i = 0; i < 5; i++) { Console.WriteLine($"Iteration: {i}"); }
The while
Loop
The while
loop executes a block of code as long as a specified condition remains true
.
while (condition) { // Code to repeat }
The do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the code block is executed at least once before the condition is checked.
do { // Code to repeat } while (condition);
The foreach
Loop
The foreach
loop is used to iterate over collections (like arrays, lists, etc.).
foreach (var item in collection) { // Code to process each item }
Jump Statements
Jump statements alter the normal flow of execution within loops or switch statements.
The break
Statement
The break
statement is used to exit a loop or a switch
statement immediately.
The continue
Statement
The continue
statement skips the rest of the current iteration of a loop and proceeds to the next iteration.
The return
Statement
The return
statement exits the current method and optionally returns a value.
The goto
Statement
The goto
statement transfers control to a labeled statement within the same method. Its use is generally discouraged due to potential readability issues.
Mastering control flow statements is crucial for writing dynamic and responsive applications in .NET Core. Experiment with these constructs to build sophisticated logic within your programs.