C++ Logo


Advanced search

Subject: [std-proposals] dofor loop
From: Menashe Rosemberg (rosemberg_at_[hidden])
Date: 2019-12-28 03:47:41


The loops we have in c++ are loop while, for and do...while. How they work is pretty know, but I will describe them shortly (in their general use) to better demonstrate my proposal.
1. while: (condition evaluation before statements)
    step1: Evaluate condition. Continue to step2 if the condition is true
    step2: Execute statements    step3:  return to step1
2. do: (condition evaluation after statements)
    step1: Execute statements    step2: Evaluate condition. Return to srep1 if the condition is true
3. for: (condition evaluation before statements)
    step1: create a counter    step2: Evaluate condition. Continue to step3 if the condition is true    step3: Execute statements    step4: Execute an mathematics operation (with the counter) and return to step2
What I see is we have a lack type of loop where is similar to for but with the condition after statements as the do...while loop).The proposal is to include a new loop that marge the for and do..while following the pattern below:

dofor loop:         step1: create a counter         step2: execute statements         step3: Execute a mathematics operation (with the counter)         step4: return to step2 if condition is true
The syntax could be similar to for like that:
dofor ( <variable declaration>; <mathematics operation>; <condition evaluation>) {         statements...

Just for example, I wrote a templated functionn to print the index of an array's elements:
//1. While      template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
      void WhileDescending(const array<obj, S>& Obj) {
            size_t Size = Obj.size();
             while (Size) {
                      cout << Size;
//2. do      template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
      void DoDescending(const array<obj, S>& Obj) {
             size_t Size = Obj.size();
             do {                  --Size;
                  cout << Size;
             } while (Size);
//3. for      template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>
      void ForDescending(const array<obj, S>& Obj) {             for (size_t Size = Obj.size() - 1; Size != numeric_limits<size_t>::max(); --Size)
                  cout << Size;
//The proposal 'dofor'         template <typename obj, size_t S, enable_if_t <S != 0, size_t> = 1>         void DoForDescending(const array<obj, S>& Obj) {                dofor (size_t Size = Obj.size(); --Size; Size >= 0)                         cout << Size;
Best regards,M.Rosemberg

STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups