While loop

From Canonica AI

Definition

A while loop is a control flow statement in programming languages that allows code to be executed repeatedly based on a given Boolean Condition. The while loop can be thought of as a repeating if statement.

Syntax

The basic syntax of a while loop in most programming languages is:

``` while (condition) {

 // code block to be executed

} ```

In this syntax, the 'condition' is evaluated, and if it's true, the code within the block is executed. This repeats until the condition becomes false. Because the 'condition' is checked before the loop is executed, the code block might never get executed if the condition is false to begin with.

Usage

While loops are used when a set of instructions needs to be repeated based on a condition. If the number of iterations is not known beforehand, a while loop is a good choice.

A screenshot of a while loop in a code editor.
A screenshot of a while loop in a code editor.

For example, reading a file line by line, until no lines are left to read, is a typical use case for a while loop. Here's an example in Python:

``` with open('file.txt', 'r') as f:

 while True:
   line = f.readline()
   if not line:
     break
   print(line)

```

In this example, the while loop will keep reading and printing lines from the file until there are no lines left to read, at which point it will break out of the loop.

Infinite Loops

An infinite loop occurs when the condition of the while loop never becomes false. This results in the loop continuously running and the program never reaching a point of completion.

Infinite loops can be intentional or the result of a programming error. If intentional, they are usually controlled by other parts of the program. For example, a game might have a main loop that runs forever, only stopping when the user decides to quit the game.

Loop Control Statements

Loop control statements change the execution from its normal sequence. When the execution leaves a scope, all automatic objects that were created in that scope are destroyed.

There are three types of loop control statements in most programming languages:

1. Break statement: Used to terminate the loop prematurely when a certain condition is met. 2. Continue statement: Skips the rest of the current loop iteration and immediately continues with the next one. 3. Pass statement: In Python, the pass statement is a null operation; nothing happens when it executes. It is used as a placeholder when a statement is required syntactically, but no code needs to be executed.

Comparison with Other Loop Types

While loops are similar to for loops and do-while loops, but there are some important differences:

- For loop: Generally used when the number of iterations is known. It has three parts: initialization, condition, and increment/decrement. - Do-while loop: Similar to a while loop, but the condition is checked after the loop body is executed. This means that the loop will always be executed at least once.

See Also

Control flow For loop Do-while loop Boolean condition

Categories