+ 1

A better explanation for “while”???

I KEEP LOOKING AT THE WHILE LOOP ARTICLE BUT I STILL DONT UNDERSTAND IT. IS THERE SOMEONE WHO CAN DESCRIBE IT BETTER??? Ex. I KEEP SEEING THE “while True” AND CONFUSED ON LIKE, WHEN THE LOOP STOPS.

12th Oct 2019, 8:08 PM
MCPE Gaming
MCPE Gaming - avatar
12 Answers
+ 3
Dominic I am assuming you meant "any_statement==true" and not "any_statement=true".
14th Oct 2019, 8:35 AM
Sonic
Sonic - avatar
+ 2
Grab a paper and a pen and write down a while loop, go over each line with your pen acting as a debugger and think about what is happening... Lets use pseudo code for simplicity and lets initialize an integer (i) with the value 0. At each loop I will add 1 to i. When i reaches 5 I will break the while loop: Int i = 0; While ( any_statement==true ) { // true by default causes an // infinite loop i += 1; If ( i == 5 ) { // this will break out the // while loop any_statement=false; } }
12th Oct 2019, 8:38 PM
Nihilo
Nihilo - avatar
+ 2
I understand you are talking about the 'while' loop in Python. The loop is enabled while something is true. Hence, it is called 'while'. Here is an example. num = 6 while num > 0: num -= 1 if num == 2: break print(num) We create a variable num and set its value to 6. Then we create a 'while' loop, that loops while our variable 'num' is more than 0. Every time it loops, it subtracts 1 from our variable num, and checks if it is 3. If it isn't, it outputs the num variable. If it is, the loop ends. The output should give you: 5 4 3 The 'break' statement stops the loop.
12th Oct 2019, 8:40 PM
syndicate.fs
syndicate.fs - avatar
+ 1
You can use the 'break' statement in code whenever you want to stop the loop.
12th Oct 2019, 8:32 PM
syndicate.fs
syndicate.fs - avatar
+ 1
okay so while True is an infinate loop that can get broken by “break”?
12th Oct 2019, 8:49 PM
MCPE Gaming
MCPE Gaming - avatar
+ 1
Ignore "break" lets just understand while loops for now. I explained it as best I could. https://code.sololearn.com/cP8XY4hMhob2/?ref=app
12th Oct 2019, 9:25 PM
Odyel
Odyel - avatar
+ 1
okay thanks this helped me a lot 1 2 3 4 5 6 7 8 9 0 End of loop!
12th Oct 2019, 9:51 PM
MCPE Gaming
MCPE Gaming - avatar
+ 1
Sonic Yes, my bad
14th Oct 2019, 9:51 AM
Nihilo
Nihilo - avatar
- 1
no im talking about while not break
12th Oct 2019, 8:32 PM
MCPE Gaming
MCPE Gaming - avatar
- 1
um
12th Oct 2019, 8:38 PM
MCPE Gaming
MCPE Gaming - avatar
- 1
so that means once i reaches 5, it will stop the loop?? and by the way is this python?? because it doesnt look like it
12th Oct 2019, 8:42 PM
MCPE Gaming
MCPE Gaming - avatar