In Python, a flag in a while loop is a boolean variable used to control the loop’s execution. The flag helps in breaking or continuing the loop based on specific conditions. It is often used to represent a condition or state that determines whether the loop should run or stop.
For example:
flag = True
while flag:
user_input = input(“Enter ‘quit’ to stop: “)
if user_input == ‘quit’:
flag = False
else:
print(“You entered:”, user_input)
Here, the flag variable controls the loop, and it stops when the user enters “quit.”