A while loop repeats as long as the condition is true. Used when the number of iterations isn’t known in advance.
while (condition) {
// code block
}A do...while loop always executes at least once. Condition is checked after the block.
do {
// code block
} while (condition);
The for loop is compact and ideal when the number of iterations is known.
for (initialization; condition; update) {
// code block
}Use break to exit a loop early
Use continue to skip the rest of the current loop iteration and continue with the next.
A loop inside another loop is useful for iterating over 2D structures like grids or matrices.
You can allow a user to stop a loop using input. For example, loop until they enter -1.
What will this print?
012301241234Ask the AI if you need help understanding or want to dive deeper in any topic