Appearance
While Loop
A while loop repeatedly executes a block of code as long as the specified condition is true.
Example
Creating a program that uses a while loop to print numbers from 1 to 5.
Pseudocode
txt
BEGIN
INITIALIZE i = 1
WHILE i <= 5:
PRINT i
INCREMENT i by 1
END WHILE
ENDSyntax
txt
while (condition) {
// Code block to be executed repeatedly
// This code runs as long as the condition is true
}Code Example
c
int i = 1; // Initialize initial value
while (i <= 5) {
printf("%d ", i); // Print the value of i
i++;
}c++
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}java
int i = 1;
while (i <= 5) {
System.out.print(i + " ");
i++;
}python
i = 1
while i <= 5:
print(i, end=" ")
i += 1