C# (C-Sharp) is a versatile, object-oriented programming language developed by Microsoft. One of its core strengths lies in its ability to control the flow of execution using conditional and loop statements. In this guide, we will dive deep into control statements in C# including if-else, switch, and loop constructs such as for, while, do-while, and foreach. These control structures form the backbone of decision-making and iteration in every C# application.
Control statements in C# are used to dictate the flow of execution within a program. They allow your application to make decisions, repeat operations, or choose different paths based on conditions and values. The major categories of control statements are:
The if statement executes a block of code if a specified condition evaluates to true.
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote.");
}
When there are two conditions β one for true and one for false β you can use an if-else statement.
int age = 16;
if (age >= 18)
{
Console.WriteLine("Eligible to vote.");
}
else
{
Console.WriteLine("Not eligible to vote.");
}
To check multiple conditions, use the if-else if-else ladder.
int marks = 75;
if (marks >= 90)
{
Console.WriteLine("Grade A");
}
else if (marks >= 75)
{
Console.WriteLine("Grade B");
}
else if (marks >= 60)
{
Console.WriteLine("Grade C");
}
else
{
Console.WriteLine("Fail");
}
You can place an if statement inside another if statement.
int age = 20;
bool hasID = true;
if (age >= 18)
{
if (hasID)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("ID is required.");
}
}
The switch statement provides an efficient way to dispatch execution to different parts of code based on the value of a variable.
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;
}
string fruit = "Apple";
switch (fruit)
{
case "Apple":
Console.WriteLine("Red fruit");
break;
case "Banana":
Console.WriteLine("Yellow fruit");
break;
default:
Console.WriteLine("Unknown fruit");
break;
}
Loops allow a set of instructions to be executed repeatedly until a condition is met.
Use when the number of iterations is known.
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Iteration: " + i);
}
Use when the number of iterations is unknown and the loop continues until the condition becomes false.
int i = 1;
while (i <= 5)
{
Console.WriteLine("Iteration: " + i);
i++;
}
Executes the loop body at least once before checking the condition.
int i = 1;
do
{
Console.WriteLine("Iteration: " + i);
i++;
} while (i <= 5);
Used for iterating over arrays or collections.
string[] colors = { "Red", "Green", "Blue" };
foreach (string color in colors)
{
Console.WriteLine(color);
}
Used to exit a loop or switch statement prematurely.
for (int i = 1; i <= 10; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
Skips the current iteration and jumps to the next.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
continue;
Console.WriteLine(i);
}
Exits from the current method and optionally returns a value.
int Add(int a, int b)
{
return a + b;
}
You can place one loop inside another loop to form a nested loop structure.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.Write(i + "," + j + " ");
}
Console.WriteLine();
}
Control statements in C# are crucial for building logic in applications. Mastering conditional statements and loops allows developers to write cleaner, efficient, and maintainable code. The combination of if-else conditions, switch-case, and loops like for, while, do-while, and foreach make it easier to implement various programming scenarios β from basic decision-making to complex data processing.
Copyrights © 2024 letsupdateskills All rights reserved