Common mistakes and practice
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most condition bugs come from a handful of small slips. Find them fast, and pick the right tool for each decision: if, if-else, an else-if ladder or switch.
Spot the bug
Each program below has one wrong line: it does not compile, or it compiles and does the wrong thing. Find the line before you look at the answer.
These six slips cause most of the condition bugs beginners meet. Once you have seen them, you will notice them in your own code.
Find the bug
Find the wrong line
Select the line that is wrong.
This should print "Full marks" when marks is 100. It does not compile.
Round 1 of 6Mistakes and their fixes
When a condition misbehaves, the error message or the wrong result usually points to one known slip. The most surprising one: == on text says false for the same word.
Example 1 · == on text gives the wrong answer
import java.util.Scanner;
public class City { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("City: "); String city = sc.next(); System.out.println("With ==: " + (city == "Pune")); System.out.println("With equals(): " + city.equals("Pune")); sc.close(); }}City: Pune With ==: false With equals(): true
What this code does
- The user typed
Pune, but==still saysfalse: it compares objects, not letters. equals()compares the letters, so it gives the right answer.
Match what you see to a line below; each gives the fix and the lesson that teaches it.
incompatible types: int cannot be converted to boolean→ compare with==, not=. Review: Comparison operators (opens in a new tab)==on text saysfalsefor the same word → useequals(). Review: Comparison operators (opens in a new tab)- The block runs every time → remove the
;afterif (...). Review: The if statement (opens in a new tab) - A line runs when it should not, or
'else' without 'if'→ put braces around every block. Review: The if statement (opens in a new tab) - A wrong grade → put the strictest check first. Review: The else-if ladder (opens in a new tab)
- A range check is always
true→ join the two ends with&&, not||. Review: Combine conditions (opens in a new tab) cannot find symbolafter a block → create the variable before theif. Review: Nested if (opens in a new tab)- Extra cases print → add
break;or use->. Review: The switch statement (opens in a new tab) does not cover all possible input values→ adddefault ->. Review: switch with arrows (opens in a new tab)
Pick the right tool
Before you write a condition, ask what kind of decision it is. Is it one yes/no question, a choice of two paths, a set of ranges, or one value matched against fixed choices?
- Something happens only sometimes →
if - Exactly one of two paths →
if-else - Ranges, or different checks in order → else-if ladder
- One value against fixed choices →
switch(the arrow form when you store a value)
Classify
Which tool fits?
Tap a task, then tap the tool that fits it best.
Example 2 · One program, every tool
import java.util.Scanner;
public class ReportCard { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Attendance %: "); int attendance = sc.nextInt(); System.out.print("Marks: "); int marks = sc.nextInt(); if (marks < 0 || marks > 100) { System.out.println("Invalid marks"); } else if (attendance < 75) { System.out.println("Detained: attendance below 75%"); } else { char grade; if (marks >= 75) { grade = 'A'; } else if (marks >= 60) { grade = 'B'; } else if (marks >= 35) { grade = 'C'; } else { grade = 'F'; } String remark = switch (grade) { case 'A', 'B' -> "Well done"; case 'C' -> "Passed, keep practising"; default -> "Re-exam in July"; }; System.out.println("Grade " + grade + ": " + remark); } sc.close(); }}Attendance %: 82 Marks: 68 Grade B: Well done
What this code does
- The outer ladder rules out bad input first (
||), then the attendance shortage; only then does the grading run. - Inside, a ladder turns the marks into a grade letter, and a switch expression turns the letter into a remark.
- 68 marks fail
>= 75and pass>= 60, so the grade is B, andcase 'A', 'B'gives "Well done".
Finish up
Mark complete & continue
Next: Must-do problems: Conditional Statements