Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

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 6

Mistakes 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

City.javaJava
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();    }}
Sample output
City: Pune
With ==: false
With equals(): true

What this code does

  • The user typed Pune, but == still says false: 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.

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

ReportCard.javaJava
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();    }}
Sample output
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 >= 75 and pass >= 60, so the grade is B, and case 'A', 'B' gives "Well done".

Finish up

Mark complete & continue

Next: Must-do problems: Conditional Statements