The goal of this tutorial is to help you understand the control flow in C# and introduce you to conditional statements and loops.
By the end of this tutorial, you will be able to:
Basic understanding of C# programming is required. Familiarity with the syntax and basic data types in C# would be beneficial.
In C#, we have two primary types of conditional statements: if statements and switch statements.
if statement: This is the most basic control flow statement. It allows the program to execute a certain section of code only if a particular test evaluates to true.if (condition)
{
// code to be executed if condition is true
}
switch statement: This is a type of selection statement that allows a variable to be tested for equality against a list of values. 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 variable doesn't match any values;
break;
}
In C#, we have three primary types of loops: for loops, while loops, and do-while loops.
for loop: The for loop is used when you know in advance how many times the script should run.for (initialization; condition; increment/decrement)
{
// code block to be executed
}
while loop: The while loop loops through a block of code as long as a specified condition is true.while (condition)
{
// code block to be executed
}
do-while loop: The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.do
{
// code block to be executed
}
while (condition);
Let's see some practical examples of these concepts.
if Statementint a = 10;
int b = 20;
if (a < b)
{
Console.WriteLine("a is less than b");
}
// The output will be "a is less than b"
In this example, the condition a < b is true, so the code within the if statement is executed.
switch Statementint day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
// The output will be "Thursday"
In this example, the variable day is checked against each case. When it matches with 4, the corresponding code is executed.
for Loopfor (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
// The output will be
// 0
// 1
// 2
// 3
// 4
In this example, the for loop runs five times, and each time it prints the value of i.
In this tutorial, we covered control flow in C#, including conditional statements (if and switch) and loops (for, while, and do-while).
Next, you should practice these concepts and try to create some programs using these control flow statements.
Additional resources:
- Microsoft C# documentation
- C# Station
Here are some exercises to help you practice:
for loop.if statement.switch statement.Solutions and explanations will be provided upon request. Happy coding!