Full-stack Python development: python loop statements while, pythonwhile
While Loop
Why is a statement like loop?
Let's take a simple example: we want to calculate the sum of an even number between 0 and. In this case, we need to loop through this statement.
Some people may say: what is the value of 0 + 2 + 4 +... + 98?
Suddenly our condition has changed: Calculate the sum of an even number in 0-0000000
We can imagine what kind of workload this is, and the cycle can solve this problem.
Flowchart:
Gif demonstrates the Python while statement execution process
In python, The while loop is represented in the same way.
Syntax format:
While loop condition: loop body (code block )...
Eg:
# Calculate the sum of the even numbers less than 100 and count = 0sum = 0 # sum while count <100: if count % 2 = 0: # determine whether the value is an even sum + = count # If the value is an even number, add count + = 1 # count Plus One
Endless loop
When the while loop is accidentally written incorrectly, an endless loop will occur, and the program will never stop.
# Calculate the sum of the even numbers less than 100 and count = 0sum = 0 # sum while count>-1: # count>-1 is always true while infinite loop if count % 2 = 0: # determine if it is an even sum + = count # if it is an even number, add count + = 1 # count and add a print ('Here the wind is so strong that it cannot be stopped !!! ')Break and continue
How can we terminate the loop in the loop body? break is used.
# Calculate the sum of the even numbers within 100 and count = 0sum = 0 # sum while count>-1: # count>-1 is always true while infinite loop if count = 100: print ('even if the wind is heavy, I can stop it !!! ') Break # ends the cycle at the current layer. if count % 2 = 0: # determine if it is an even sum + = count # if it is an even number, add count + = 1 # count and add a print ('Here the wind is so strong that it cannot be stopped !!! ')
Break statement: Used to terminate a loop statement. That is, if the loop condition does not have a False condition or the sequence is not completely recursive, the execution of the loop statement is also stopped.
# Calculate the sum of the even numbers less than 100 and count = 0sum = 0 # sum while count>-1: # count>-1 is always true while infinite loop if count = 50: print ('wind is so big, I can only stop it !!! ') Continue # End this loop and enter the next loop if count = 100: print (' even if the wind is heavy, I can stop it !!! ') Break # ends the cycle at the current layer. if count % 2 = 0: # determine if it is an even sum + = count # if it is an even number, add count + = 1 # count and add a print ('Here the wind is so strong that it cannot be stopped !!! ')
Continue statement: tells Python to skip the remaining statement of the current loop, and then continues the next loop.
Summary:
The break jumps out of the entire cycle and ends cyclically.
Continue jumps out of this loop and executes the next loop
Else statement
In python, while... When the loop condition of else is False, execute the else statement block:
Count = 0 while count <5: print ('loop in running ') count + = 1 else: print ('loop terminated ')
Result
Note:
The else statement is executed only when the while loop is completed normally (the loop condition is False ).
Terminate the loop through break and will not execute
Eg:
Count = 0 while count <5: if count = 3: break print ('loop in running ') count + = 1 else: print ('loop terminated ')
Result
If you have any questions or errors, please leave a message.