While condition program having confusion

Hi All,

I want to understand the execution of both program-1 and program-2.
In program-1, I can see the result it is breaking once the value reached 2.
While in Program-2, it is resulting values up to 10 by skip 3.

I’ve not understood the behavior of program-1 actually. Why it is not continuing up to 10.

Please help me with your valued words.

Program-1:

   i = 0
   while (i<=10):
         if (i==3):
         continue
   print(i)
   i=i+1

Result:
0
1
2

Program-2:

   i = 0
   while (i<=10):
        i=i+1
        if (i==3):
             continue
        print(i)

Result:

1
2
4
5
6
7
8
9
10
11

Welcome to python discuss. You can refer to this link to learn more about formatting your code instead of displaying it as raw text and some general rules about the user category.

Basically, in the first example, the program executes the scope of the while loop while i is increasing, and when i reaches 3, we have a continue statement. This statement will instruct the program to jump to the line while(i<=10):. The most important thing here is that the program is not updating the control variable i before the continue statement. This will cause the program to jump repeatedly to the beginning of the while loop. Hence, the max value that i can achieve is 3. As a result of this, the program workflow is never coming out of the while loop, and it will ultimately get stuck forever.

The second program works as expected because it updates the value of i before the continue statement, and it only skips the value i==3.

That’s pretty much it. I hope it helps.

By Mohammed Tanveer Mohsin via Discussions on Python.org at 07Jun2022 19:34:

I want to understand the execution of both program-1 and program-2.
In program-1, I can see the result it is breaking once the value reached 2.

Actually, it is spinning. I suspect you will find that program-1 never
finishes.

Thank you for including both the code and the resulting output.

While in Program-2, it is resulting values up to 10 by skip 3.

I’ve not understood the behavior of program-1 actually. Why it is not continuing up to 10.

Let’s look at program-1.

BTW, see the other comment to this post: it helps to surround your
Python code with triple backticks like this:

```python
your python code here
```

which will preserved the indenting in the forum.

Anyway, to your code:

i = 0
while (i<=10):
    if (i==3):
        continue
    print(i)
    i=i+1

If you put a print("top of loop") right under the while the
behaviour may be more obvious. When i==3 it continues directly to the
next iteration of the loop. Importantly, it does not run the i=i+1
code which increments the value in i. So i remains ==3 on the next
tieration, and so on forever.

Usually we would write such a “counting” loop like this instead:

for i in range(0, 11):  # counts 0 through 10, stops before 11
    if i != 3:
        print(i)

By using a for-loop with a range() the loop handles the increment
itself, and has no possibility of not incrementing. So your control
flow bug, where you accidentally avoided the increment, is outright not
possible.

Also, notice that instead of using continue to skip the rest of the
loop body, we instead invert the test, and use the if-statement to skip
just the part of the loop body which we want to not happen. Control then
proceeds to the rest of the loop body so that the normal things happen.
Here, that body is empty below the if-statemment, but in your original
while loop, modified the same way:

i = 0
while (i<=10):
    if (i!=3):
        print(i)
    i=i+1

you would not have skipped the increment.

A few other remarks:

  • you do not need the brackets around the conditions, though they do no
    harm
  • you can write the increment like this: i += 1; again, what you wrote
    is still valid, this is just a more readable alternative

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you all so much for your valuable time and explaining to me for both my test program.

I’m new to python, i’m doing a day 2-day test for each topic.
I’ve corrected the program as per the suggestion for indentation.

I’ve last doubt, in my program 2, the condition is for the i<=10, then why it is going till 11.

By Mohammed Tanveer Mohsin via Discussions on Python.org at 08Jun2022 01:10:

I’ve last doubt, in my program 2, the condition is for the i<=10, then
why it is going till 11.

Because you enter the final loop with i==10, then you increment it
before the print().

Cheers,
Cameron Simpson cs@cskk.id.au