while Loop?A while loop keeps executing a block of code as long as the condition is true.
while(condition) {
// loop body (runs repeatedly)
}
πΉ Before each iteration, the condition is checked.
πΉ If itβs true β loop runs
πΉ If false β loop stops
int i = 1;
while(i <= 5) {
cout << i << " ";
i++;
}
π§ Output: 1 2 3 4 5
You must update the loop variable (like i++), or you might create an infinite loop.
Suppose youβre entering a password β and the system keeps asking "Enter again" until you type the correct one.
Thatβs a perfect use case for a
whileloop.