C# - Control Statements (if, switch, loops)

C# Control Statements - if, switch, loops

C# Control Statements - if, switch, loops

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.

What Are Control Statements in C#?

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:

  • Conditional Statements: if, else if, else, switch
  • Looping Statements: for, while, do-while, foreach

1. C# Conditional Statements

1.1 if Statement

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.");
}

1.2 if-else Statement

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.");
}

1.3 if-else if-else Ladder

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");
}

1.4 Nested if

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.");
    }
}

2. switch Statement in C#

The switch statement provides an efficient way to dispatch execution to different parts of code based on the value of a variable.

2.1 Syntax of switch

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;
}

2.2 switch with string

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;
}

3. Looping Statements in C#

Loops allow a set of instructions to be executed repeatedly until a condition is met.

3.1 for Loop

Use when the number of iterations is known.

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine("Iteration: " + i);
}

3.2 while Loop

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++;
}

3.3 do-while Loop

Executes the loop body at least once before checking the condition.

int i = 1;

do
{
    Console.WriteLine("Iteration: " + i);
    i++;
} while (i <= 5);

3.4 foreach Loop

Used for iterating over arrays or collections.

string[] colors = { "Red", "Green", "Blue" };

foreach (string color in colors)
{
    Console.WriteLine(color);
}

4. Jump Statements

4.1 break Statement

Used to exit a loop or switch statement prematurely.

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
        break;

    Console.WriteLine(i);
}

4.2 continue Statement

Skips the current iteration and jumps to the next.

for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
        continue;

    Console.WriteLine(i);
}

4.3 return Statement

Exits from the current method and optionally returns a value.

int Add(int a, int b)
{
    return a + b;
}

5. Nested Loops

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.

Beginner 5 Hours
C# Control Statements - if, switch, loops

C# Control Statements - if, switch, loops

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.

What Are Control Statements in C#?

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:

  • Conditional Statements: if, else if, else, switch
  • Looping Statements: for, while, do-while, foreach

1. C# Conditional Statements

1.1 if Statement

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."); }

1.2 if-else Statement

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."); }

1.3 if-else if-else Ladder

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"); }

1.4 Nested if

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."); } }

2. switch Statement in C#

The switch statement provides an efficient way to dispatch execution to different parts of code based on the value of a variable.

2.1 Syntax of switch

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; }

2.2 switch with string

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; }

3. Looping Statements in C#

Loops allow a set of instructions to be executed repeatedly until a condition is met.

3.1 for Loop

Use when the number of iterations is known.

for (int i = 1; i <= 5; i++) { Console.WriteLine("Iteration: " + i); }

3.2 while Loop

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++; }

3.3 do-while Loop

Executes the loop body at least once before checking the condition.

int i = 1; do { Console.WriteLine("Iteration: " + i); i++; } while (i <= 5);

3.4 foreach Loop

Used for iterating over arrays or collections.

string[] colors = { "Red", "Green", "Blue" }; foreach (string color in colors) { Console.WriteLine(color); }

4. Jump Statements

4.1 break Statement

Used to exit a loop or switch statement prematurely.

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

4.2 continue Statement

Skips the current iteration and jumps to the next.

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

4.3 return Statement

Exits from the current method and optionally returns a value.

int Add(int a, int b) { return a + b; }

5. Nested Loops

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.

Related Tutorials

Frequently Asked Questions for General

line

Copyrights © 2024 letsupdateskills All rights reserved