CHASH Tutorial



C# CONTROL FLOW


C# Control Flow — If, Switch, Loops

Control flow statements help you decide which code runs and how many times. This tutorial covers:

  • If, else if, else — Conditional branching
  • Switch — Select one block from many options
  • Loops — Repeat code multiple times (for, while, do-while)

1. If, Else If, Else

Checks conditions and runs code based on the result.

int age = 20;

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else if (age >= 13)
{
    Console.WriteLine("You are a teenager.");
}
else
{
    Console.WriteLine("You are a child.");
}
  

2. Switch Statement

Selects one code block to run based on a variable.

char grade = 'B';

switch (grade)
{
    case 'A':
        Console.WriteLine("Excellent!");
        break;
    case 'B':
    case 'C':
        Console.WriteLine("Well done.");
        break;
    case 'D':
        Console.WriteLine("You passed.");
        break;
    default:
        Console.WriteLine("Try again.");
        break;
}
  

3. Loops

Repeat code multiple times.

For Loop

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

While Loop

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

Do-While Loop

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

Try It Yourself

Change the values and conditions, then click Run to see control flow in action.




🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review