+ 1
what is i = i + 1
can't undrstand what is i=i+1 or i=i-1 !!!
5 Answers
+ 3
I guess it's a typical misunderstanding in Python (and in C, I should say). A single = stands for "assign the expression in the right to the variable in the left", whereas the comparison operator is not =, but == (meaning "are the left and right sides equal?"). A bit weird for beginners, I guess.
+ 1
i = i+1
its nothing but every time your adding i by 1 .
let me explain the code
while initially i =1
1 loop
while 1 <=5 yes
it will print value i that is 1
now i=i+1 adding i by one results to i=2
2loop :
while 2<=5 yes
it will print value i that is 2
now i=i+1 adding i by one results to i=3
3loop:
while 3 <=5 yes
it will print value i that is 3
now i=i+1 adding i by one results to i=4
4loop
while 4<=5 yes
it will print value i that is 4
now i=i+1 adding i by one results to i=5
5 loop
while 5<=5 yes
it will print value i that is 5
now i=i+1 adding i by one results to i=6
6 loop
while 6 <=5 false
program terminates
Hope you understood ..
0
for more clarity please post the code ..
0
i = 1
while i <=5:
print(i)
i = i + 1
print("Finished!")
0
It means that i takes its value plus one. So if i is 10, i=i+1 makes i 11. It is often used in loops to make a progression in them. In while loops you always need this kind of statement to progress throughout the loop or it will carry on indefinetely.
i=0
while i<10
print(i)
i=i+1
prints 0 the first time
1
2
3
...
9
If there wasn't i=i+1 i would always be 0 and loop wouldn't stop printing i.