7

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
  • 3,028
  • 2
  • 25
  • 41
Chris
  • 681
  • 1
  • 6
  • 16

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
  • 232,247
  • 41
  • 429
  • 459
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,286
  • 22
  • 51
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.
  • 22,289
  • 12
  • 68
  • 103
dotixx
  • 1,490
  • 12
  • 23