WHILE LOOP Jam 1
WHILE LOOP Jam 1
Output:
6
7
8
Output:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
How to Make an Infinite Loop
with While True
We can generate an infinite
loop intentionally using while
True. In this case, the loop will run
indefinitely until the process is
stopped by external intervention
(CTRL + C) or when
a break statement is found (you
will learn more about break in just
a moment).
How to Make an Infinite Loop with While
True
This is the basic logic of the break statement:
• The while loop starts only if the condition evaluates to True.
• If a break statement is found at any point during the execution of the
loop, the loop stops immediately.
• Else, if break is not found, the loop continues its normal execution and
it stops when the condition evaluates to False.
• We can use break to stop a while loop when a condition is met at a
particular point of its execution, so you will typically find it within a
conditional statement, like this:
while True:
# Code
if <condition>:
break
# Code
• This stops the loop immediately if the condition is True.
Here we have an example of break in
a while True loop:
Output:
Enter an integer: 4
This number is even
while True:
Enter an integer: 6
user_input = int(input(“Enter an
This number is even
integer: “))
Enter an integer: 8
if user_input % 2 != 0:
This number is even
print(“This number is odd”)
Enter an integer: 3
break
This number is odd
print(“This number is even”)