Control Flow Statements in C#

Control Flow Statements in C#

In this article, I am going to discuss the Control Flow Statements in C# with Examples. Please read our previous article, where we discussed Operators in C# with Examples. At the end of this article, you will learn what are Control Statements, their type, and when and how to use Control Statements in C# with examples.

Control Flow Statements in C#:

The Control Flow Statements in C# are the statements that Alter the Flow of Program Execution and provide better control to the programmer on the flow of execution. The Control Flow Statements are useful to write better and more complex programs. A program executes from top to bottom except when we use control statements. We can control the order of execution of the program, based on logic and values.

Generally, the statements inside our C# program are executed from top to bottom, in the order that they appear. The Control flow statements, change or break the flow of execution by implementing decision-making, looping, and branching in our program to execute particular blocks of code based on the conditions.

Example to Understand Control Flow Statements in C#:

By default, when we write statements in a program, the statements are going to be executed sequentially from top to bottom line by line. For example, in the below program we have written five statements. Now, if you execute the below program, the statements are going to be executed one by one from the top to bottom. That means, first it will execute statement1, then statement2, then statement3, then statement4, and finally statement5.

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Executing Statment1");
            Console.WriteLine("Executing Statment2");
            Console.WriteLine("Executing Statment3");
            Console.WriteLine("Executing Statment4");
            Console.WriteLine("Executing Statment5");
            Console.ReadKey();
        }
    }
}
Output:

It is also possible in the C# programming language to alter the execution of the program. What it means, instead of executing the statements sequentially one by one, we can change the order of execution. If we want, we can skip some of the statement execution based on some conditions. If we want, we can also jump from one statement to another statement inside the program, let’s say jumping from statement 1 to statement 4. Even if we want, we can repeatedly execute some of the statements multiple times. And this is possible because of Control Flow Statements in C#.

In the below example, the statements written inside the if block is going to execute and the statements written inside the else block will be skipped. But the statement which is there after the if block is then going to execute. Here, we are using the if-else control flow statement.

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            if(10 > 5)
            {
                Console.WriteLine("Executing Statment1");
                Console.WriteLine("Executing Statment2");
            }
            else
            {
                Console.WriteLine("Executing Statment3");
                Console.WriteLine("Executing Statment4");
            }
            
            Console.WriteLine("Executing Statment5");
            Console.ReadKey();
        }
    }
}
Output:

Control Flow Statements in C# with Examples

In the below example, we are repeatedly executing the statement which is there inside the for-loop block 5 times. After executing the for-loop body 5 times, it will then come out from the loop and execute the other statement only once. This is possible because of the looping conditional statement.

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Welcome to C#.NET");
            }
            
            Console.WriteLine("Some Other Statement");
            Console.ReadKey();
        }
    }
}
Output:

Control Flow Statements in C#

In the below example, after executing the statement1, the control will jump to statement4 by skipping statement2 and statement3. Here, we are using the goto control statement.

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Executing Statment1");
            goto statement4;   //goto label someLabel:
            Console.WriteLine("Executing Statment2");
            Console.WriteLine("Executing Statment3");

            statement4:    //label:
            Console.WriteLine("Executing Statment4");
            Console.WriteLine("Executing Statment5");
            Console.ReadKey();
        }
    }
}
Output:

Control Flow Statements

In the above three examples, we have used control flow statements to control the flow of program execution or you can alter the flow of program execution.

Types of Control Flow Statements in C#:

In C#, the control flow statements are divided into the following three categories:

  1. Selection Statements or Branching Statements: (Examples: if-else, switch case, nested if-else, if-else ladder)
  2. Iteration Statements or Looping Statements: (Examples: while loop, do-while loop, for-loop, and foreach loop)
  3. Jumping Statements: (Examples: break, continue, return, goto)

For a better understanding, please have a look at the following diagram which shows the classification of different control flow statements.

Types of Control Flow Statements in C#

Note: The Control Flow Statements are used to write powerful programs by repeating important sections of the program and selecting between optional sections of a program.

In the next article, I am going to discuss Selection or Branching Control Flow Statements in C# with Examples. Here, in this article, I try to explain what is Control Flow Statements in C#, and their type. I hope you enjoy this Control Flow Statements in C# Language article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.