C# Control Flow
Control flow in C# refers to the order in which individual statements, instructions, or function calls are executed or evaluated in a program. It helps define the flow of execution based on conditions, loops, or branching decisions. Understanding control flow is crucial for building programs that can make decisions, repeat tasks, and handle multiple scenarios.
1. Control Flow Constructs in C#
C# provides several types of control flow constructs:
- Conditional Statements (if, else, else if, switch)
- Looping Statements (for, while, do-while, foreach)
- Jump Statements (break, continue, return, goto)
Let’s go over each of these categories with examples.
2. Conditional Statements
if
, else
, and else if
The if
statement allows you to execute a block of code based on a condition. You can use else
to define code that will execute when the if
condition is false, and else if
to check multiple conditions.
Syntax:
if (condition)
{
// Code to execute if condition is true
}
else if (anotherCondition)
{
// Code to execute if anotherCondition is true
}
else
{
// Code to execute if none of the conditions are true
}
Example:
int num = 10;
if (num > 10)
{
Console.WriteLine("Number is greater than 10");
}
else if (num == 10)
{
Console.WriteLine("Number is equal to 10");
}
else
{
Console.WriteLine("Number is less than 10");
}
switch
Statement
The switch
statement is used for multi-way branching. It is particularly useful when you need to compare the same variable or expression against multiple possible values.
Syntax:
switch (expression)
{
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if none of the cases match
break;
}
Example:
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
3. Looping Statements
Loops allow you to repeatedly execute a block of code until a condition is met. C# provides several looping constructs.
for
Loop
The for
loop is used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and iteration expression.
Syntax:
for (initialization; condition; iteration)
{
// Code to execute on each loop
}
Example:
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i); // Prints numbers from 1 to 5
}
while
Loop
The while
loop repeats a block of code as long as a given condition is true.
Syntax:
while (condition)
{
// Code to execute while the condition is true
}
Example:
int i = 1;
while (i <= 5)
{
Console.WriteLine(i); // Prints numbers from 1 to 5
i++;
}
do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the code block will be executed at least once, even if the condition is false.
Syntax:
do
{
// Code to execute at least once
}
while (condition);
Example:
int i = 1;
do
{
Console.WriteLine(i); // Prints numbers from 1 to 5
i++;
} while (i <= 5);
foreach
Loop
The foreach
loop is used to iterate over collections such as arrays or lists. It simplifies the iteration process without needing an index.
Syntax:
foreach (type variable in collection)
{
// Code to execute for each element in the collection
}
Example:
int[] numbers = {1, 2, 3, 4, 5};
foreach (int number in numbers)
{
Console.WriteLine(number); // Prints numbers from 1 to 5
}
4. Jump Statements
Jump statements are used to control the flow of loops or the current method.
break
The break
statement is used to exit a loop or switch
statement prematurely.
Example:
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
break; // Exits the loop when i equals 3
}
Console.WriteLine(i);
}
continue
The continue
statement skips the current iteration of the loop and moves to the next iteration.
Example:
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue; // Skips the rest of the loop when i equals 3
}
Console.WriteLine(i);
}
return
The return
statement is used to exit a method and optionally return a value.
Example:
int Add(int a, int b)
{
return a + b; // Exits the method and returns the result
}
goto
The goto
statement transfers control to a labeled statement in the code. It is generally discouraged as it can make code harder to follow.
Example:
int num = 3;
if (num == 3)
{
goto Label; // Jumps to the label
}
Console.WriteLine("This will be skipped");
Label:
Console.WriteLine("Jumped to Label");
5. Summary
- Conditional statements like
if
,else if
,else
, andswitch
allow you to make decisions based on conditions. - Looping statements like
for
,while
,do-while
, andforeach
are used to repeat code. - Jump statements like
break
,continue
,return
, andgoto
alter the normal flow of control in loops or methods. - Control flow constructs are essential in making your program dynamic, allowing it to make decisions, handle different scenarios, and repeat tasks efficiently.