Click here to Skip to main content
15,039,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everybody.
i m stuck in logical approach to an algorithm flow.
inside code there is an if-then statement i want to be checked once, after return true then dont check again.
in details

C++
while(true)
{ do something     (A)
  
  if(something)
     {do something  (B)
     }

 
 do other things   (C)
}


during the process within while loop, conditions B may turn true multiple times but i want, once is true, always jump from A to C without checking the B again.

What I have tried:

i tried to insert a boolean variable that is turned to true once B statement is checked, but no success.
this variable is set outside the scope of if statement (B) and is not possible
Posted
Updated 31-Jan-21 20:46pm
v2

If I understand correctly, then this should work:
done = false

while(true)
{
   do something (A)

   if(!done && something)
   {
      do something (B)
      done = true
   }


   do other things (C)
}
   
v2
Comments
CPallini 1-Feb-21 2:07am
   
5.
Greg Utas 1-Feb-21 7:50am
   
Grazie.
Try:
C++
bool doneOnce = false;
while(true)
   { 
   DoSomethingA();

   if(!doneOnce && something)
      {
      DoSomethingB();
      doneOnce = true;
      }

   DoSomethingC();
   }
   
Comments
CPallini 1-Feb-21 2:07am
   
5
Just an advice: Learn better coding style.
Coding style is the way you write code, a good coding style makes your code easier to read, and in large code base, it matters.
Never try to save a keystroke by grouping things on same line, particularly never put code after a '{' , it makes code more difficult to read.
Some professional programmer editors (like Eclipse) have a code formatting feature.
C++
while(true)
{
    do something     (A)
    if(something)
    {
        do something  (B)
    }
    do other things   (C)
}

or
C++
while(true) {
    do something     (A)
    if(something) {
        do something  (B)
    }
    do other things   (C)
}


Advice: Learn to indent properly your code, it show its structure and it helps spotting structures mistakes.
Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900