The Loop Construct – While
The while loop is a control flow statement in programming languages that allows you to repeatedly execute a block of code while a certain condition is true.
The syntax of the while loop is:
while (condition) {
// code to execute while condition is true
}
In this syntax, the condition
is a boolean expression that is evaluated before each iteration of the loop. If the condition
is true, the block of code inside the loop is executed. After the block of code is executed, the condition
is evaluated again, and if it is still true, the block of code is executed again. This continues until the condition
is false, at which point the loop terminates and control is passed to the next line of code after the loop.
Here is an example of a while loop in Python that prints the numbers from 1 to 5:
num = 1
while num <= 5:
print(num)
num += 1
In this example, the loop executes as long as num
is less than or equal to 5. The print
statement inside the loop prints the value of num
and then the num += 1
statement increments num
by 1, so that the loop will eventually terminate when num
becomes greater than 5.
Apply for PHP Certification!
https://www.vskills.in/certification/certified-php-developer