r/pythonhelp • • Sep 24 '21

I cannot understand this basic code

i=0

while i < 6:

i += 1

if i == 3 :

continue

print(i)

I know the answer is 6, but how?

It says it is adding the i value by 1.

Also, why is the third line important

Please help. I am trying to study for my exam

2 Upvotes

2 comments sorted by

View all comments

3

u/Goobyalus Sep 24 '21
i=0                # declare a variable called `i` and assign it to 0
while i < 6:       # loop until i is not less than 6
	i += 1         # assign `i` to its current value plus 1
	if i == 3:
		continue   # go to the top of the loop if i == 3 (essentially does nothing here)
print(i)