levo

Program Control Structures

The switch statement

  • 3 things to know
  • AS Unit 1

Overview

Switch statement, expression does a switch test and fall-through in a switch.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

switch (month) { case 4: case 6: case 9: case 11: days = 30; break; } — what does this fragment achieve?

Key term

Why must case labels be constants, and why must they be distinct?

Key term

What kind of expression does a switch test?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a switch statement?

    Model answer

    A switch statement is a multi-way selection that compares one expression against a list of constant case labels and runs the statements belonging to the matching label.

  2. 2

    What kind of expression does a switch test?

    Model answer

    A switch tests a single expression whose value is compared for equality against each case constant, rather than a boolean condition.

  3. 3

    Give the general form of a switch statement in Java.

    Model answer

    The general form is switch (expression) { case constant: statements; break; ... default: statements; }, with one case label for each value handled.

  4. 4

    What does break do inside a switch statement?

    Model answer

    A break ends the switch immediately, so execution continues after the closing brace instead of running the statements of the following cases.

  5. 5

    What is fall-through in a switch?

    Model answer

    Fall-through is the continuation of execution into the following case when no break ends the current one, which is a defect when accidental and useful when several values are meant to share one action.

  6. 6

    int day = 3; switch (day) { case 1: System.out.println("Mon"); break; case 2: System.out.println("Tue"); break; case 3: System.out.println("Wed"); break; default: System.out.println("Other"); } — what does this fragment print?

    Model answer

    The value 3 matches the third case, so Wed is printed and the break then leaves the switch, so no further case runs.

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.