CodeGym /Courses /C# SELF /Conditional Statement: if else

Conditional Statement: if else

C# SELF
Level 3, Lesson 1
Available

1. The if else statement

Programs wouldn't be very useful if they always did the same thing, no matter what was going on around them. A program needs to be able to adapt to different situations and do one thing in some cases, and something else in others.

In C#, this is done with a conditional statement — a special keyword that lets you run different blocks of code depending on whether a condition is true or not.

The conditional statement has three parts: condition, command 1, and command 2. If the condition is true, command 1 runs, otherwise command 2 runs. The commands never run at the same time. The general form looks like this:

if (condition)
   command1;
else
   command2;
Conditional statement if-else

if means "if", and else means "otherwise". In plain English, you could write it like this:

If condition is true, then
   do command1;
otherwise
   do command2;
Conditional statement if-else in plain English

Examples:

//The screen will show: You are still a kid

int age = 17;
if (age < 18)
   Console.WriteLine("You are still a kid");
else
   Console.WriteLine("You are already an adult");
//The screen will show: Warm

int temperature = 5;
if (temperature < 0)
   Console.WriteLine("It's freezing outside");
else
   Console.WriteLine("Warm");
//The screen will show: You can take the driving test

int age = 18;
if (age == 18)
   Console.WriteLine("You can take the driving test");
else
   Console.WriteLine("You can start saving for a car");

2. Command block

If you want your program to run several commands when a condition is (or isn't) met, you can group them into a command block.

To group commands into a block, you need to "wrap" them in curly braces. Here's what it looks like in general:

{
   command1;
   command2;
   command3;
}

You can have as many commands in a block as you want. Even zero.

Examples of combining the if-else statement and a command block:

//The screen will show: You're still a kid. Don't argue with adults

int age = 17;
if (age < 18)
{
   Console.WriteLine("You're still a kid");
   Console.WriteLine("Don't argue with adults");
}
else
{
   Console.WriteLine("You are already an adult");
   Console.WriteLine("Kids these days, huh");
}
//The screen will show: Warm

int temperature = 5;
if (temperature < 0)
{
   Console.WriteLine("It's freezing outside");
   Console.WriteLine("Put your hat on");
}
else
   Console.WriteLine("Warm");
//An empty command block will be executed. Nothing will be shown on the screen.

int age = 21;
if (age == 18)
   Console.WriteLine("Report to the draft office");
else
{
}

3. Shortened syntax for the if statement

If there's no code in the else block, you can just leave it out. Then the if statement looks like this:

if (condition)
   command1;
Conditional statement if

Example:

int age = 19;
if (age >= 18)
{
    Console.WriteLine("You are of legal age!");
}

How does this work?

If the condition is true, the code block runs. If it's false, nothing happens — the program just keeps going.

The else block is there, but it's empty (there are no commands between the curly braces). You can just remove it: nothing will change in the program.

4. Combining conditions: else if

Sometimes you need to check not just "yes/no", but pick from several options. That's where else if comes in:

if (condition1)
{
    // Runs if condition1 is true
}
else if (condition2)
{
    // Runs if condition2 is true and condition1 is false
}
else
{
    // Runs if none of the above conditions matched
}

Example:

int hour = 13;
if (hour < 12)
{
    Console.WriteLine("Good morning!");
}
else if (hour < 18)
{
    Console.WriteLine("Good afternoon!");
}
else
{
    Console.WriteLine("Good evening!");
}

What's happening here?

  • If the time is before 12 — it's morning.
  • If it's from 12 to 18 — it's afternoon.
  • If it's more — it's evening.
  • The program picks only ONE of the options! As soon as a condition matches — the rest aren't even checked.

5. Schematic illustration: branching flowchart

Here's what it looks like as a flowchart:

And if there are several conditions:

This is a lot like the forks in tabletop RPGs: go left — one thing, right — another, straight — something else!

6. What conditions can you check?

Inside the parentheses after if there should be a LOGICAL expression — its result is either true or false.

Main comparison operators:

Operator Meaning Example
== Equals x == 5
!= Not equal x != 10
> Greater than x > 0
< Less than age < 18
>= Greater than or equal to score >= 100
<= Less than or equal to count <= 50

Examples of conditions:

if (number == 10)
if (age != 18)
if (temperature > 36)
if (balance <= 0)

Notice: = (one sign) — is assignment. == (two signs) — is comparison. This is one of the most popular mistakes among newbies! If you accidentally write:

if (x = 5) // ERROR!

— the compiler will yell at you, and that's totally fair.

7. Working with strings in conditions

You can compare not just numbers, but strings too:

string password = Console.ReadLine();
if (password == "qwerty")
{
    Console.WriteLine("You're in!");
}

Heads up: comparing strings with == in C# works fine for normal cases, since it compares the contents of the strings (not just object references). We'll talk more about the details of string comparison when we get to string methods and localization stuff.

8. Features and nuances:

Curly braces: do you need them or not?

If there's only one command after if or else, you can skip the curly braces:

if (age > 18)
    Console.WriteLine("You're over 18!");

// But it's safer to ALWAYS write them:
if (age > 18)
{
    Console.WriteLine("You're over 18!");
}

Tip: always use braces. It'll save you from a ton of bugs, especially when you expand your code later.

Logic bugs: be careful with your conditions!

If you write this:

if (age > 18)
    Console.WriteLine("You're over 18!");
    Console.WriteLine("Welcome!");

The compiler will read this code like:

if (age > 18)
{
    Console.WriteLine("You're over 18!");
}

Console.WriteLine("Welcome!");

The second line will always run — you need braces to make both lines part of the "branch".

2
Task
C# SELF, level 3, lesson 1
Locked
Temperature Classification
Temperature Classification
2
Task
C# SELF, level 3, lesson 1
Locked
Password Check
Password Check
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION