How to escape from an if statement from a boolean in an if statement

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, which checks a when b becomes false. A view is like break and continues in cycles. Any ideas? Edit: sorry forgot to include the big if statement. I want to exit if (a) when b is false, but not break out of if (plot).

+6
source share
3 answers
 if(plot) { if(a) { b= !b; if( b ) { //do something meaningful stuff here } } //some more stuff here that needs to be executed } 
+7
source

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

 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 } 
+13
source
 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.

+5
source

Source: https://habr.com/ru/post/949761/


All Articles