While loop
suggest changeA while loop executes a block while a condition is true. Use it when the number of iterations is not known in advance. The example reads integers until the user enters 0. Entering 0 makes the condition false, so execution leaves the loop and continues with any subsequent code. If scanf does not convert an integer, the loop stops; otherwise a failed conversion would leave num unchanged and the loop would never end.
#include <stdio.h>
int num = 1;
while (num != 0)
{
if (scanf("%d", &num) != 1) {
break;
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents