<- Back to journalThis Week’s Programming Lesson: C# Lesson 4

This Week’s Programming Lesson: C# Lesson 4

Learn how C# programs make decisions using if, else if, and else, then combine conditionals with the variables, user input, and math from previous lessons.

Tech / Development / Oct 6, 2026 / Roguegoose1

This Week’s Programming Lesson: C# Lesson 4

Making Decisions With If, Else If, and Else

So far, our programs have been pretty obedient.

We tell them what to do, and they do it.

Every single time.

Last week we took numbers stored in variables and started doing some math with them.

This week we are going to make the computer choose what to do based on the information it has.

This is where things start getting more interesting.

We are learning:

if
else if
else

These let us basically tell the computer:

"If this is true, do this. Otherwise, do something else."

Start With If

Let's start extremely simple.

Console.Write("What is your score? ");
int score = int.Parse(Console.ReadLine());

if (score >= 100)
{
    Console.WriteLine("You win!");
}

Run it.

Enter:

150

You should see:

You win!

Run it again and enter:

50

Nothing happens.

Why?

Because the code inside the braces only runs if this is true:

score >= 100

Read It Like English

Again, we are going to read our code like English.

if (score >= 100)

Means:

"If score is greater than or equal to 100..."

Then:

{
    Console.WriteLine("You win!");
}

Means:

"...print You win."

Put it together:

"If the score is greater than or equal to 100, print You win."

That is an if statement.

Comparison Operators

We need a few new symbols.

==
!=
>
<
>=
<=

These are comparison operators.

== means equal to.

!= means not equal to.

> means greater than.

< means less than.

>= means greater than or equal to.

<= means less than or equal to.

One thing to pay attention to:

=

and

==

do not mean the same thing.

We use:

int score = 100;

to assign 100 to score.

But:

score == 100

asks whether score is equal to 100.

One equals sign gives something a value.

Two equals signs compare values.

You will eventually mess this up.

Everybody does.

Fix it and move on.

Add Else

Right now our program only does something when the player wins.

Let's fix that.

Console.Write("What is your score? ");
int score = int.Parse(Console.ReadLine());

if (score >= 100)
{
    Console.WriteLine("You win!");
}
else
{
    Console.WriteLine("Keep trying.");
}

Now one of those two things has to happen.

If the score is at least 100:

You win!

Otherwise:

Keep trying.

Read it like English:

"If the score is at least 100, say You win. Otherwise, say Keep trying."

Add Else If

Sometimes we need more than two choices.

Let's create some score levels.

Console.Write("What is your score? ");
int score = int.Parse(Console.ReadLine());

if (score >= 1000)
{
    Console.WriteLine("Gold Rank");
}
else if (score >= 500)
{
    Console.WriteLine("Silver Rank");
}
else if (score >= 100)
{
    Console.WriteLine("Bronze Rank");
}
else
{
    Console.WriteLine("Unranked");
}

Now the computer checks the conditions from top to bottom.

If your score is 1200, you get:

Gold Rank

If your score is 700:

Silver Rank

If your score is 250:

Bronze Rank

If your score is 50:

Unranked

Order Matters

Look at this:

if (score >= 1000)
{
    Console.WriteLine("Gold Rank");
}
else if (score >= 500)
{
    Console.WriteLine("Silver Rank");
}

C# checks the first condition first.

Once it finds a true condition in this chain, it runs that section and skips the rest.

That means order matters.

For example, this would be bad:

if (score >= 100)
{
    Console.WriteLine("Bronze Rank");
}
else if (score >= 500)
{
    Console.WriteLine("Silver Rank");
}
else if (score >= 1000)
{
    Console.WriteLine("Gold Rank");
}

If somebody scores 1500, the first condition is already true:

score >= 100

So they get Bronze.

Not exactly what we wanted.

This is a good example of something being completely valid C# while still being wrong.

The program did exactly what we told it to do.

We just told it to do something dumb.

Welcome to programming.

Let's Bring Back Bool

Remember bool from Lesson 2?

A Boolean can only be:

true

or:

false

That fits perfectly with if.

Console.Write("Is the game over? ");
bool isGameOver = bool.Parse(Console.ReadLine());

if (isGameOver == true)
{
    Console.WriteLine("Game Over");
}
else
{
    Console.WriteLine("Keep playing.");
}

If the user enters:

true

The game is over.

If they enter:

false

Keep playing.

We can actually simplify that later, but for right now I want the comparison to be obvious.

Let's Combine Some Stuff From Previous Lessons

Now let's make our little program slightly more useful.

Console.Write("What is your player name? ");
string playerName = Console.ReadLine();

Console.Write("How many enemies did you defeat? ");
int enemies = int.Parse(Console.ReadLine());

Console.Write("How many points is each enemy worth? ");
int pointsPerEnemy = int.Parse(Console.ReadLine());

int score = enemies * pointsPerEnemy;

Console.WriteLine(playerName + " scored " + score + " points.");

if (score >= 1000)
{
    Console.WriteLine("Gold Rank");
}
else if (score >= 500)
{
    Console.WriteLine("Silver Rank");
}
else if (score >= 100)
{
    Console.WriteLine("Bronze Rank");
}
else
{
    Console.WriteLine("Unranked");
}

Look at how much more our program does now.

We ask for information.

We store the information.

We perform math using that information.

Then we make a decision based on the result.

That is starting to look a lot more like actual programming.

Put the Decision in a Function

Let's keep building our habit of separating pieces of our program.

string playerName = GetPlayerName();
int enemies = GetEnemies();
int pointsPerEnemy = GetPointsPerEnemy();

int score = CalculateScore(enemies, pointsPerEnemy);

PrintScore(playerName, score);
PrintRank(score);

static string GetPlayerName()
{
    Console.Write("What is your player name? ");
    return Console.ReadLine();
}

static int GetEnemies()
{
    Console.Write("How many enemies did you defeat? ");
    return int.Parse(Console.ReadLine());
}

static int GetPointsPerEnemy()
{
    Console.Write("How many points is each enemy worth? ");
    return int.Parse(Console.ReadLine());
}

static int CalculateScore(int enemies, int pointsPerEnemy)
{
    return enemies * pointsPerEnemy;
}

static void PrintScore(string playerName, int score)
{
    Console.WriteLine(playerName + " scored " + score + " points.");
}

static void PrintRank(int score)
{
    if (score >= 1000)
    {
        Console.WriteLine("Gold Rank");
    }
    else if (score >= 500)
    {
        Console.WriteLine("Silver Rank");
    }
    else if (score >= 100)
    {
        Console.WriteLine("Bronze Rank");
    }
    else
    {
        Console.WriteLine("Unranked");
    }
}

Again, do not worry about memorizing every concept in this program.

Look at the flow.

We get information.

We calculate something.

Then:

PrintRank(score);

sends the score into our PrintRank function.

That function checks the score and decides what to print.

Screw Around With It

You know the deal by now.

Change things.

Make Gold Rank require 5000 points.

Add a Platinum Rank.

Make anything below zero say:

Invalid score

Try:

if (score < 0)
{
    Console.WriteLine("Invalid score");
}

Make a program that asks somebody's age.

if (age >= 18)
{
    Console.WriteLine("Adult");
}
else
{
    Console.WriteLine("Minor");
}

Make a temperature checker.

Make a grade calculator.

Make a health checker for a game.

It really does not matter what the program is about.

We are trying to burn the pattern into your brain:

if (condition)
{
    // Do something
}
else
{
    // Do something else
}

Our Exercise for the Week

Start with the smallest possible version.

Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());

if (number >= 10)
{
    Console.WriteLine("10 or higher");
}
else
{
    Console.WriteLine("Less than 10");
}

Type it from scratch.

Run it.

Change 10 to something else.

Then build an else if version.

Then rebuild the score and rank program.

Do not copy and paste the whole thing every time.

The whole point of this series is getting comfortable enough with the syntax that you can start writing some of it without constantly looking it up.

Break it.

Forget a brace.

Use one equals sign instead of two.

Move your conditions into the wrong order.

See what happens.

Then fix it.

That's It for This Week

We have come quite a ways from:

Console.WriteLine("Hello, World!");

We can now ask the user for information, store different kinds of values, perform calculations, and make decisions based on the results.

That is enough for this week.

Keep typing it.

Keep changing it.

Keep breaking it.

Next time we can start letting our program repeat things on purpose.

That means loops.

This Week’s Programming Lesson: C# Lesson 4 | Goose Journal