WHILE LOOP
WHILE LOOP
A while loop is a control flow statement in programming that allows a block of code to be executed repeatedly as long as a specified condition is true. The general syntax of a while loop is as follows:
SYNTAX:
while condition: # Code block
The condition is evaluated before each iteration of the loop. If the condition is true, the code block is executed. After the code block is executed, the condition is checked again, and if it's still true, the code block is executed again. This process continues until the condition becomes false, at which point the loop is exited, and the program continues with the next statement after the loop.
Here's a simple example to illustrate the concept of a while loop:
EXAMPLE CODE:
count = 0 while count < 5: print("Count:", count) count += 1
count
is less than 5. The code block inside the loop prints the value of count
and increments it by 1. The output of this code will be:count
becomes 5, and the program continues with the next statement after the loop.
Comments
Post a Comment