C# Control Flow

Conditional Statements:

  • if: Executes a block of code if a specified condition is true.
  • else if: Provides an alternative condition to be executed if the preceding 'if' condition is false.
  • else: Specifies a block of code to be executed if none of the preceding conditions are true.
                                                    
                                                    int x = 10;
if (x > 0)
{
    Console.WriteLine("x is positive");
}
else if (x < 0)
{
    Console.WriteLine("x is negative");
}
else
{
    Console.WriteLine("x is zero");
}
                                                    
                                                

Switch Statement:

  • switch: Evaluates an expression and executes the corresponding case block based on its value.
  • case: Defines a condition to match against the value of the expression.
  • default: Specifies a block of code to be executed if no case matches the value of the expression.
                                                    
                                                    int dayOfWeek = 3;
switch (dayOfWeek)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    // other cases...
    default:
        Console.WriteLine("Invalid day");
        break;
}
                                                    
                                                

Loops:

  • for: Executes a block of code a specified number of times.
  • while: Executes a block of code as long as a specified condition is true.
  • do-while: Executes a block of code once and then repeats it as long as a specified condition is true.

for loop:

                                                    
                                                    for (int i = 0; i < 5; i++)
{
    Console.WriteLine("Count: " + i);
}
                                                    
                                                

while loop:

                                                    
                                                    int n = 0;
while (n < 5)
{
    Console.WriteLine("Count: " + n);
    n++;
}
                                                    
                                                

do-while loop:

                                                    
                                                    int m = 0;
do
{
    Console.WriteLine("Count: " + m);
    m++;
} while (m < 5);
                                                    
                                                

Jump Statements:

  • break: Exits the loop or switch statement.
  • continue: Skips the rest of the loop's code and proceeds to the next iteration.
  • return: Exits the current method and returns a value (if specified).

break:

                                                    
                                                    for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break; // exit loop when i equals 5
    }
    Console.WriteLine("Count: " + i);
}
                                                    
                                                

continue:

                                                    
                                                    for (int i = 0; i < 5; i++)
{
    if (i == 2)
    {
        continue; // skip iteration when i equals 2
    }
    Console.WriteLine("Count: " + i);
}
                                                    
                                                

return:

                                                    
                                                    int Add(int a, int b)
{
    return a + b; // return the sum of a and b
}