Home > Software engineering >  Python - if în while loop
Python - if în while loop

Time:11-23

I am studiing the Python Fundamentals and i have some trouble understanding this folowing example:

c = 1 

while c < 5:
    c = c   1

    if c >= 4:

        print("string")

        print(c)

   else:

        continue

And the output is:

string

4

string

5

Could someone explain to me please, why i have this output?

CodePudding user response:

Consider the third iteration when c is 3.

c = c 1 increments the value to 4, and goes into the if statement and prints out the values.

Now c is equal to 4. On the next iteration, c < 5 (because its value is 4) and loops again. It gets incremented again and goes into the if statement again.

Therefore, it prints out 2 more values, hence why you see your output.

  • Related