arrow
arrow
arrow
Start Read n Initialize i to 1. while i < n do: Write i Increment i end while Stop. Which of the following options is CORRECT?
Question

Start
Read n
Initialize i to 1.
while i < n do:
Write i
Increment i
end while
Stop.
Which of the following options is CORRECT?

A.

Read number n and print the integers counting up to (n-1).

B.

Read number n and print the integers counting up to i.

C.

Read number n and print the integers counting up to (n+1).

D.

Read number n and print the integers counting up to n.

Correct option is A

The variable i starts with the value 1. The loop runs while i < n, meaning it stops when i becomes equal to n. Therefore, the values printed will be:

i=1,2,3,……,(n-1)

Since the condition i < n fails at i = n, n is not printed.

Hence, the program prints all integers from 1 to (n – 1).

Important Key Points:

  1. In while loops, the condition is checked before execution, so the final value that violates the condition is not included.
  2. When a counter starts from 1 and runs while i < n, the total printed numbers are (n – 1).
  3. To include n, the condition must be written as i ≤ n instead of i < n.

Knowledge Booster:

  • Counting up to i: Incorrect — i changes in each iteration; the final value is not constant.
  • Counting up to (n + 1): Incorrect — loop never runs for i = n or beyond.
  • Counting up to n: Incorrect — since i = n is not printed due to the < n condition.

Free Tests

Free
Must Attempt

UP SI 2025 Full Mock Test : 01

languageIcon English
  • pdpQsnIcon160 Questions
  • pdpsheetsIcon400 Marks
  • timerIcon120 Mins
languageIcon English
Free
Must Attempt

UP Police Constable Full Mock Test 01

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon300 Marks
  • timerIcon120 Mins
languageIcon English
Free
Must Attempt

Government Policies and Scheme

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon5 Mins
languageIcon English
test-prime-package

Access ‘UPSSSC’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow