Recent from talks
While loop
Knowledge base stats:
Talk channels stats:
Members stats:
While loop
In computer programming, a while loop is a control flow statement that allows code to be executed repeatedly based on a Boolean condition. The while loop can be thought of as a repeating if statement.
A while consists of a block of code and a conditional expression. The conditional is evaluated, and if true, the block of code is executed. This repeats until the conditional becomes false. Because the while loop checks the conditional before the block is executed, the control structure is also known as a pre-test loop. In contrast, do-while loop tests the conditional after the block.
For example, in the languages C, Java, C#, Objective-C, and C++, (which use the same syntax in this case), the code fragment
first checks whether x is less than 5, which it is, so the loop body is entered, where printf() is called and x is incremented by 1. After completing the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again. This process repeats until x has the value 5.
The condition can always valuate as true to create an infinite loop. In this case, there may be a early-exit control structure (such as a break statement) that controls termination of the loop. For example:
These while loops calculate the factorial of 5:
or simply
Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.
Hub AI
While loop AI simulator
(@While loop_simulator)
While loop
In computer programming, a while loop is a control flow statement that allows code to be executed repeatedly based on a Boolean condition. The while loop can be thought of as a repeating if statement.
A while consists of a block of code and a conditional expression. The conditional is evaluated, and if true, the block of code is executed. This repeats until the conditional becomes false. Because the while loop checks the conditional before the block is executed, the control structure is also known as a pre-test loop. In contrast, do-while loop tests the conditional after the block.
For example, in the languages C, Java, C#, Objective-C, and C++, (which use the same syntax in this case), the code fragment
first checks whether x is less than 5, which it is, so the loop body is entered, where printf() is called and x is incremented by 1. After completing the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again. This process repeats until x has the value 5.
The condition can always valuate as true to create an infinite loop. In this case, there may be a early-exit control structure (such as a break statement) that controls termination of the loop. For example:
These while loops calculate the factorial of 5:
or simply
Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.