Control Flow Statement

Indicates the flow of the program, the execution process of the program is under a control flow statement. In C#  language there are 3 types of control flow statement available.

1.Selection Statement - IF, IF-ELSE, SWITCH.

2.Iteration Statement - For loop, foreach loop, do-while loop, while loop.

3. Jumping Statement - Break, Continue, go to.

Selection Statement

Selection statement is also called decision-making statement, depending on the condition block need to be executed or not which is decided by the condition, If the condition is 'true' block will be executed, if the condition is 'false' then block will not be executed.



{ /* statement(s) will execute if the boolean expression is true */ }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// namespace declartion
namespace ControlFlowDecisionMaking
    class Program
        static void Main(string[] args)
            /* local variable definition */
            int x = 20;

            /* check the boolean condition using if statement */
            if (x < 120)
                /* if condition is true then print the following */
                Console.WriteLine("a is less than 20");
            Console.WriteLine("value of a is : {0}", x);

1. Constructing the body if always optional i.e recommended to create the body when we are having multiple statement.

2. For a single statement, it is not required to specify the body.

C# - if...else Statement

else is a keyword, by using this keyword we can create an alternate block for its part when we are working with if else among those two blocks at given any point of time only one block will be executed.


{ /* statement(s) will execute if the boolean expression is true */ }


{ /* statement(s) will execute if the boolean expression is false */ }

C# - if...else Statement

It is always legal in C# to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

if( boolean_expression 1)

{ /* Executes when the boolean expression 1 is true */

if(boolean_expression 2)

{ /* Executes when the boolean expression 2 is true */ } }

For Loop

For loop contain 3 parts 




for ( init; condition; increment ) { statement(s); }

Here is the flow of control in a for loop −

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

  • After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of the loop, then increment step, and then again testing for a condition). After the condition becomes false, the for loop terminates.


Foreach Loop

The foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list. It executes for each element present in the array.

  • It is necessary to enclose the statements of foreach loop in curly braces {}.

  • Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.

  • In the loop body, you can use the loop variable you created rather than using an indexed array element.

foreach(data_type var_name in collection_variable)

{ // statements to be executed }

While loop

The test condition is given in beginning of loop and all statements are executed till the given boolean condition satisfies, when the condition becomes false, the control will be out from the while loop.

while (boolean condition) { loop statements... }