Could anyone explain me the output? | SoloLearn: Learn to code for FREE!

+14

Could anyone explain me the output?

i = 0 while 1==1: print(i) i = i +1 print("Finished") Output: 0 Finished 1 Finshed 2 Finished 3 Finished . . . So on.

7/9/2020 10:20:42 AM

Sarfira

40 Answers

New Answer

+7

i is a variable with the value 0. while 1 == 1: is the same as while True: wich is an infinite loop (nothing tells it to stop) each time it loops it first prints the value of i, then adds 1 to the value of i, then prints finished. it will do this forever.

+11

Do you think in both cases statement is always true?

+11

Actually I was confused that why the Finished message also printing alternatively along with infinite loop.

+11

Learner If you wanna make infinite loop , no need to write 1==1 Directly use while True: OR while 1:

+6

i assume you want it to print "finished" at the end but it will never do that since its an infinite loop. It's all about indentation in the loops try this: i = 0 while i < 6: print(i) i += 1 print('Finished')

+5

Try to write break after print ("finish ") This will keep on counting till infinity but finish will print only once

+4

1 == 1 will always be True, infinite loop. i = 0; Everytime it loops add 1 to i, means increment i by 1.

+3

"==" sign is a Boolean function to represent if a vale is true or false So you took i=0 In the loop you used 1 is true so it prints all the values and becomes an infinite loop

+3

Like shift the print("finished ") Just below the inundation of while

+3

He literally just said to do what that code i showed above does.

+3

It ran exactly the way you programmed it. Look at the subtle differences between the code you posted and the code i posted. Copy and paste mine if it helps, you got it man. **Indentation**

+3

So you set the variable i = 0 (I assume a counter) You then declared a while loop with the condition that it will run while 1 == 1 which is the same as saying while True As 1 is always going to be equal to 1 the loop will become an infinite loop (AKA never ending) There is only 1 way to exit this current loop, that would be with the break keyword which breaks out of the loop. Every iteration of this loop you are iterating your i variable and adding 1 to it. The best way to avoid an infinite loop is to set a boolean outside that will become true/ false at some point that will subsiquently exit the loop. Having either while 1==1 or while True is considered bad practice among a lot of python programmers. Heres an example. https://code.sololearn.com/cnWOSxeARIwd/?ref=app

+3

i agree Vivek Vadi

+2

I was confused that why "Finished" also printing alternatively along with the loop

+2

Why do you disliking it? i am just clearing my doubt. There is nothing useless here. It's quite weird.

+2

Finished is also placed in side the loop If you backspace the print command of finished then it won't print repeatedly

+2

Bhavya gupta sorry i didn't get you. Tum hindi mein bhi samjha sakti ho

+2

Slick let me try

+2

What if we use 'break' statement before print("Finished")?

+2

Give it a shot and see. Thats what the playground is for