Lesson 1 of 0
In Progress

Control Flow Statements in C#

Control flow statements are the foundation of programming. As a developer, you will use them daily to control the flow of your program and to create complex logic. C# is a widely used programming language, and understanding control flow statements is essential for writing successful C# programs. In this article, we will explore the various control flow statements available in C#, and how to use them to create complex logic.

What are Control Flow Statements?

Control flow statements are instructions that are used to control the flow of your program. They are used to decide when certain instructions should be executed, and when certain instructions should be skipped. By using control flow statements, you can create complex logic that can be used to make decisions and execute different instructions depending on the conditions of the program.

Types of Control Flow Statements in C#

In C#, there are four main types of control flow statements: if statements, switch statements, loops, and jump statements. Each type of control flow statement has a specific purpose, and they all work together to create complex logic in your program.

If Statements

If statements are the most basic control flow statement. They are used to execute a certain instruction only if a certain condition is met. In C#, if statements are written using the following syntax:

if (condition) 
{ 
    // Statements to execute if condition is true 
}

For example, if we wanted to check if a variable called x is equal to 10, we could write the following code:

int x = 10; 
if (x == 10) 
{ 
    Console.WriteLine("x is equal to 10"); 
}

In this example, the if statement checks if the value of x is equal to 10. If it is, then the statement inside the if block is executed. If it is not, then the statement inside the if block is skipped.

Switch Statements

Switch statements are similar to if statements, but they are used when there are multiple conditions that need to be checked. In C#, switch statements are written using the following syntax:

switch (variable) 
{ 
    case value1: 
        // Statements to execute if variable is equal to value1 
        break; 
    case value2: 
        // Statements to execute if variable is equal to value2 
        break; 
    default: 
        // Statements to execute if variable is not equal to any of the cases 
        break; 
}

For example, if we wanted to check the value of a variable called color, we could write the following code:

string color = "red"; 
switch (color) 
{ 
    case "red": 
        Console.WriteLine("The color is red"); 
        break; 
    case "blue": 
        Console.WriteLine("The color is blue"); 
        break; 
    default: 
        Console.WriteLine("The color is not red or blue"); 
        break; 
}

In this example, the switch statement checks the value of the color variable. If it is equal to “red”, then the statement inside the “red” case block is executed. If it is equal to “blue”, then the statement inside the “blue” case block is executed. If it is not equal to either “red” or “blue”, then the statement inside the default block is executed.

Loops

Loops are control flow statements that are used to execute a certain instruction multiple times. In C#, there are two types of loops: for loops and while loops. For loops are used when you know how many times an instruction should be executed, while while loops are used when you don’t know how many times an instruction should be executed.

For Loops

For loops are used when you know how many times an instruction should be executed. In C#, for loops are written using the following syntax:

for (initialization; condition; update) 
{ 
    // Statements to execute 
}

For example, if we wanted to print the numbers 1 to 10 to the console, we could write the following code:

for (int i = 1; i <= 10; i++) 
{ 
    Console.WriteLine(i); 
}

In this example, the for loop starts by initializing a variable called i to 1. It then checks if the value of i is less than or equal to 10. If it is, then the statement inside the for loop is executed. After the statement is executed, the value of i is updated by adding 1 to it. This process is repeated until the condition is false, at which point the loop is exited.

While Loops

While loops are used when you don’t know how many times an instruction should be executed. In C#, while loops are written using the following syntax:

while (condition) 
{ 
    // Statements to execute 
}

For example, if we wanted to keep asking the user for their name until they enter “quit”, we could write the following code:

string name = ""; 
while (name != "quit") 
{ 
    Console.WriteLine("Enter your name: "); 
    name = Console.ReadLine(); 
}

In this example, the while loop checks if the value of the name variable is equal to “quit”. If it is not, then the statement inside the while loop is executed. After the statement is executed, the condition is checked again. This process is repeated until the condition is false, at which point the loop is exited.

Jump Statements

Jump statements are used to control the flow of a program. They are used to jump to a specific point in the program, or to exit a loop. In C#, there are three types of jump statements: break, continue, and goto.

Break Statement

The break statement is used to exit a loop. In C#, the break statement is written using the following syntax:

break; 

For example, if we wanted to exit a while loop when the user enters “quit”, we could write the following code:

string name = ""; 
while (true) 
{ 
    Console.WriteLine("Enter your name: "); 
    name = Console.ReadLine(); 
    if (name == "quit") 
    { 
        break; 
    } 
}

In this example, the break statement is used to exit the while loop when the user enters “quit”.

Continue Statement

The continue statement is used to skip the rest of the instructions in a loop and go back to the beginning of the loop. In C#, the continue statement is written using the following syntax:

continue; 

For example, if we wanted to skip printing the number 5 to the console, we could write the following code:

for (int i = 1; i <= 10; i++) 
{ 
    if (i == 5) 
    { 
        continue; 
    } 
    Console.WriteLine(i); 
}

In this example, the continue statement is used to skip printing the number 5 and continue to the next iteration of the loop.

Goto Statement

The goto statement is used to jump to a specific point in the program. In C#, the goto statement is written using the following syntax: goto label;

For example, if we wanted to jump to the start of the program, we could write the following code:

Console.WriteLine("This is the start of the program"); 

// Some code here 

goto start; 

Console.WriteLine("This is the end of the program"); 

start: 
Console.WriteLine("This is where the program starts again"); 

In this example, the goto statement is used to jump to the start of the program after the code has been executed.

Conclusion

Control flow statements are the foundation of programming. They are used to control the flow of your program and to create complex logic. In C#, there are four main types of control flow statements: if statements, switch statements, loops, and jump statements. Each type of control flow statement has a specific purpose, and they all work together to create complex logic in your program.

Exercises

Write a program that uses an if statement to check if a variable called x is equal to 10. If it is, print “x is equal to 10” to the console.

int x = 10; 
if (x == 10) 
{ 
    Console.WriteLine("x is equal to 10"); 
}

Write a program that uses a switch statement to check the value of a variable called color. If it is equal to “red”, print “The color is red” to the console. If it is equal to “blue”, print “The color is blue” to the console. If it is not equal to either “red” or “blue”, print “The color is not red or blue” to the console.

string color = "red"; 
switch (color) 
{ 
    case "red": 
        Console.WriteLine("The color is red"); 
        break; 
    case "blue": 
        Console.WriteLine("The color is blue"); 
        break; 
    default: 
        Console.WriteLine("The color is not red or blue"); 
        break; 
}

Write a program that uses a for loop to print the numbers 1 to 10 to the console.

for (int i = 1; i <= 10; i++) 
{ 
    Console.WriteLine(i); 
}

Write a program that uses a while loop to keep asking the user for their name until they enter “quit”.

string name = ""; 
while (name != "quit") 
{ 
    Console.WriteLine("Enter your name: "); 
    name = Console.ReadLine(); 
}

Write a program that uses a break statement to exit a while loop when the user enters “quit”.

string name = ""; 
while (true) 
{ 
    Console.WriteLine("Enter your name: "); 
    name = Console.ReadLine(); 
    if (name == "quit") 
    { 
        break; 
    } 
}