Need help understanding this while loop
Reddit
[Python] Why would I use a for loop instead of a while loop?
for means "do something with each of these things".
while means "keep doing this until something is no longer true".
So... say what you mean. Readability counts.
I've trying to learn while loops for over 2 weeks... Help? [ELI5]
You have two variables, m and c. The variable m is what the user inputs to determine what height the ball is dropped from. The variable c will count the number of bounces.
Then the while loop starts. Quite literally, while the ball bounces back to a height that is greater than 0.01, your program keeps executing the rest of the code over and over.
In this specific case, your program first decreases the height to 70% of previous value (so it bounces back lower due to gravity), then increments the c counter by one, so says the ball bounces one time.
Then the instruction in the while loop is over. The program checks again if the height m is greater than 0.01. If it is, it will keep repeating the while loop code block until the condition is no longer met. If it isn't, it will do the rest of the code. In this example it will print that the ball has bounced c number of times.
You print c because that is the variable where you store number of bounces. If you printed m it would print the height the ball is at right now, after all those operations. Which would mean it would print a number that's less than 0.01.