Exercise 2 of 10 · Conditionals
Spelling Test Pass Checker
What you will make
A program that takes a student's spelling test score and prints whether they passed.
The one new idea: Branching with if/else based on a comparison
Programs constantly need to make decisions — pass or fail, eligible or not — and if/else is how code chooses between paths based on a condition.
Go straight to the code ↓Programs often need to choose between two different responses depending on some condition — pass or fail, eligible or not. In C#, an if statement runs its block only when its condition (a bool expression) is true; an optional else block runs when that same condition is false. Conditions are built with comparison operators: == (equal), != (not equal), <, >, <=, >=. It's easy to mix up = (assignment) with == or another comparison operator — and in C#, that mistake usually shows up immediately as a compiler error, because if requires a bool, and assigning an int doesn't give you one. This kind of check comes up constantly, anywhere a program needs to decide between two outcomes based on a number it was handed.
Worked example
Here's a food truck order checked against a delivery minimum:
double orderTotal = 18.50;
double deliveryMinimum = 15.00;
Console.WriteLine($"Order total: {orderTotal}");
if (orderTotal >= deliveryMinimum)
{
Console.WriteLine("Delivery: Eligible");
}
else
{
Console.WriteLine("Delivery: Add more to qualify");
}Order total: 18.5
Delivery: EligibleThe condition orderTotal >= deliveryMinimum compares two numbers and produces a bool, which is exactly what if needs to decide which block to run.
Your turn
The starter code checks a spelling test score against a passing score. Its if condition uses a single = instead of a comparison operator, so it's trying to assign passingScore into score right inside the condition — which doesn't compile, because the result of an assignment isn't automatically a bool here. Fix the condition so it correctly checks whether score is greater than or equal to passingScore.
If something goes wrong
If you get a compiler error like "cannot implicitly convert type 'int' to 'bool'", that's the assignment-instead-of-comparison bug — look for a lone = inside an if (...) and change it to a real comparison operator. If the program compiles but prints the wrong result, check the direction of your comparison, since >= and <= are easy to flip by accident and both compile just fine.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing = instead of == (or a proper comparison operator) inside a condition.
- A single = assigns a value and, for non-bool types like int, won't even compile inside an if statement's parentheses.
- Getting the comparison direction backwards, such as using <= when >= was intended.
- This silently flips which cases count as passing, without any compiler error to catch it.
- Assuming the else branch only runs for some specific 'opposite' case.
- else always runs whenever the if condition is false — there's no in-between state to account for.
Want a blank editor instead? Open the C# playground.