I want to check a condition inside a loop and execute a block of code when it's first met. After that, the loop might repeat but the block should be ignored. Is there a pattern for that? Of course it's easy to declare a flag outside of the loop. But I I'm interested in an approach that completely lives inside the loop.
This example is not what I want. Is there a way to get rid of the definition outside of the loop?
bool flag = true;
for (;;) {
if (someCondition() && flag) {
// code that runs only once
flag = false;
}
// code that runs every time
}
if (flag) ...
(orif (!flag) ...
. This is the idiomatic (typical) way to do do this. – Floaterwhile(1){ if(level > 7){ once([=]{ award(); }); } }
– Pulverulent