Learn how to make decisions and create loops in C# programming. Master if/else statements, switch statements, and loops to build dynamic, interactive programs that respond to different conditions.
In our previous article, you learned how to store information using variables and data types. But what makes programming truly powerful is the ability to make decisions and repeat actions based on different conditions. This is called control flow - directing how your program executes based on the data it encounters.
Think of control flow like a choose-your-own-adventure book. Depending on the choices you make (or the data your program receives), it takes different paths and performs different actions.
The most fundamental way to control program flow is with if statements. They work exactly like they sound: "If this condition is true, then do this action."
int temperature = 25;
if (temperature > 20)
{
Console.WriteLine("It's a warm day!");
}
This code checks if the temperature is greater than 20. If it is, it prints the message. If not, nothing happens.
Most of the time, you want something to happen regardless of the condition:
int age = 17;
if (age >= 18)
{
Console.WriteLine("You can vote!");
}
else
{
Console.WriteLine("You're not old enough to vote yet.");
}
When you have several conditions to check:
int score = 85;
if (score >= 90)
{
Console.WriteLine("Grade: A - Excellent!");
}
else if (score >= 80)
{
Console.WriteLine("Grade: B - Good job!");
}
else if (score >= 70)
{
Console.WriteLine("Grade: C - Average");
}
else if (score >= 60)
{
Console.WriteLine("Grade: D - Below average");
}
else
{
Console.WriteLine("Grade: F - Failed");
}
Let's build a simple number guessing game to see if statements in action:
using System;
class NumberGuessingGame
{
static void Main()
{
int secretNumber = 42;
int playerGuess = 35;
Console.WriteLine("=== Number Guessing Game ===");
Console.WriteLine($"Your guess: {playerGuess}");
if (playerGuess == secretNumber)
{
Console.WriteLine("๐ Congratulations! You guessed correctly!");
}
else if (playerGuess < secretNumber)
{
Console.WriteLine("๐ Too low! Try a higher number.");
}
else
{
Console.WriteLine("๐ Too high! Try a lower number.");
}
Console.WriteLine("Thanks for playing!");
}
}
To make good decisions, you need to compare values. Here are the essential comparison operators:
int a = 10;
int b = 5;
// Equality
bool isEqual = (a == b); // false
bool isNotEqual = (a != b); // true
// Magnitude comparisons
bool isGreater = (a > b); // true
bool isLess = (a < b); // false
bool isGreaterOrEqual = (a >= b); // true
bool isLessOrEqual = (a <= b); // false
Sometimes you need to check multiple conditions at once:
int age = 25;
bool hasLicense = true;
bool hasInsurance = true;
// AND operator (&&) - ALL conditions must be true
if (age >= 18 && hasLicense && hasInsurance)
{
Console.WriteLine("You can rent a car!");
}
// OR operator (||) - ANY condition can be true
bool isWeekend = true;
bool isHoliday = false;
if (isWeekend || isHoliday)
{
Console.WriteLine("No work today!");
}
// NOT operator (!) - reverses the condition
bool isLoggedIn = false;
if (!isLoggedIn)
{
Console.WriteLine("Please log in first.");
}
When you have many specific values to check, switch statements are cleaner than multiple if-else statements:
string dayOfWeek = "Monday";
switch (dayOfWeek)
{
case "Monday":
Console.WriteLine("Start of the work week!");
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
Console.WriteLine("Midweek grind!");
break;
case "Friday":
Console.WriteLine("TGIF!");
break;
case "Saturday":
case "Sunday":
Console.WriteLine("Weekend time!");
break;
default:
Console.WriteLine("That's not a valid day!");
break;
}
C# also offers a more concise syntax:
string dayOfWeek = "Friday";
string mood = dayOfWeek switch
{
"Monday" => "๐ด Sleepy",
"Tuesday" or "Wednesday" or "Thursday" => "๐ Working",
"Friday" => "๐ Excited",
"Saturday" or "Sunday" => "๐ Relaxed",
_ => "๐ค Confused"
};
Console.WriteLine($"Today's mood: {mood}");
Loops let you repeat code multiple times without writing it over and over again.
Use for loops when you know exactly how many times you want to repeat something:
// Count from 1 to 5
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Count: {i}");
}
// Output:
// Count: 1
// Count: 2
// Count: 3
// Count: 4
// Count: 5
Let's break down the for loop structure:
int i = 1 - Initialize the counter
i <= 5 - Continue while this condition is true
i++ - Increment the counter after each iteration
Use while loops when you want to repeat until a condition changes:
int health = 100;
int damage = 15;
while (health > 0)
{
Console.WriteLine($"Player health: {health}");
health -= damage;
Console.WriteLine("Player takes damage!");
}
Console.WriteLine("Game Over!");
Sometimes you want to execute code at least once, then check the condition:
string userInput;
do
{
Console.WriteLine("Enter 'quit' to exit:");
userInput = Console.ReadLine();
Console.WriteLine($"You entered: {userInput}");
} while (userInput != "quit");
Console.WriteLine("Goodbye!");
Let's combine everything we've learned into a simple calculator:
using System;
class SimpleCalculator
{
static void Main()
{
bool keepRunning = true;
Console.WriteLine("=== Simple Calculator ===");
while (keepRunning)
{
Console.WriteLine("\nChoose an operation:");
Console.WriteLine("1. Addition (+)");
Console.WriteLine("2. Subtraction (-)");
Console.WriteLine("3. Multiplication (*)");
Console.WriteLine("4. Division (/)");
Console.WriteLine("5. Exit");
Console.Write("Enter your choice (1-5): ");
string choice = Console.ReadLine();
if (choice == "5")
{
keepRunning = false;
Console.WriteLine("Thanks for using the calculator!");
continue;
}
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
double result = 0;
string operation = "";
switch (choice)
{
case "1":
result = num1 + num2;
operation = "+";
break;
case "2":
result = num1 - num2;
operation = "-";
break;
case "3":
result = num1 * num2;
operation = "*";
break;
case "4":
if (num2 != 0)
{
result = num1 / num2;
operation = "/";
}
else
{
Console.WriteLine("Error: Cannot divide by zero!");
continue;
}
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
continue;
}
Console.WriteLine($"Result: {num1} {operation} {num2} = {result}");
}
}
}
Instead of deeply nested if statements, use guard clauses:
// โ Deeply nested (harder to read)
public void ProcessUser(string username, int age)
{
if (username != null)
{
if (username.Length > 0)
{
if (age >= 18)
{
Console.WriteLine("Processing adult user...");
// Main logic here
}
else
{
Console.WriteLine("User is too young");
}
}
else
{
Console.WriteLine("Username cannot be empty");
}
}
else
{
Console.WriteLine("Username cannot be null");
}
}
// โ
Guard clauses (cleaner)
public void ProcessUser(string username, int age)
{
if (username == null)
{
Console.WriteLine("Username cannot be null");
return;
}
if (username.Length == 0)
{
Console.WriteLine("Username cannot be empty");
return;
}
if (age < 18)
{
Console.WriteLine("User is too young");
return;
}
Console.WriteLine("Processing adult user...");
// Main logic here
}
Use named constants instead of magic numbers:
// โ Magic numbers (unclear meaning)
if (score >= 90)
{
Console.WriteLine("Excellent!");
}
// โ
Named constants (clear meaning)
const int EXCELLENT_GRADE_THRESHOLD = 90;
if (score >= EXCELLENT_GRADE_THRESHOLD)
{
Console.WriteLine("Excellent!");
}
Break complex conditions into readable parts:
// โ Complex condition (hard to understand)
if (user.Age >= 18 && user.HasValidID && (user.AccountType == "Premium" || user.YearsActive >= 2) && !user.IsSuspended)
{
// Grant access
}
// โ
Broken down conditions (easier to understand)
bool isAdult = user.Age >= 18;
bool hasValidCredentials = user.HasValidID;
bool isEligibleMember = user.AccountType == "Premium" || user.YearsActive >= 2;
bool isAccountActive = !user.IsSuspended;
if (isAdult && hasValidCredentials && isEligibleMember && isAccountActive)
{
// Grant access
}
// โ This assigns 5 to x (always true!)
if (x = 5)
// โ
This compares x to 5
if (x == 5)
// โ This will run forever!
int counter = 0;
while (counter < 10)
{
Console.WriteLine(counter);
// Forgot to increment counter!
}
// โ
This will stop after 10 iterations
int counter = 0;
while (counter < 10)
{
Console.WriteLine(counter);
counter++; // Don't forget this!
}
// โ This will fall through to the next case!
switch (grade)
{
case "A":
Console.WriteLine("Excellent!");
// Missing break - will also execute case "B"!
case "B":
Console.WriteLine("Good!");
break;
}
// โ
Always include break statements
switch (grade)
{
case "A":
Console.WriteLine("Excellent!");
break;
case "B":
Console.WriteLine("Good!");
break;
}
Try building these programs to practice control flow:
Create a program that checks if a password meets these criteria:
- At least 8 characters long
- Contains at least one number
- Contains at least one uppercase letter
Create a program that prints the multiplication table for any number using nested loops.
Build a simple rock-paper-scissors game that:
- Takes user input
- Generates computer choice
- Determines the winner
- Asks if the user wants to play again
Congratulations! You now understand how to make your programs interactive and dynamic. In our next article, we'll explore Functions and Methods - how to organize your code into reusable pieces that make your programs more maintainable and efficient.
Control flow is what transforms simple data storage into intelligent, responsive programs. Practice with different conditions and loops, and you'll start to see the endless possibilities of what you can create!
- If statements let your program make decisions based on conditions
- Loops let you repeat actions efficiently
- Switch statements handle multiple specific value checks elegantly
- Comparison operators (
==, !=, >, <, >=, <=) let you compare values
- Logical operators (
&&, ||, !) let you combine conditions
- Guard clauses and simple conditions make code more readable
- Always avoid infinite loops and remember break statements in switch cases
You're building the foundation for creating truly interactive programs. Keep practicing! ๐