Programming Language & Its Applications — Introduction to C Programming, NEC licence examination syllabus (Nepal Engineering Council).
for, while, do-while — and knowing which fits saves you from off-by-one errors.
All three loops repeat a block, and any one can be rewritten as the others. But they express different intentions: for says "a known number of iterations", while says "until a condition changes", and do-while says "at least once, then check". Matching the loop to the intention is what makes code readable — and prevents the off-by-one mistakes that plague loop-heavy exam questions.
for(;;) at the end of this topic is what every embedded device runs. The firmware in a microwave, a router, an Arduino or a pacemaker has a main that never returns — it loops forever reading sensors and driving outputs, because there is no operating system to return to. Search "embedded superloop architecture" and you will find this exact pattern in production code controlling real machinery.#include <stdio.h>
int main(void) {
/* for - counted iteration */
for (int i = 1; i <= 5; i++) printf("%d ", i);
printf("\n");
/* while - same thing, more verbose */
int j = 1;
while (j <= 5) { printf("%d ", j); j++; }
printf("\n");
/* do-while - runs even when the condition is false */
int k = 10;
do { printf("ran once with k=%d\n", k); } while (k < 5);
/* while with the same false condition: never runs */
int m = 10;
while (m < 5) { printf("never printed\n"); }
return 0;
}
Output:
1 2 3 4 5
1 2 3 4 5
ran once with k=10
The do-while distinction is not academic. Menu loops and input validation genuinely need "do the thing, then decide whether to repeat" — you must show the menu before you can know whether the user wants to quit. Using a while loop there forces you to duplicate the menu code before the loop.
#include <stdio.h>
int main(void) {
/* skip multiples of 3, stop at 13 */
for (int i = 1; i <= 20; i++) {
if (i % 3 == 0) continue; /* skip this one */
if (i > 13) break; /* leave the loop */
printf("%d ", i);
}
printf("\n");
/* nested loops: break exits only the INNER one */
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) break; /* only escapes the j loop */
printf("(%d,%d) ", i, j);
}
}
printf("\n");
return 0;
}
Output:
1 2 4 5 7 8 10 11 13
(1,1) (2,1) (3,1)
#include <stdio.h>
int main(void) {
int n = 5;
/* right triangle */
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) printf("* ");
printf("\n");
}
printf("\n");
/* centred pyramid: spaces then stars */
for (int i = 1; i <= n; i++) {
for (int s = n - i; s > 0; s--) printf(" ");
for (int j = 1; j <= 2*i - 1; j++) printf("*");
printf("\n");
}
return 0;
}
Output:
*
* *
* * *
* * * *
* * * * *
*
***
*****
*******
*********
#include <stdio.h>
int main(void) {
int count = 0;
/* all three are the same infinite loop */
/* for (;;) { } while (1) { } do { } while(1); */
for (;;) { /* the idiomatic C form */
count++;
if (count >= 3) break; /* exit condition inside */
printf("iteration %d\n", count);
}
printf("exited after %d\n", count);
/* An accidental infinite loop - float comparison */
/* for (float f = 0.0f; f != 1.0f; f += 0.1f) ...
NEVER terminates: 0.1 is not exact in binary, so
the sum never equals exactly 1.0 */
return 0;
}
Output:
iteration 1
iteration 2
exited after 3
That commented-out float loop is a genuinely important trap: never test floating-point values for exact equality. Use f < 1.0f or compare against a tolerance.
i < n gives n iterations, i <= n gives n+1.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…