levo

Program Control Structures

Nested IF and multi-way selection

  • 3 things to know
  • AS Unit 1

Overview

Nested IFs and else-if ladders traced through worked Java fragments, ordering the conditions correctly, the dangling else, combining conditions with AND, counting paths for testing, and when a nested IF is clearer than a switch.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a dangling else? Explain which IF the else belongs to in: if (a > 0) if (b > 0) System.out.println("both"); else System.out.println("other");.

Key term

What is a nested IF?

Key term

int mark = 85; if (mark >= 40) { System.out.println("Pass"); } else if (mark >= 70) { System.out.println("Distinction"); } — what is the defect?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a nested IF?

    Model answer

    A nested IF is an IF statement written inside the branch of another IF, so the inner condition is tested only when the outer one has already decided a particular way.

  2. 2

    int mark = 72; if (mark >= 70) { System.out.println("A"); } else if (mark >= 60) { System.out.println("B"); } else { System.out.println("C"); } — what does this fragment print?

    Model answer

    The first condition is true because 72 is at least 70, so A is printed and the remaining branches are skipped entirely.

  3. 3

    int mark = 65; if (mark >= 70) { System.out.println("A"); } else if (mark >= 60) { System.out.println("B"); } else { System.out.println("C"); } — what is printed, and which path is taken?

    Model answer

    The first condition fails because 65 is below 70, the second succeeds because 65 is at least 60, so B is printed.

  4. 4

    In an else-if ladder testing mark >= 70 then mark >= 60, why does the second branch not need to test mark < 70 as well?

    Model answer

    The second branch is only reached when the first condition has already been found false, so a mark below 70 is guaranteed and repeating the test would add nothing.

  5. 5

    int mark = 85; if (mark >= 40) { System.out.println("Pass"); } else if (mark >= 70) { System.out.println("Distinction"); } — what is the defect?

    Model answer

    The output is Pass, because 85 satisfies the first condition and the ladder stops there; the Distinction branch can never be reached, and the tests must be ordered from the most restrictive downwards.

  6. 6

    What is a dangling else? Explain which IF the else belongs to in: if (a > 0) if (b > 0) System.out.println("both"); else System.out.println("other");

    Model answer

    A dangling else is an else that could belong to more than one IF by indentation; the language pairs it with the nearest unmatched IF, so the else belongs to the inner test on b, and braces are needed to attach it to the outer one.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Program Control Structures, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.