c# - One 'else' for nested 'if' statements -


i've got problem can simplified this:

parameters: a, b  if (a > 5) {     print("very well, > 5");      if (b > 7)         print("even better, b > 7");     else     {         print("i don't variables");     } } else {     print("i don't variables"); } 

i use 1 else instead of 2 since same code. thought of creating additional method, return combined true`false`, serious overkill.

another option goto, make code less readable , unsafe.

what way it, avoiding checking same condition many times , making readable possible?

boolean logic 101:

public void test(int a, int b, int c) {     boolean = true;     if (good = && > 5) {         system.out.println("very well, > 5");     }     if (good = && b > 7) {         system.out.println("even better, b > 7");     }     if (good = && c > 13) {         system.out.println("even better, c > 13");     }     // have many conditions need, ,     if (!good) {         system.out.println("i don't variables");     } } 

alternatively - if want loads of checks -

enum tests {     a_gt_5 {         @override         boolean test(int a, int b, int c) {             return > 5;         }     },     b_gt_7 {         @override         boolean test(int a, int b, int c) {             return b > 7;         }     },     c_gt_13 {         @override         boolean test(int a, int b, int c) {             return c > 13;         }     };      abstract boolean test (int a, int b, int c); }  public void test(int a, int b, int c) {     boolean = true;     ( tests t : tests.values() ) {         = && t.test(a, b, c);         if (!good) {             break;         }     }     if (!good) {         system.out.println("i don't variables");     } } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -