.NET Documentation

Control Flow in .NET

Control flow statements are essential for directing the execution path of your program. They allow you to make decisions, repeat actions, and define the sequence in which code is executed. .NET, leveraging languages like C# and Visual Basic, provides a rich set of control flow constructs.

Conditional Statements

Conditional statements allow your program to execute different blocks of code based on whether certain conditions are true or false.

if and else

The most basic form of conditional logic. The code block within the if statement is executed only if the specified condition is true. An optional else block can execute if the condition is false.


if (condition)
{
    // Code to execute if the condition is true
}
else
{
    // Code to execute if the condition is false
}
        

else if

Used to check multiple conditions in sequence.


if (condition1)
{
    // Execute if condition1 is true
}
else if (condition2)
{
    // Execute if condition1 is false and condition2 is true
}
else
{
    // Execute if all preceding conditions are false
}
        

switch Statement

The switch statement provides a more structured way to select one of many code blocks to be executed. It's particularly useful when you have a single variable or expression to compare against multiple possible constant values.


switch (expression)
{
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    default:
        // Code if no match
        break;
}
        

Looping Statements

Looping statements are used to execute a block of code repeatedly.

for Loop

A for loop is ideal when you know in advance how many times you want to iterate.


for (initialize; condition; increment)
{
    // Code to repeat
}
        

while Loop

A while loop repeatedly executes a code block as long as a specified condition remains true. The condition is checked before each iteration.


while (condition)
{
    // Code to repeat
}
        

do-while Loop

Similar to while, but the condition is checked after the code block has been executed at least once, guaranteeing at least one execution.


do
{
    // Code to repeat
} while (condition);
        

foreach Loop

The foreach loop iterates over all elements in a collection (like an array or list) without needing to manage an index.


foreach (var item in collection)
{
    // Process each item
}
        

Jump Statements

Jump statements alter the flow of execution by transferring control to another part of the program.

break

Exits the innermost enclosing loop or switch statement immediately.

continue

Skips the rest of the current iteration of a loop and proceeds to the next iteration.

return

Exits the current method and optionally returns a value.

goto

Transfers control directly to a labeled statement within the same method. Use with caution, as it can make code harder to read and maintain.

Best Practices

Use the most appropriate loop for your task. Prefer foreach for collections when index management isn't needed. Ensure conditions in loops eventually become false to avoid infinite loops.

Examples

See the full code examples in the C# and VB.NET sections of the .NET documentation for practical application of these control flow structures.

Advanced Topics

Explore exception handling (try-catch-finally) and asynchronous programming constructs like async/await for more complex control flow scenarios.