How many stars will the following code print to the monitor?

How many stars will the following code print to the monitor?

i = 0

while i <= 3:

i += 2

print(‘*’)
A . one
B. zero
C. two
D. three

Answer: C

Explanation:

Topic: while

Try it yourself:

i = 0

while i <= 3: # i=0, i=2

i += 2

print(‘*’)

"""

*

*

"""

In the first iteration of the while loop i is 0

i becomes 2 and the first star is printed.

In the second iteration of the while loop i is 2

i becomes 4 and the second star is printed.

i is 4 and therefore 4 <= 3 is False what ends the while loop.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments