C++ Logo


Advanced search

dofor loop

From: Menashe Rosemberg <rosemberg_at_[hidden]>
Date: Fri, 27 Dec 2019 11:04:04 +0000 (UTC)

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:step1: Evaluate condition. Continue to step2 if the condition is true
step2: Execute statementsstep3: return to step1
2. do:step1: Execute statementsstep2: Evaluate condition. Return to srep1 if the condition is true
3 for:step1: create a counterstep2: Evaluate condition. Continue to step3 if the condition is truestep3: Execute statementsstep4: Execute an mathematics operation (with the counter) and return to step2
What I see is we have a lack of one more that may help to make the c++ language more robust and elegant and that is a union of loop do and for to form a 'dofor' loop. It may have the same syntax of for loop but the condition is evaluated in the end not in the start of loop as do loop does.
dofor loop:step1: create a counterstep2: execute statementsstep3: 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>) { statements...

Also the step3 and step4 may be inlined on the assemble level and make this loop very efficient.

Best regards,TheArquitect

Received on 2019-12-27 05:06:32