C-SHARP - 3.9 Condition (Demo)
We are now on Visual Studio to practise conditions in C#. We reuse the cinema example from the previous lesson: a person must be at least 18 to enter. We declare an int age = 19, then write an if followed by the condition between parentheses and the instructions between braces. Running the program prints the access message because the condition is true. If we change the age to 12, nothing appears: the if block is skipped because the condition is false.
To handle the opposite case, C# offers the else keyword, which means otherwise. The else block runs only when the condition of the if is false. We can chain a third branch with else if to test another condition, for instance equality with age == 65 to display a senior discount. Remember to use the double == for equality (single = assigns a value). If a block contains only one instruction, braces are optional, but they are required as soon as you have two or more.
int age = 19;
if (age >= 18)
{
Console.WriteLine("You are an adult, you can enter.");
}
else
{
Console.WriteLine("You are a minor, you must be accompanied.");
}
switch (age)
{
case < 4:
Console.WriteLine("Entrance is free for children under 4.");
break;
case >= 5 and <= 12:
Console.WriteLine("Children pay a reduced fare.");
break;
default:
Console.WriteLine("No discount applies.");
break;
}
The switch statement
- Use
switch (variable)when many independent values lead to different actions. - Each
caseends withbreak;to exit the structure. - The
defaultcase acts like a finalelseand catches every value not explicitly listed.
A long chain of if / else if quickly becomes unreadable. The switch statement clusters the cases on a single variable and runs faster. Building these habits early helps you write optimised, clear programs as your scripts grow longer. See you in the next video.
Summary
This lesson demonstrates how to implement conditional statements in C# through a practical age-verification example for cinema access. It covers the `if` statement for testing conditions, the `else` keyword for handling false conditions, and the `else if` statement for testing multiple specific conditions. The lesson also introduces the `switch` statement as a cleaner, more optimized alternative when dealing with many conditional branches.
Key points
- The `if` statement in C# tests a condition and executes code only if that condition evaluates to true
- The `else` keyword handles all remaining cases that the preceding `if` statement does not cover
- The `else if` statement allows testing additional specific conditions without re-evaluating the original `if`
- Console.WriteLine() is used to display messages to the user based on which condition is met
- Single-instruction code blocks can omit curly braces for improved readability
- The `switch` statement provides a cleaner and more optimized alternative when dealing with many conditional branches on the same variable
FAQ
What happens when an `if` condition is false?
If the condition is false, the code block inside the `if` is skipped. If an `else` block follows, that code executes instead. If there is an `else if`, its condition is tested before the `else`.
Why is `else if` better than multiple separate `if` statements?
`else if` is more efficient and readable because it stops checking conditions once one is true, whereas multiple separate `if` statements would evaluate all conditions regardless of previous results.
When should I use `switch` instead of `if`/`else if`/`else`?
Use `switch` when you have many conditions testing the same variable for specific values. It makes the code cleaner, more organized, and more optimized than multiple nested `if`/`else if` blocks.