AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Easynested loopscontinue

~75s

Skipping the diagonal

What is printed?

Integer i, j, k
Set k = 0
For i = 1 to 3
    For j = 1 to 3
        If i == j Then
            Continue
        End If
        Set k = k + 1
    End For
End For
Print k
Show the answer and the worked solution

Correct answer: B.

6

Worked solution

  1. The nested loops generate 3 × 3 = 9 pairs (i, j).
  2. Continue skips the increment whenever i equals j, which happens for (1,1), (2,2) and (3,3) — 3 pairs.
  3. The counter therefore advances 9 - 3 = 6 times and 6 is printed.

Answer: 6.

The idea

Continue skips the rest of the current iteration but not the loop itself, so counting the total and subtracting the skipped cases is the quickest route.

Shortcut. The counter moves once for each ordered pair of different values from {1, 2, 3}: 3 × 2 = 6.

Common trap. 9 reads Continue as "carry on to the next line", so the increment would run for all 3 × 3 pairs; in fact it skips the rest of that pass.

Skipping the diagonal — Pseudocode Aptitude Question

Skipping the diagonal: What is printed? Answer and worked solution on CodeKairo.

Question

What is printed?

Integer i, j, k
Set k = 0
For i = 1 to 3
    For j = 1 to 3
        If i == j Then
            Continue
        End If
        Set k = k + 1
    End For
End For
Print k

Options

  1. 3
  2. 6
  3. 9
  4. 12
Show the answer and the worked solution

Correct answer: B. 6

Worked solution

  1. The nested loops generate 3 × 3 = 9 pairs (i, j).
  2. Continue skips the increment whenever i equals j, which happens for (1,1), (2,2) and (3,3) — 3 pairs.
  3. The counter therefore advances 9 - 3 = 6 times and 6 is printed.

Answer: 6.

The idea

Continue skips the rest of the current iteration but not the loop itself, so counting the total and subtracting the skipped cases is the quickest route.

Shortcut. The counter moves once for each ordered pair of different values from {1, 2, 3}: 3 × 2 = 6.

Common trap. 9 reads Continue as "carry on to the next line", so the increment would run for all 3 × 3 pairs; in fact it skips the rest of that pass.

All Pseudocode questions · placement mock tests