6

I have something like this

bool a = true;
bool b = true;
bool plot = true;
if(plot)
{
    if(a)
    {
        if(b)
            b = false;
        else
            b = true;
    //do some meaningful stuff here
    }
//some more stuff here that needs to be executed
}

I want to break out of the if statement that tests a when b turns false. Kind of like break and continue in loops. Any ideas? Edit: sorry forgot to include the big if statement. I want to break out of if(a) when b is false but not break out of if(plot).

nj-ath
  • 2,650
  • 2
  • 21
  • 41
Chris
  • 651
  • 1
  • 6
  • 15

3 Answers3

13

You can extract your logic into separate method. This will allow you to have maximum one level of ifs:

private void Foo()
{
   bool a = true;
   bool b = true;
   bool plot = true;

   if (!plot)
      return;

   if (a)
   {
      b = !b;
      //do something meaningful stuff here
   }

   //some more stuff here that needs to be executed   
}
Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
7
if(plot)
{
    if(a)
    {
        b= !b;
        if( b )
        {
            //do something meaningful stuff here
        }
    }
    //some more stuff here that needs to be executed
}
Denise Skidmore
  • 2,133
  • 18
  • 44
5
bool a = true;
bool b = true;
bool plot = true;
if(plot && a)
{
  if (b)
    b = false
  else
    b = true;

  if (b)
  {
    //some more stuff here that needs to be executed
  }
}

This should do what you want ..

It'sNotALie.
  • 20,741
  • 10
  • 63
  • 99
dotixx
  • 1,437
  • 12
  • 23