Python Forum

Full Version: Common understanding of output processing with conditional statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
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')
Could throw a break statement in there.
i = 2000
while i <= 2100:
    i= i + 10
    print(i)
    if i == 2100:   
        print('task complete')
        break
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')
@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?
@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
@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.