In C#, control structures are fundamental constructs that allow you to control the flow of execution in a program. They enable you to make decisions based on certain conditions and repeat blocks of code as needed. Here are the main control structures in C#:
1. if statement:
The if statement allows you to execute a block of code if a certain condition is true. It has the following syntax:
if (condition)
{
// code to be executed if the condition is true
}
Example of if statement:
int x = 10;
if (x > 0)
{
Console.WriteLine("x is positive");
}
2. if-else statement:
The if-else statement allows you to execute one block of code if a condition is true and another block of code if the condition is false. It has the following syntax:
if (condition)
{
// code to be executed if the condition is true
}
else
{
// code to be executed if the condition is false
}
Example of if-else statement:
int x = 5;
if (x % 2 == 0)
{
Console.WriteLine("x is even");
}
else
{
Console.WriteLine("x is odd");
}
3. nested if-else:
You can also nest if-else statements to handle multiple conditions.
if (condition1)
{
// code to be executed if condition1 is true
}
else if (condition2)
{
// code to be executed if condition1 is false and condition2 is true
}
else
{
// code to be executed if both condition1 and condition2 are false
}
Example of nested if-else statement:
int x = 10;
if (x > 0)
{
Console.WriteLine("x is positive");
}
else if (x < 0)
{
Console.WriteLine("x is negative");
}
else
{
Console.WriteLine("x is zero");
}
4. Short `if-else` Statement:
In C#, you can use the short version of the `if-else` statement, also known as the ternary conditional operator (`? :`). It allows you to conditionally assign a value to a variable based on a condition.
Syntax of the short `if-else` statement:
variable = (condition) ? value_if_true : value_if_false;
Example:
int x = 10;
string result = (x > 0) ? "x is positive" : "x is non-positive";
Console.WriteLine(result);
5. switch statement:
The switch statement allows you to select one of many code blocks to be executed based on the value of a variable. It has the following syntax:
switch (variable)
{
case value1:
// code to be executed if variable equals value1
break;
case value2:
// code to be executed if variable equals value2
break;
// ...
default:
// code to be executed if none of the above cases are true
break;
}
Example of switch statement:
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
Console.WriteLine("Good job!");
break;
case 'C':
Console.WriteLine("Passing grade");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
6. while loop:
The while loop repeatedly executes a block of code as long as a certain condition is true.
while (condition)
{
// code to be executed repeatedly as long as the condition is true
}
Example of while loop:
int count = 0;
while (count < 5)
{
Console.WriteLine("Count: " + count);
count++;
}
7. do-while loop:
The do-while loop is similar to the while loop, but it guarantees that the code block is executed at least once before checking the condition. It has the following syntax:
do
{
// code to be executed
} while (condition);
Example of do-while loop:
int count = 0;
do
{
Console.WriteLine("Count: " + count);
count++;
} while (count < 5);
8. for loop:
The for loop allows you to repeatedly execute a block of code for a specific number of times. It has the following syntax:
for (initialization; condition; increment)
{
// code to be executed in each iteration
}
Example of for loop:
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("i: " + i);
}
9. `foreach` Loop:
The `foreach` loop is used to iterate over elements in a collection, such as arrays, lists, or other data structures. It simplifies the process of iterating over elements, especially for collections that don't have a fixed size.
Syntax of the `foreach` loop:
foreach (var item in collection)
{
// code to be executed for each item in the collection
}
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output:
1
2
3
4
5
In this example, the `foreach` loop iterates through each element in the `numbers` array and prints its value.
NOTE: Don't worry if you don't understand the example above. You will learn more about Arrays in later tutorials.(alert-success)
These are the main control structures in C#. They provide powerful ways to control the flow of your program based on various conditions and loops.
Jump Statements:
In C#, jump statements are used to alter the normal flow of control in a program. They allow you to transfer program control to a different part of the code. The two main jump statements in C# are:
1. break statement:
The break statement is used to exit the current loop or switch statement. When encountered, it terminates the innermost loop or exits the switch statement, and the control is transferred to the next statement after the loop or switch. Here's an example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // exit the loop when i is equal to 5
}
Console.Write(i + " ");
}
Console.Write("Loop ended.");
Output:
0 1 2 3 4 Loop ended.
2. continue statement:
The continue statement is used to skip the rest of the current iteration of a loop and move to the next iteration. It forces the loop to immediately jump to the next iteration without executing the remaining code inside the loop for the current iteration. Here's an example:
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
continue; // skip iteration when i is equal to 2
}
Console.Write(i + " ");
}
Console.Write("Loop ended.");
Output:
0 1 3 4 Loop ended.
These jump statements provide ways to control the flow of a program by altering the normal sequence of execution.