0

How does i = i + 1 works after print code?

As much as I know the code runs downwards but how does "i = i + 1" continue to loop after print(i) code executed? Shouldn't it stop at "1"?

5th Sep 2016, 9:30 AM
Fırat
Fırat - avatar
5 odpowiedzi
+ 1
i = 1 while i <=5: print(i ,"code shouldn't stop here ?") if i <=5: i +=1 print("Finished")
5th Sep 2016, 10:18 AM
Özgür Bildik
Özgür Bildik - avatar
0
I would need to see more of your code in context. But your right in saying that i = i + 1 should only be executed once. Unless you have this in an looping function?
5th Sep 2016, 10:02 AM
Dane Lennon
Dane Lennon - avatar
0
I am talking about original code in here: i = 1 while i <=5: print(i)[code shouldn't stop here?] i = i + 1 print("Finished!")
5th Sep 2016, 10:04 AM
Fırat
Fırat - avatar
0
Ok so as I said before, this is in a While Loop. This means that "while" the integer "i" is smaller than 5, the code inside the while loop will continue to execute line by line. This means that the line "i = i + 1" the first time around will be come "i = 1 + 1" which is 2. Therefore i = 2 now. The second time around the line is now "i = 2 + 1" meaning the "i" is now equal to 3 and so on and so forth. The value of "i" is stored and saved each time
5th Sep 2016, 10:09 AM
Dane Lennon
Dane Lennon - avatar
0
got it. so code goes back to print line after (i = i + 1) in while thanks very much
5th Sep 2016, 10:17 AM
Fırat
Fırat - avatar