CCEA A-Level Software Systems Development CL4
Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures
Exception Handling and Managing Input and Output
Testing an Object Oriented Application
Graphical User Interface Objects and Events
Multiple Forms and Linking to Files
Designing, Testing and Evaluating an Event Driven Application
Reasons for Systems Development and Systems Methodologies
Managing Projects
Testing Strategies
Database Concepts and Entity Relationship Modelling
Normalisation and Structured Query Language
Project Management and the Systems Approach in Practice
Defining Requirements and Documenting the Design
Building a Desktop Solution with a Database
Testing and Evaluating the Solution
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.
What is a nested IF?
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
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
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
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
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
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
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.
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.