Do While
This command is similar to While
.
The main difference is that Do While
checks the condition after it executes the block.
It executes the block at least once.
Let's write a program that reads a number from user.
We want to make sure that it is positive:
- declare
x: Integer
- add a
Do While
command with conditionx <= 0
- input
x
inDo While
body (arrow pointing from circle toDo While
diamond)
We read it like this: "Enter number x, as long as it is less than or equal to zero"
Run the program and try various inputs: 1
, 17
, -55
, 6
.
It will finish the Do While
only if you enter a positive number.