For example, if someone wants to execute only a particular set of statements based on some certain logic, then Flow control, and conditional statements will be useful. You will get a better understanding as we go through the various statements which are available in C#. Please note that all the code below is made to the Program.cs file. In this tutorial, you will learn-

If Statement Switch Statement While loop For loop

1) If statement

The if statement is used to evaluate a boolean expression before executing a set of statements. If an expression evaluates to true, then it will run one set of statements else it will run another set of statements. In our example below, a comparison is made for a variable called value. If the value of the variable is less than 10, then it will run one statement, or else it will run on another statement.

Code Explanation

We first define a variable called value and set it to the value of 11. We then use the ‘if’ statement to check if the value is less than 10 of the variable. The result will either be true or false. If the if condition evaluates to true, we then send the message “Value is less than 10” to the console. If the if condition evaluates to false, we then send the message “Value is greater than 10” to the console.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

Output:

We can clearly see that the ‘if’ statement was evaluated to false. Hence the message “Value is greater than 10” was sent to the console.

2) Switch statement

The switch statement is an enhancement to the ‘if’ statement. If you have multiple expressions that need to be evaluated in one shot, then writing multiple ‘if’ statements becomes an issue. The switch statement is used to evaluate an expression and run different statements based on the result of the expression. If one condition does not evaluate to true, the switch statement will then move to the next condition and so forth. Let’s see, how this works with the below example. Here, we are again comparing the value of a variable called ‘value.’ We then check if the value is equal to 1, or 2, or something totally different.

Code Explanation:-

We first define a variable called ‘value’ and set it to the value of 11. We then use the ‘switch’ statement to check the value of the variable ‘value.’ Case statements are used to set different conditions. Based on the conditions, a set of statements can be executed. A switch statement can have multiple case conditions. The first case statement checks to see if the value of the variable is equal to 1. If the first case statement is true, then the message “Value is 1” is written to the console. The break statement is used to break from the entire switch statement, once a condition is true. The default condition is a special condition. This just means that if no case expression evaluates to true, then run the set of statements for the default condition.

If the above code is entered properly and the program is executed successfully, the following output will be displayed. The output prints the default value “Value is different”, since no condition is satisfied.

Output:

3) While loop

The while loop is used for iterative purposes. Suppose if you want to repeat a certain set of statements for a particular number of times, then while loop is used. In our example below, we use the while statement to display the value of a variable ‘i’. The while statement is used to display the value 3 times.

Code Explanation:-

Two Integer variables are defined, one being value and the other being ‘i’. The value variable is used as the upper limit to which we should iterate our while statement. And ‘i’ is the variable which will be processed during the iteration. In the while statement, the value of ‘i’ is constantly checked against the upper limit. Here we display the value of ‘i’ to the console. We also increment the value of ‘i’.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

Output:

Here you can see that the while statement is executed 3 times and incremented at the same time. And each time, it displayed the current value of the variable ‘i’.

4) For loop

The ‘for’ loop is also used for iterative purposes. Suppose if you want to repeat a certain set of statements for a particular number of times, then forloop is used. In our example below, we use the ‘for’ statement to display the value of a variable ‘i’. The ‘for’ statement is used to display the value 3 times.

Code Explanation:-

The ‘for’ keyword is used to start off the ‘for loop’ statement. In the ‘for loop’, we define 3 things. The first is to initialize the value of a variable, which will be used in the ‘for loop’. The second is to compare the value of the ‘i’ against an upper limit. In our case, the upper limit is the value of 3 (i<3). Finally, we increment the value of ‘i’ accordingly. Here we display the value of ‘i’ to the console.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

Output:

Here you can see that the ‘for’ statement is executed 3 times. And each time, it displayed the current value of the variable ‘i’.