+ 1

While loop. It won’t add +1 to A. Never ending continue. Pls help

Condition. If C is less than 5 then the loop will continue, else it will end. A will increase by 1 after every completed loop. A = 1 B = A + 1 C = A + B while C < 5: print (“continue”) A += 1

8th Sep 2018, 3:04 AM
Mark Coching
Mark Coching - avatar
10 Antworten
+ 3
Try this. A = 1 B = A + 1 C = A + B while C < 5: print (“continue”) A += 1 C= A + B
8th Sep 2018, 3:47 AM
Alexander Santos
Alexander Santos - avatar
+ 2
you must place C=A+B inside loop
8th Sep 2018, 3:40 AM
Nafis Arinda Rizky Putra Handoko
Nafis Arinda Rizky Putra Handoko - avatar
+ 2
No problem! Well, programs only run line per line. If line has passed, it won't return unless it's an order. "While" creates a block ("area") for you and that area will be repeated while the condition is true. The reason break works is that it stops the iterations around while, for and other blocks functions, and continue after their delimited area. I recommend trying to see how FOR works, also how DO... WHILE works. They are different, but can do same result. Search for it, will be nice for you. EDIT: Changed the word object to function, to avoid confusion
8th Sep 2018, 4:00 AM
Alexander Santos
Alexander Santos - avatar
+ 1
C is never higher than 5 by this way. You should add C= A+B after while, so it could repeat.
8th Sep 2018, 3:37 AM
Alexander Santos
Alexander Santos - avatar
+ 1
Let's follow your program line per line so you can understand better. A = 1 //A=1 B = A + 1 //B=2 C = A + B //C=3 while C < 5: print (“continue”) A += 1 //A=2 on first step C= A + B //C=2+2=4 on first step //C is < 5. So redo all instructions On second step: A+=1 //A=3 C=A+B //C=3+2=5 //C is 5, so won't do anything anymore.
8th Sep 2018, 4:07 AM
Alexander Santos
Alexander Santos - avatar
0
that one didnt work. i even put a break and it did stop once. its suppose to run the program until it becomes false.
8th Sep 2018, 3:45 AM
Mark Coching
Mark Coching - avatar
0
that works. thank you. i was stump for hours with this. i never thought putting C...below
8th Sep 2018, 3:56 AM
Mark Coching
Mark Coching - avatar
0
so it ran twice and stopped
8th Sep 2018, 3:57 AM
Mark Coching
Mark Coching - avatar
0
make sense now. i wanted to change my print to say loop so i did this print (“loop”) but i wanted to count to loop 1, loop 2 so now i put a variable loop=1 on the top above while after the C=A+B. how do i make it count to loop 1 loop 2
8th Sep 2018, 4:23 AM
Mark Coching
Mark Coching - avatar
0
You can add another variable, start with the desired number and increment inside the while loop
8th Sep 2018, 4:43 AM
Alexander Santos
Alexander Santos - avatar