C# Basics/

Flow Control

Original YouTube Logo

This page have a related YouTube video

  • C# has several flow control statements, including conditional statements (such as if-else) and looping statements (such as for and while).
  • Conditional statements allow you to execute a block of code only if a certain condition is true, while looping statements allow you to execute a block of code repeatedly.
  • You can use the break and continue statements to control the flow of a loop, and the goto statement to transfer control to a labeled statement elsewhere in the code.

Flow control determines the order in which the statements in a program are executed. C# has several flow control statements, including conditional statements (such as if-else) and looping statements (such as for and while).

Conditional Statements

Conditional statements allow you to execute a block of code only if a certain condition is true. C# has the following conditional statements:

  • if
  • if-else
  • if-else if-else
  • switch-case

For example:

In this example, the if statement checks whether a is greater than b. If it is, the code block inside the curly braces is executed; otherwise, the code block inside the else clause is executed.

You can also use the if-else if-else statement to check multiple conditions:

And you can use the switch-case statement to select from multiple options:

In this example, the switch-case statement checks the value of day and executes the code block for the matching case. If no case matches, the code block in the default clause is executed.

Looping Statements

Looping statements allow you to execute a block of code repeatedly, either a fixed number of times or until a certain condition is met. C# has the following looping statements:

  • for
  • while
  • do-while
  • foreach

For example, the for loop is used to repeat a block of code a fixed number of times:

In this example, the for loop initializes the variable i to 0, checks whether i is less than 10, and increments i by 1 after each iteration. The code block inside the curly braces is executed 10 times, and the value of i is printed to the console.

The while loop is used to repeat a block of code while a certain condition is true:

In this example, the while loop checks whether i is less than 10, and the code block inside the curly braces is executed as long as the condition is true. The value of i is incremented by 1 after each iteration.

The do-while loop is similar to the while loop, but the code block is executed at least once and the condition is checked at the end of each iteration:

In this example, the code block inside the curly braces is executed once, and then the condition i < 10 is checked. If the condition is true, the code block is executed again and the condition is checked again. This process is repeated until the condition becomes false.

Finally, the foreach loop is used to iterate over the elements of an array or collection:

In this example, the foreach loop iterates over the elements of the numbers array, and the variable number is assigned the value of each element in turn. The code block inside the curly braces is executed for each element, and the value of number is printed to the console.

You can also use the break and continue statements to control the flow of a loop. The break statement immediately exits the loop, while the continue statement skips the rest of the current iteration and moves to the next one:

In the first example, the break statement exits the loop when i is equal to 5. In the second example, the continue statement skips the rest of the current iteration when i is odd.

Flow control is an important aspect of C# programming, and you will use control structures and looping statements frequently as you develop applications. It is important to understand the various options available and how to use them effectively to achieve the desired result.

}

ZetBit

Subscribe to ZetBits Newsletter