The format of while is: |
while condition: |
code block |
For example: find the sum of all integers 1-100
x = 0 #summation i = 1 #count while i < 101: i = i + 1 x = x + i print('sum = ',x)Each time the code block is executed once, the condition will be re-evaluated.
In addition , the while statement has two commands: continue and break
continue is used to skip the while loop
and break is used to exit the while loop . Same as if, while can also be Use else to execute a block of code.