Python Forum
Common understanding of output processing with conditional statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Common understanding of output processing with conditional statement (/thread-40749.html)



Common understanding of output processing with conditional statement - neail - Sep-17-2023

Hello,

Can anyone please explain why the output calls the Evaluation statement before printing the series?

And what is the process to print the evaluation at the end?

Thank you. Heart

----------------------------------------------
i=2000
while i <= 2100:
    i= i + 10
    print(i)
    if i == 2100:        
        print('task complete')
----------------------------------------------

Output image- [attachment=2559]


RE: Common understanding of output processing with conditional statement - Gribouillis - Sep-17-2023

The problem is that the program prints 'task complete' before the task is completed, because when i == 2100, the loop must run one more time because the loop says while i <= 2100.

Anyway, the 'task complete' thing should come after the loop.
i = 2000
while i <= 2100:
    i= i + 10
    print(i)
print('task complete')



RE: Common understanding of output processing with conditional statement - menator01 - Sep-17-2023

Could throw a break statement in there.
i = 2000
while i <= 2100:
    i= i + 10
    print(i)
    if i == 2100:   
        print('task complete')
        break



RE: Common understanding of output processing with conditional statement - deanhystad - Sep-17-2023

A for loop is cleaner when you know exactly how many times the loop executes.
for i in range(2000, 2101, 10):
    print(i)
print('task complete')



RE: Common understanding of output processing with conditional statement - neail - Sep-17-2023

@Gribouillis

Thank you, I have tried multiple combinations before posting it here, but don't know how I missed that, I feel so dumb Tongue

update-
I just noticed you omitted the == operator. I know its silly I am instructing and comparing the exact value. but can you do that with the == operator?


RE: Common understanding of output processing with conditional statement - neail - Sep-17-2023

@menator01
hi, is the break operation really necessary? because it's already stopping, I mean its not an infinite loop.

update-

kudos, problem solved.
What magic is the break doing?? what did I miss? Huh


RE: Common understanding of output processing with conditional statement - neail - Sep-17-2023

@deanhystad

hi, I have learned the range (from, to, interval) function, but this was an experiment, and despite trying a many combination, I failed to get the desired result so I posted. actually I wanted to clear my conception. Still, thank you, if someone is looking will be helpful.