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

In this example, the while loop is executed as long as the variable 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:

OUTPUT:

Count: 0 Count: 1 Count: 2 Count: 3 Count: 4
The loop stops executing when count becomes 5, and the program continues with the next statement after the loop.

Comments

Popular posts from this blog

Fibonacci Series

COMPARISION EXPRESSION

LOGICAL EXPRESSION