If and else decisions
An if statement runs a block only when its condition is true. Add else to handle the remaining case, or else if to test another condition. Branching lets one program respond differently to different input values.
int score = 60;if (score >= 60){ System.Console.WriteLine("Pass");}else{ System.Console.WriteLine("Retry");}The comparison includes 60, so only the first block runs.
Expected output (reasoned, not executed):
PassOne decision at a time
Write if (condition) { ... } with a bool expression inside parentheses. The braces form a block, which groups statements and introduces a local scope. An else block runs only when the associated if condition is false. Use braces even for one statement to make later changes safer.
Order branches by priority
In an if / else if / else chain, tests run from top to bottom until one succeeds. Later branches are skipped. Validate out-of-range input before classifying it. Independent if statements are different: more than one can run, which is useful when rules are not mutually exclusive.
Test the boundaries
A rule such as passing at 60 means score >= 60, not score > 60. Test 59, 60 and 100 plus invalid values such as -1 and 101. A local declared only inside a branch is not visible outside its braces. When assigning an existing variable in branches, ensure it is assigned on every path before reading it.
Examples and walkthroughs
int score = 101;if (score < 0 || score > 100){ System.Console.WriteLine("Invalid");}else if (score >= 60){ System.Console.WriteLine("Pass");}else{ System.Console.WriteLine("Retry");}Invalid inputs take priority. A score of 101 cannot accidentally pass.
Expected output (reasoned, not executed):
Invalidint books = 12;if (books >= 10){ System.Console.WriteLine("Bulk order");}if (books % 5 != 0){ System.Console.WriteLine("Partial carton");}Both conditions are true and the two independent if statements both run.
Expected output (reasoned, not executed):
Bulk orderPartial cartonCommon mistake and correction
An else if chain is not a set of independent checks. Putting score >= 60 before score > 100 incorrectly accepts 101. Also avoid a stray semicolon after if (condition), which creates an empty controlled statement.
Try it yourself
Classify a score from 0 to 100 as Invalid outside that range, Distinction at 90 or above, Pass at 60 or above, and Retry otherwise. Trace -1, 59, 60, 89, 90, 100 and 101.
Next: Switch statements and expressions. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
If and else decisions — knowledge check
Apply the lesson concepts to the following scenarios.
Which comparison includes a passing score of exactly 60?
How many branches of an if / else if / else chain execute?
Two separate if statements have true conditions. What happens?
Why check score > 100 before score >= 60?
A variable is declared inside the braces of an if block. Can code after the block refer to it by that name?