One, while loop
A while loop must have a counter. Satisfies the loop condition that executes the loop body until the condition is not satisfied and ends the loop. When a while loop corresponds to an else, the loop executes after the normal end.
Example: randomly generate a number and guess what the number is. There are three opportunities, each time input will be prompted to guess big, guess small or guessed right, more than the number of times prompted "the number of times has been exhausted."
Analytical:
(1) Count is a counter that needs to be incremented by 1 for each cycle.
Count+=1, equivalent to count=count+1. The same count*=3 is equivalent to count=count*3, and the same is true for/,%d operations.
(2) Break: A break is encountered in the loop and the loop ends immediately. Regardless of whether the loop has ended, end the entire loop immediately and perform the next step.
Continue: In the loop encountered continue, immediately jump out of this cycle, the next cycle.
Second, for Loop
The For loop does not require a counter. The syntax format for A For loop is:
For Iterating_var in sequence:
Statements (s)
For example, the judging condition can be for the I in Range (ten), I starting from 0 count, each cycle automatically add 1, until 9, that is, a total loop 10 times.
For n in range (10,20), n is counted from 10, and each cycle is automatically added 1 to 19, with a total loop of 10 times.
Example: Still using the just while loop to guess the numbers.
Python-while loops and for loops