Lesson contents

C++ — Do-while loops

A do-while statement tests for repetition after its body. A bounded retry can therefore count the initial attempt in one place.

A simple example

Print first, then test

  1. Start with n starts at 1
  2. Do this Print n, add 1
  3. Result Stop at 4
The first print happens before the stopping test. The loop prints 1, 2 and 3 on separate lines.
lesson.cpp
#include <iostream>

int main() {
    int n = 1;
    do {
        std::cout << (n) << '\n';
        n++;
    } while (n <= 3);
}
Output
1
2
3
Advertisements

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. The ready-on value simulates success and the limit bounds the number of calls. Either comparison becoming false stops the loop.

First check: Busy. Second check: Ready. Checks made: 2.
Check a printer until it is ready

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 C++17 compiler. Run these commands from the directory containing the example. Use a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson
lesson.cpp
#include <iostream>

int poll_count(int ready_on, int limit) {
    int attempts = 0;
    do {
        ++attempts;
    } while (attempts < ready_on && attempts < limit);
    return attempts;
}

int main() {
    std::cout << poll_count(5, 1) << '\n'
              << poll_count(2, 3) << '\n'
              << poll_count(8, 3) << '\n';
    return 0;
}
Output
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.

First reply: Temporary failure. Next reply: Success. Attempts used: 2.
Handle a temporary print failure

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.cpp and run it separately.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson
lesson.cpp
#include <iostream>
#include <string_view>
#include <utility>

struct Result {
    std::string_view outcome;
    std::size_t attempts;
};

Result process(std::string_view responses, std::size_t limit) {
    std::size_t used = 0;
    for (char response : responses) {
        if (used == limit) break;
        ++used;
        if (response == 'S') return {"sent", used};
        if (response == 'X') return {"rejected", used};
    }
    return {used == limit ? "limit reached" : "no response", used};
}

int main() {
    using Job = std::pair<char, std::string_view>;
    for (Job job : {Job{'A', "TS"}, Job{'B', "XS"}, Job{'C', "TTS"}, Job{'D', ""}}) {
        auto [outcome, attempts] = process(job.second, 2);
        std::cout << job.first << ": " << outcome << " (" << attempts << ")\n";
    }
}
Output
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.

C++17 working draft

Related tags

  • C++
  • post-test loops
  • retries
  • termination