Multiple choice technology web technology

How does a "while" loop start?

  1. while (i<=10)

  2. while (i<=10;i++)

  3. while i=1 to 10

  4. while (i=0;i<=10;i++)

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

A while loop in C-style syntax requires a condition in parentheses: while (condition). Option B incorrectly adds i++ which is for-loop syntax, and options C and D use Pascal/BASIC style (for i=1 to 10) or full for-loop syntax respectively.

AI explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) while (i<=10) - This option is correct because it starts the "while" loop with the condition that the loop will continue as long as the variable "i" is less than or equal to 10.

Option B) while (i<=10;i++) - This option is incorrect because it uses the semicolon (;) inside the parentheses, which is not the correct syntax for a "while" loop.

Option C) while i=1 to 10 - This option is incorrect because it uses the assignment operator (=) instead of the comparison operator (<=) to set the condition for the loop.

Option D) while (i=0;i<=10;i++) - This option is incorrect because it uses the assignment operator (=) instead of the comparison operator (<=) to set the initial value of "i" and the condition for the loop.

The correct answer is Option A. This option is correct because it correctly starts the "while" loop with the condition that the loop will continue as long as the variable "i" is less than or equal to 10.