C — Do-while loops
A do-while loop executes its body before checking whether it should repeat. Use it when one operation must occur even if the first attempt succeeds.
A simple example
Print first, then test
- Start with n starts at 1
- Do this Print n, add 1
- Result Stop at 4
#include <stdio.h>
int main(void) {
int n = 1;
do {
printf("%d\n", n);
n++;
} while (n <= 3);
}
1 2 3
Poll until ready or the attempt limit
Count the first poll for every request, even if it is already ready. Stop after success or after the configured maximum number of attempts. ready_on is the simulated first successful poll. The second comparison independently caps the work. The terminating semicolon belongs to the do-while statement.
A print desk checks the printer once before deciding whether to check again. If the printer answers ready on the second check, the loop stops there. A limit of three checks keeps an offline printer from holding up the desk.
Run the example
Use a C17 compiler. Run these commands from the directory containing the example. Use a Unix-style shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson
#include <stdio.h>
static int poll_count(int ready_on, int limit) {
int attempts = 0;
do {
++attempts;
} while (attempts < ready_on && attempts < limit);
return attempts;
}
int main(void) {
printf("%d\n", poll_count(5, 1));
printf("%d\n", poll_count(2, 3));
printf("%d\n", poll_count(8, 3));
return 0;
}
1 2 3
With one permitted attempt, the first case stops at 1. The second becomes ready on attempt 2. The last case never reaches readiness within its three-attempt limit.
Apply the loop to a retry queue
Simulate a job using prerecorded replies. T means a temporary failure, S means success, and X means permanent rejection. Process at most two replies per job. Stop on S or X and report how many replies were consumed.
A queue can retry a temporary failure once under a two-attempt limit. A temporary failure followed by success finishes the job. A permanent rejection stops the job immediately and leaves the next reply unused.
Run this example
Save this program as lesson.c and run it separately.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson#include <stdio.h>
#include <stddef.h>
struct result {
const char *outcome;
size_t attempts;
};
static struct result process(const char *responses, size_t limit) {
size_t used = 0;
while (used < limit && responses[used] != '\0') {
char response = responses[used++];
if (response == 'S') return (struct result){"sent", used};
if (response == 'X') return (struct result){"rejected", used};
}
return (struct result){used == limit ? "limit reached" : "no response", used};
}
int main(void) {
const char *jobs[] = {"TS", "XS", "TTS", ""};
const char *names[] = {"A", "B", "C", "D"};
for (size_t i = 0; i < sizeof jobs / sizeof jobs[0]; ++i) {
struct result result = process(jobs[i], 2);
printf("%s: %s (%zu)\n", names[i], result.outcome, result.attempts);
}
return 0;
}
A: sent (2) B: rejected (1) C: limit reached (2) D: no response (0)
Job A succeeds on its second reply. B stops at the rejection before its later success. C reaches the attempt limit with a success still unread. D has no recorded reply, so its count stays zero. Try raising the limit to three: only C changes, succeeding on the third reply.
Try it yourself
A request is ready on the first poll and allows three attempts. How many polls run?
Check your answer
Exactly one. The success check happens after the first poll. The example assumes a positive attempt limit.