+ 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.
12 odpowiedzi
+ 3
Dominic I am assuming you meant "any_statement==true" and not "any_statement=true".
+ 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;
}
}
+ 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.
+ 1
You can use the 'break' statement in code whenever you want to stop the loop.
+ 1
okay so while True is an infinate loop that can get broken by “break”?
+ 1
Ignore "break" lets just understand while loops for now. I explained it as best I could.
https://code.sololearn.com/cP8XY4hMhob2/?ref=app
+ 1
okay thanks this helped me a lot
1
2
3
4
5
6
7
8
9
0
End of loop!
+ 1
Sonic Yes, my bad
- 1
no im talking about while not break
- 1
um
- 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