Practical C Programs
Retry Budget
A retry loop should stop when it succeeds or when the attempt budget is exhausted.
Retry Budget
retry_budget.c
#include <stdio.h>
int main(void) {
int successAt = ;
int maxAttempts = 3;
int attempts = 0;
int ok = 0;
while (attempts < maxAttempts && ok == 0) {
attempts++;
if (attempts >= successAt) {
ok = 1;
}
}
printf("successAt=%d attempts=%d ok=%d\n", successAt, attempts, ok);
return 0;
}
#include <stdio.h>
int main(void) {
int successAt = ;
int maxAttempts = 3;
int attempts = 0;
int ok = 0;
while (attempts < maxAttempts && ok == 0) {
attempts++;
if (attempts >= successAt) {
ok = 1;
}
}
printf("successAt=%d attempts=%d ok=%d\n", successAt, attempts, ok);
return 0;
}
#include <stdio.h>
int main(void) {
int successAt = ;
int maxAttempts = 3;
int attempts = 0;
int ok = 0;
while (attempts < maxAttempts && ok == 0) {
attempts++;
if (attempts >= successAt) {
ok = 1;
}
}
printf("successAt=%d attempts=%d ok=%d\n", successAt, attempts, ok);
return 0;
}
attempt budget
The maximum attempt count bounds the loop even when the operation does not succeed.
success condition
The loop checks success after each attempt and exits early when possible.