Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Loops
What will be the output of this Swift code?
var i = 1
while i < 4 {
    print(i)
    i += 1
}
A1 2 3 4
B2 3 4
C1 2 3
D0 1 2 3
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    Starts with i=1, prints 1, increments to 2; prints 2, increments to 3; prints 3, increments to 4; loop stops because 4 is not less than 4.
  2. Step 2: List printed values

    Printed values are 1, 2, and 3.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Loop prints 1 to 3 = 1 2 3 [OK]
Quick Trick: Check loop condition before each iteration [OK]
Common Mistakes:
  • Including 4 in output mistakenly
  • Starting count from 0 instead of 1

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes