Skip to content
Computer ScienceISC Class 11 · Chapter 8

Statements and Scope in Java: ISC Class 11 Notes

Updated 15 October 2026 · Fact-checked

Statements control what a Java program does and in what order: conditional statements (if, if-else, if-else-if, switch, the ternary operator) choose between actions, and loops (for, while, do-while) repeat them, with break and continue. ISC Class 11 Computer Science also covers blocks and the scope and visibility of variables.

What Statements and Scope is about

A program is more than a list of formulas: it must make decisions and repeat work. This chapter covers the statements that control the flow of a Java program, and the rules that decide where a variable can be used (its scope).

The CISCE 2028 syllabus (Section B, topic 8) asks for: statements; conditional statements (if, if else, if else if, switch case), the ternary operator; looping (for, while, do while), continue and break; grouping statements in blocks; scope and visibility of variables; the semantics of conditional and looping statements and the evaluation of conditions; nesting of blocks; block, method and class scope; and visibility rules for variables with the same name in different scopes.

Four programs on this page were compiled and run: a grade calculator with switch, a loop showcase, a scope demonstration and an Armstrong-number finder.

Weightage: Statements and Scope is topic 8 of the ISC Class XI Computer Science syllabus, in Section B - Java. CISCE weights Section B at 30 of the 70 marks in Paper I (Theory). Class XI is examined by the school, so your school sets the paper within this weightage.Source: CISCE ISC Computer Science syllabus, examination year 2028, Class XI unit weightage

Key concepts

Statements and blocks

A statement is a complete instruction. Kinds:

1. Declaration statements: int n = 5;
2. Expression statements: assignments, increments, method calls: n++; System.out.println(n);
3. Control statements: conditionals (if, switch), loops (for, while, do-while), jumps (break, continue, return).

A block (compound statement) is a group of statements enclosed in braces { }; it can appear wherever a single statement can. Blocks are needed when an if or a loop must control more than one statement:

if (marks >= 40)
{
    passed++;
    System.out.println("Pass");
}

Without braces, only the first statement belongs to the if; the second always runs. Nesting: blocks can contain other blocks (a loop inside an if inside a method).

Empty statement: a lone semicolon. Beware: for (i = 0; i < 5; i++); { ... } runs an empty loop five times, then the block once.

Worked example

What is printed by: int c = 0; for (int i = 1; i <= 3; i++); c++; System.out.println(c);? [1 mark]

Try it first, then show the solutionSolution
  1. The semicolon after the for header makes the loop body empty; c++ runs once after the loop.

Answer: 1.

Conditional statements: if, if-else, if-else-if

if: runs a statement (or block) only when the condition is true.

if-else: chooses between two alternatives.

if-else-if ladder: tests conditions in order; the first true condition's block runs and the rest are skipped; a final else handles all remaining cases.

Nested if: an if inside another if. Dangling else rule: an else belongs to the nearest unmatched if; use braces to make the intent clear.

Evaluation of conditions: the condition must be a boolean expression (Java does not treat 0 as false). && and || short-circuit.

The grade method below uses an if-else-if ladder; because conditions are tested from the top, marks ≥ 75 need not also test < 90.

// if, if-else, if-else-if ladder, switch (with and without break) and the ternary operator.
public class Conditionals
{
    static char grade(int marks)
    {
        if (marks >= 90)
            return 'A';
        else if (marks >= 75)
            return 'B';
        else if (marks >= 60)
            return 'C';
        else if (marks >= 40)
            return 'D';
        else
            return 'E';
    }

    static String dayName(int d)
    {
        String name;
        switch (d)
        {
            case 1: name = "Monday"; break;
            case 2: name = "Tuesday"; break;
            case 6:
            case 7: name = "Weekend"; break;        // two cases share one block
            default: name = "Midweek";
        }
        return name;
    }

    public static void main(String[] args)
    {
        int[] marks = {95, 75, 61, 40, 12};
        for (int i = 0; i < marks.length; i++)
            System.out.print(marks[i] + ":" + grade(marks[i]) + " ");
        System.out.println();

        System.out.println(dayName(1) + ", " + dayName(4) + ", " + dayName(7));

        int k = 2;
        switch (k)                              // missing break: fall-through
        {
            case 1: System.out.print("one ");
            case 2: System.out.print("two ");
            case 3: System.out.print("three ");
            default: System.out.print("default");
        }
        System.out.println();

        int a = 14, b = 9;
        int bigger = (a > b) ? a : b;           // ternary operator
        String parity = (a % 2 == 0) ? "even" : "odd";
        System.out.println("bigger = " + bigger + ", " + a + " is " + parity);
    }
}

Output:

95:A 75:B 61:C 40:D 12:E 
Monday, Midweek, Weekend
two three default
bigger = 14, 14 is even

Worked example

Using grade(), which grade is returned for 74 and for 90, and why does the order of conditions matter? [2 marks]

Try it first, then show the solutionSolution
  1. 74: fails ≥ 90 and ≥ 75, passes ≥ 60 → 'C'. 90: passes the first test → 'A'.
  2. If marks >= 60 were tested first, 90 would wrongly get 'C': in a ladder the first true branch wins.

Answer: 74 → C, 90 → A; conditions must go from the strictest to the weakest.

switch-case and the ternary operator

switch selects one of many branches by matching an expression against constant case labels.

  • The expression may be int, char, short, byte, String (or an enum or a wrapper of these) but not double, float, long or boolean.
  • Case labels must be constants and distinct.
  • break ends the switch; without it, execution falls through into the next case (useful when several cases share code, as with cases 6 and 7 above).
  • default runs when no case matches; it is optional.

In the program above, k = 2 with no breaks printed 'two three default': execution entered at case 2 and fell through every following case.

if-else-if vs switch: switch tests only equality with constants; if-else-if can test ranges and any boolean condition.

Ternary (conditional) operator ?: is an expression that chooses one of two values:

condition ? valueIfTrue : valueIfFalse

int bigger = (a > b) ? a : b;. It can be nested (right-associative), but deep nesting harms readability.

Worked example

Rewrite with the ternary operator: if (n % 2 == 0) s = "even"; else s = "odd";. [1 mark]

Try it first, then show the solutionSolution
  1. Assign the result of the conditional expression.

Answer: s = (n % 2 == 0) ? "even" : "odd";

Loops: for, while, do-while

for: for (initialisation; condition; update) body. Best when the number of repetitions is known. The condition is tested before each iteration.

while: while (condition) body. Entry-controlled: the condition is tested first, so the body may run zero times. Best when repetitions depend on a condition (for example, until a number becomes 0).

do-while: do body while (condition);. Exit-controlled: the body runs at least once, then the condition is tested. Note the semicolon after while (condition).

Parts of a loop: initialisation, test condition, body, update. Forgetting the update (or a wrong condition) gives an infinite loop.

Counting iterations: for (i = 10; i > 0; i −= 3) runs for i = 10, 7, 4, 1: 4 times.

// for, while and do-while loops, with break and continue.
public class Loops
{
    public static void main(String[] args)
    {
        // for: a known number of repetitions
        int sum = 0;
        for (int i = 1; i <= 10; i++)
            sum += i;
        System.out.println("1 + 2 + ... + 10 = " + sum);

        // while: repeat while a condition holds (count the digits)
        int n = 90817, digits = 0;
        while (n > 0)
        {
            digits++;
            n /= 10;
        }
        System.out.println("digits in 90817 = " + digits);

        // do-while: body runs at least once
        int x = 50;
        do
        {
            System.out.println("do-while body ran with x = " + x);
            x++;
        } while (x < 10);

        // continue: skip multiples of 3
        System.out.print("1 to 10 without multiples of 3: ");
        for (int i = 1; i <= 10; i++)
        {
            if (i % 3 == 0)
                continue;
            System.out.print(i + " ");
        }
        System.out.println();

        // break: first number above 100 divisible by both 7 and 9
        int found = 0;
        for (int i = 101; ; i++)
        {
            if (i % 7 == 0 && i % 9 == 0)
            {
                found = i;
                break;
            }
        }
        System.out.println("first above 100 divisible by 7 and 9 = " + found);

        // nested loops: a number triangle
        for (int row = 1; row <= 4; row++)
        {
            for (int col = 1; col <= row; col++)
                System.out.print(col);
            System.out.println();
        }
    }
}

Output:

1 + 2 + ... + 10 = 55
digits in 90817 = 5
do-while body ran with x = 50
1 to 10 without multiples of 3: 1 2 4 5 7 8 10 
first above 100 divisible by 7 and 9 = 126
1
12
123
1234

Note that the do-while body ran once even though x = 50 already failed x < 10.

Worked example

How many times does the body run, and what is i afterwards: int i = 1; while (i < 50) i *= 3;? [2 marks]

Try it first, then show the solutionSolution
  1. i: 1 → 3 → 9 → 27 → 81; the test fails at 81.
  2. Body ran 4 times.

Answer: 4 times; i = 81.

break and continue; nested loops

break: immediately exits the innermost loop (or switch); control passes to the statement after it. Used to stop a search once the answer is found.

continue: skips the rest of the current iteration and goes to the next one (in a for loop, the update runs first). Used to skip unwanted values.

Nested loops: a loop inside a loop; the inner loop runs completely for each iteration of the outer loop. Patterns, tables and pairs are typical uses: an outer loop of 4 rows and an inner loop of row columns prints a number triangle (as in the Loops program).

break inside an inner loop exits only the inner loop.

Example of break and continue together: summing odd numbers up to 15 from a loop running to 20: for (k = 1; k <= 20; k++) { if (k % 2 == 0) continue; if (k > 15) break; sum += k; } gives 1 + 3 + ... + 15 = 64.

Worked example

In the Loops program, why is the first number found 126? [1 mark]

Try it first, then show the solutionSolution
  1. Divisible by both 7 and 9 means divisible by 63; multiples are 63, 126, ...; the first above 100 is 126, and break stops the search.

Answer: 126 = 2 × 63 is the first multiple of 63 above 100; break ends the loop there.

Scope and visibility of variables

The scope of a variable is the part of the program where it can be used.

1. Class scope: instance and static variables (fields) declared in a class are visible in all methods of the class.
2. Method scope: parameters and local variables declared in a method are visible from the declaration to the end of the method; they are created when the method is called and destroyed when it returns.
3. Block scope: a variable declared inside a block (including a for-loop header) is visible only inside that block.

Visibility rules for the same name:

  • A local variable or parameter with the same name as a field hides (shadows) the field inside that method; the field is still reached with this.name.
  • Two local variables with the same name cannot be declared where their scopes overlap (a block inside a method cannot redeclare the method's local variable), but the same name can be reused after an earlier block has ended.
// Class scope, method scope and block scope; a local variable hiding a field of the same name.
public class Scope
{
    int count = 100;                   // class scope (instance variable)

    void show()
    {
        int count = 5;                 // method scope: hides the field inside this method
        System.out.println("local count = " + count + ", field count = " + this.count);
        for (int i = 0; i < 2; i++)    // i exists only inside the loop
        {
            int sq = i * i;            // block scope: sq exists only in this block
            System.out.println("i = " + i + ", sq = " + sq);
        }
        // System.out.println(i);      // would not compile: i is out of scope here
    }

    void increase()
    {
        count++;                       // no local count here, so this is the field
    }

    public static void main(String[] args)
    {
        Scope s = new Scope();
        s.show();
        s.increase();
        System.out.println("after increase, field count = " + s.count);
        {
            int temp = 7;              // a block inside main
            System.out.println("temp in block = " + temp);
        }
        int temp = 8;                  // allowed: the earlier temp is out of scope
        System.out.println("temp after block = " + temp);
    }
}

Output:

local count = 5, field count = 100
i = 0, sq = 0
i = 1, sq = 1
after increase, field count = 101
temp in block = 7
temp after block = 8

Worked example

Why can main declare int temp = 8; after a block that declared int temp = 7;? [1 mark]

Try it first, then show the solutionSolution
  1. The first temp has block scope; it ceases to exist at the closing brace, so the name is free again.

Answer: Because the earlier temp's scope ended with its block; the scopes do not overlap.

Statements and Scope in real life

Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

if-else-if ladder

Suppose a cinema charges ₹150 for children under 12, ₹250 for adults, and ₹180 for senior citizens aged 60 and above.

Write the conditions in the right order.

Try it first, then show the solutionSolution
  1. if (age < 12) price = 150; else if (age >= 60) price = 180; else price = 250;
  2. Each age matches exactly one branch; the last else covers 12 to 59.

Answer: An if-else-if ladder with mutually exclusive conditions and a final else.

Memory hook: First true branch wins.

do-while for menus

Imagine an ATM menu that must be shown at least once and repeats until you choose 'Exit'.

Which loop fits, and why?

Try it first, then show the solutionSolution
  1. The menu must appear before any choice is made.
  2. A do-while runs the body first and tests the choice afterwards.

Answer: do-while: the menu is displayed at least once, then repeated while the choice is not Exit.

Memory hook: Do first, ask later.

break in a search

Suppose you are looking through a stack of 100 answer sheets for your own and find it at number 17.

How does break save effort in a program doing the same search?

Try it first, then show the solutionSolution
  1. Without break, the loop checks all 100 sheets.
  2. With break, it stops at 17, after 17 checks.

Answer: break ends the search as soon as the item is found, saving 83 unnecessary checks here.

Memory hook: Found it? Stop looking.

Topics in this chapter

Statements and Scope: syntax and key facts

Iterations of a counting loop
for (i = a; i <= b; i += s): number of iterations = floor((b − a) ÷ s) + 1 (if a ≤ b)
Ternary operator
result = condition ? valueIfTrue : valueIfFalse;
switch skeleton
switch (expr) { case c1: ... break; case c2: ... break; default: ... }
Loop types
for and while: entry-controlled (0 or more runs); do-while: exit-controlled (1 or more runs)
Scope order
Class scope ⊃ method scope ⊃ block scope; inner name hides outer; this.name reaches the field

Statements and Scope: diagrams to practise

Practise each one on paper: draw it from the checklist, then tick off every label.

Flowcharts of the three loops

Explain the difference between entry- and exit-controlled loops.

Draw

  1. while/for: Start → condition diamond → (true) body → update → back to condition; (false) → exit.
  2. do-while: Start → body → condition diamond → (true) back to body; (false) → exit.
  3. Label the first two 'entry-controlled' and the third 'exit-controlled'.

Label

  • condition
  • body
  • update
  • entry-controlled
  • exit-controlled

Watch out: The position of the diamond (before or after the body) is the whole difference.

Scope boxes

Explain class, method and block scope.

Draw

  1. Outer box 'class Scope' with field count = 100.
  2. Inside it, a box 'method show()' with local count = 5 (shading the field).
  3. Inside that, a box 'for block' with i and sq.
  4. Arrows show that inner boxes can see outer names unless hidden; outer boxes cannot see inner names.

Label

  • class scope
  • method scope
  • block scope
  • hiding

Watch out: Nested boxes make visibility rules obvious.

How the school exam asks Statements and Scope

The question patterns to expect, and the method that works for each.

Question typeHow it is askedMethod
Output and tracing (1-5 marks)Predict output of loops, switch with or without break, nested if; count iterations.Make a trace table of variables for each iteration.
Conversion and correction (2-5 marks)Convert for to while or do-while; if-else to ternary or switch; find errors in scope.Keep initialisation, condition and update equivalent; check variables are in scope.
Programs (5-10 marks)Number problems (Armstrong, perfect, prime, GCD), patterns, menus.Algorithm first; choose the loop type; use break and continue where they simplify logic.

Tricks to remember Statements and Scope

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • for = know how many; while = know when to stop; do-while = at least once

    Pick the loop by what you know before starting.

    Use it when: Choosing a loop.

  • No break, fall through

    Every case without break slides into the next.

    Use it when: switch behaviour.

  • break leaves, continue skips

    break exits the loop; continue jumps to the next turn.

    Use it when: Jump statements.

  • Braces are borders

    A variable lives only inside the braces where it was born.

    Use it when: Block scope.

Statements and Scope: exam-level solved questions

Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.

Example 1: switch expression types · 1 mark · MCQExam level

Which type can NOT be used as the expression of a switch statement?
(a) int
(b) char
(c) String
(d) double

Try it first, then show the solutionSolution
  1. switch works with int, char, short, byte, String and enums, not floating-point types.

Answer: (d) double.

Example 2: do-while · 1 mark · Assertion-reasonHarder exam level

Assertion: A do-while loop executes its body at least once.
Reason: The condition of a do-while loop is tested after the body.
(a) Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
(b) Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
(c) Assertion is true but Reason is false.
(d) Both Assertion and Reason are false.

Try it first, then show the solutionSolution
  1. The test comes after the body, so the body runs before any check: the Reason explains the Assertion.

Answer: (a)

Example 3: Fall-through output · 2 marks · Short answerExam level

Give the output when k = 2: switch (k) { case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); default: System.out.print("default"); }

Try it first, then show the solutionSolution
  1. Execution enters at case 2; there are no breaks.
  2. It falls through case 3 and default.

Answer: two three default (as the compiled Conditionals program printed).

Example 4: Convert loops · 5 marks · Short answerHarder exam level

Convert for (int i = 10; i > 0; i -= 3) System.out.print(i + " "); into a while loop and a do-while loop, give the output, and state a case where the do-while version would behave differently.

Try it first, then show the solutionSolution
  1. while: int i = 10; while (i > 0) { System.out.print(i + " "); i -= 3; }
  2. do-while: int i = 10; do { System.out.print(i + " "); i -= 3; } while (i > 0);
  3. Output (all three): 10 7 4 1 (4 iterations).
  4. Difference: if i started at 0, the for and while loops print nothing, but the do-while prints '0 ' once.
  5. Because do-while is exit-controlled.

Answer: Equivalent while and do-while loops print 10 7 4 1; they differ when the starting value already fails the condition (do-while still runs once).

Example 5: Trace break and continue · 5 marks · Short answerHOTS / competency

Trace and give the final sum: int sum = 0; for (int k = 1; k <= 20; k++) { if (k % 2 == 0) continue; if (k > 15) break; sum += k; }

Try it first, then show the solutionSolution
  1. Even k: continue skips them (2, 4, ..., 16 never reach the sum).
  2. Odd k from 1 to 15 are added: 1, 3, 5, 7, 9, 11, 13, 15.
  3. k = 17 is odd and > 15: break ends the loop.
  4. Sum = 8 odd numbers, 1 + 3 + ... + 15 = 8² = 64.
  5. Compiled check printed sum 64.

Answer: 64.

Example 6: Armstrong numbers · 10 marks · Long answerHOTS / competency

Write a program that prints all three-digit Armstrong numbers (numbers equal to the sum of the cubes of their digits) and their count. Explain the role of each loop.

Try it first, then show the solutionSolution
  1. Outer for loop runs n from 100 to 999: a known range.
  2. For each n, copy t = n and s = 0; inner while loop extracts digits with t % 10 and t /= 10 until t is 0 (number of digits need not be known).
  3. Add d × d × d to s for each digit d.
  4. If s == n, print n and increase count.
  5. After the loops, print count.
  6. Output (compiled Armstrong program): 153 370 371 407; count = 4.

Answer: See the compiled program:

// Prints all three-digit Armstrong numbers (sum of cubes of digits equals the number)
// and counts them, using nested loops and a while loop.
public class Armstrong
{
    public static void main(String[] args)
    {
        int count = 0;
        for (int n = 100; n <= 999; n++)
        {
            int t = n, s = 0;
            while (t > 0)
            {
                int d = t % 10;
                s += d * d * d;
                t /= 10;
            }
            if (s == n)
            {
                System.out.print(n + " ");
                count++;
            }
        }
        System.out.println();
        System.out.println("count = " + count);
    }
}
Output:
153 370 371 407 
count = 4

Case-study practice (Section D style)

Read the situation, then answer every part. In a school exam set on the board pattern this is a 5-mark question split into 2 + 2 + 1 marks.

Parking charges

Suppose a mall's parking program charges: first 2 hours free; each further hour (or part) ₹30, up to a maximum of ₹200 a day. Vehicle type 'C' (car) pays this rate, 'B' (bike) pays half, and any other code gives an error message. The program uses a switch on the vehicle code and a loop to repeat for several vehicles until the attendant enters 'X'.

  1. (i) · 2 marks

    Write the expression for a car's charge for h hours (whole hours) using Math.min and the ternary operator.

    Answer

    Answer: int charge = (h <= 2) ? 0 : Math.min((h - 2) * 30, 200);

    1 mark condition, 1 mark cap.

  2. (ii) · 2 marks

    Find the charges for a car parked 5 hours and for 12 hours, and for a bike parked 5 hours.

    Answer

    Answer: Car 5 h: (5 − 2) × 30 = ₹90. Car 12 h: 10 × 30 = 300, capped at ₹200. Bike 5 h: 90 ÷ 2 = ₹45.

    2 marks for all three.

  3. (iii) · 1 mark

    Which loop suits 'repeat until the attendant enters X', and why?

    Answer

    Answer: A do-while loop (or while with a priming read): at least one vehicle code must be read before the test, and the loop repeats while the code is not 'X'.

    1 mark.

Where the marks are

How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.

switch fall-through output (2 marks) (2 marks)

StepMarks
Entry at the matching case1
Fall-through to the end shown1

Convert a for loop to while and do-while (5 marks) (5 marks)

StepMarks
Correct while version2
Correct do-while version1
Output1
Case where they differ1

Armstrong number program (10 marks) (10 marks)

StepMarks
Outer loop over the range2
Inner digit-extraction loop on a copy3
Cube sum and comparison2
Count and output2
Explanation of loop choice1

Where students lose marks in Statements and Scope

The slips examiners see most often, and what to write instead.

  • Mistake: Forgetting break in switch

    Execution falls through into the following cases.

    Do this: End each case with break unless fall-through is intended.

    Usually costs: 1-2 marks.

  • Mistake: Semicolon after a for or while header

    It makes an empty loop body; the intended body runs once afterwards.

    Do this: No semicolon before the body's opening brace.

    Usually costs: 1-2 marks.

  • Mistake: Using a loop variable after the loop when it was declared in the for header

    Its scope is only the loop.

    Do this: Declare it before the loop if needed afterwards.

    Usually costs: 1 mark.

  • Mistake: Destroying the original number in digit loops

    After the loop the number is 0, so it cannot be compared or printed.

    Do this: Work on a copy.

    Usually costs: 1 mark.

Practice questions on Statements and Scope

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQExam level

    Which loop is exit-controlled?
    (a) for
    (b) while
    (c) do-while
    (d) all of these

    Hint

    Test after the body.

    Answer

    (c) do-while.

  2. Q2 · 1 mark · Very short answerExam level

    How many times does for (int i = 10; i > 0; i -= 3) run?

    Hint

    List the values.

    Answer

    4 times (i = 10, 7, 4, 1).

  3. Q3 · 2 marks · Short answerExam level

    Distinguish between break and continue with an example each.

    Hint

    Exit vs skip.

    Answer

    break exits the loop immediately (stop searching once a number is found). continue skips the rest of the current iteration and moves to the next (skip multiples of 3 while printing 1 to 10).

  4. Q4 · 2 marks · Short answerHarder exam level

    Write a switch that prints 'vowel' or 'consonant' for a lower-case letter ch, using fall-through.

    Hint

    Stack the vowel cases.

    Answer

    switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("vowel"); break; default: System.out.println("consonant"); } For ch = 'e' it prints vowel.

  5. Q5 · 2 marks · Short answerHarder exam level

    What is y after int x = 5; int y = x > 3 ? x < 8 ? 1 : 2 : 3;? Explain the grouping.

    Hint

    Right-associative.

    Answer

    It groups as x > 3 ? (x < 8 ? 1 : 2) : 3. x > 3 is true, x < 8 is true, so y = 1.

  6. Q6 · 2 marks · Short answerHarder exam level

    Write a loop to print the first 8 Fibonacci numbers starting 0, 1.

    Hint

    Keep two previous terms.

    Answer

    int a = 0, b = 1; for (int k = 1; k <= 8; k++) { System.out.print(a + " "); int c = a + b; a = b; b = c; } Output: 0 1 1 2 3 5 8 13.

  7. Q7 · 2 marks · Short answerHOTS / competency

    Identify the scope error and correct it: for (int i = 0; i < 5; i++) { int sq = i * i; } System.out.println(i + " " + sq);

    Hint

    Block scope.

    Answer

    i (declared in the for header) and sq (declared in the block) are out of scope after the loop, so the println does not compile. Declare int i, sq; before the loop (or print inside the loop). After the corrected loop ends, i = 5 and sq = 16.

  8. Q8 · 5 marks · Short answerHarder exam level

    Write a program segment that checks whether 28 is a perfect number (equal to the sum of its proper divisors) and prints the result.

    Hint

    Divisors less than n.

    Answer

    int n = 28, s = 0; for (int d = 1; d < n; d++) if (n % d == 0) s += d; System.out.println(n + " perfect? " + (s == n)); Divisors 1, 2, 4, 7, 14 sum to 28, so it prints true.

  9. Q9 · 2 marks · Short answerHOTS / competency

    Trace and give the output: int m = 5; do { m -= 2; } while (m > 0); System.out.println(m);

    Hint

    Test after each subtraction.

    Answer

    m: 5 → 3 (test true) → 1 (true) → −1 (false); prints −1.

  10. Q10 · 10 marks · Long answerHOTS / competency

    Write a program to find the GCD of two numbers using a while loop (Euclid's method) and the LCM from it, and give the result for 48 and 36.

    Hint

    gcd(a, b) = gcd(b, a % b); LCM = a × b ÷ GCD.

    Answer

    int a = 48, b = 36, x = a, y = b; while (y != 0) { int t = x % y; x = y; y = t; } int gcd = x; int lcm = a * b / gcd; Trace: (48, 36) → (36, 12) → (12, 0): GCD = 12; LCM = 48 × 36 ÷ 12 = 144. The while loop suits because the number of steps is not known in advance.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Entry-controlled loops?Show answer

    for and while.

  2. 2.What does break do in a switch?Show answer

    Ends the switch, preventing fall-through.

  3. 3.Ternary operator form?Show answer

    condition ? a : b.

  4. 4.Scope of a for-loop variable?Show answer

    Only the loop.

  5. 5.Local variable with a field's name?Show answer

    Hides the field; use this.name for the field.

  6. 6.continue does?Show answer

    Skips to the next iteration.

Best order to study it

  1. Statements and blocks.
  2. if, if-else, ladder, nested if.
  3. switch and the ternary operator.
  4. for, while, do-while.
  5. break, continue, nested loops.
  6. Class, method and block scope; hiding.

Quick revision checklist

The night before the exam: can you do each of these without looking?

  • Block = { } groups statements; without braces only one statement is controlled.
  • if-else-if: first true branch wins; dangling else pairs with nearest if.
  • switch: constant cases, break to stop fall-through, default optional; no double or boolean.
  • for/while test first; do-while tests after (runs at least once).
  • break exits; continue skips; inner loop completes for each outer iteration.
  • Scope: class ⊃ method ⊃ block; inner name hides outer; this.field.

Statements and Scope: frequently asked questions

Can every for loop be written as a while loop?

Yes. Move the initialisation before the while, keep the condition, and put the update at the end of the body. A do-while differs only when the condition is false at the start.

Is fall-through in switch always a mistake?

No. It is useful when several cases share the same code (case 'a': case 'e': ...). It is a mistake only when a break was forgotten.

Can a local variable have the same name as a field?

Yes. Inside that method the local variable hides the field; use this.name to refer to the field. Two local variables with overlapping scopes cannot share a name.

Based on: CISCE ISC 2028 Computer Science syllabus (Classes XI and XII); CISCE Regulations for the ISC Examination Year 2028 (Class XI internal examination)