while
loop is used to execute the block of code repeatedly while the specified condition is True.
while condition:
statement(s)
else:
statement(s)
Here,
break
statement.Note: When looping, while statement provides a condition and the statement(s) are executed repeatedly till the condition evaluates False.
The test on expression takes place before each execution of the statement.
>>> i = 0
>>> while i <= 3:
... print(i)
... i += 1
...
0
1
2
3
>>> key = "Cloudyfox"
>>> user_input = input("Please enter the key: ")
Please enter the key: hello
>>> while user_input != key:
... user_input = input("Please enter the key: ")
...
Please enter the key: vertex
Please enter the key: python
Please enter the key: cloudyfox
Please enter the key: Cloudyfox
>>> i = 0
>>> while i <= 3:
... print(i)
... i += 1
... else:
... print("break")
...
0
1
2
3
break
>>> i = 0
>>> while i <= 3:
... print(i)
... i += 1
... break
... else:
... print("break")
...
0
Here, since break
is used, else will not be executed.