levo

Program Control Structures

Nested IF and multi-way selection

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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