while do in pascal

In Pascal, the while loop is used to execute a block of code repeatedly as long as a certain condition is true. The basic syntax of the while loop in Pascal is as follows:

while condition do
begin
  // Code to be executed
end;

Here's a brief explanation of each part of the while loop syntax:

  • condition: This is the condition that is evaluated before each iteration of the loop. If the condition is true, the loop body will be executed. If the condition is false, the loop will exit and the program will continue with the next line of code after the loop.
  • begin and end: These are keywords used to enclose the block of code that will be executed repeatedly as long as the condition is true. The code block can contain any valid Pascal statements, including nested loops, conditional statements, and other constructs.

Here's an example that demonstrates the usage of the while loop in Pascal:

program WhileLoopExample;
var
  i: Integer;
begin
  i := 1;
  while i <= 5 do
  begin
    writeln('Iteration: ', i);
    i := i + 1;
  end;
end.

In this example, the while loop iterates as long as the value of i is less than or equal to 5. Inside the loop body, the current value of i is printed, and then i is incremented by 1. The loop will continue until i becomes greater than 5, at which point the condition will evaluate to false and the program will exit the loop.

The output of this program will be:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5

This demonstrates how the while loop can be used to repeat a certain block of code until a specific condition is no longer true.