logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Jun 27, 2019
      ·  Edited: Nov 04, 2020

    4.6 How to handle multiple branches correctly?

    in Introduction to Java

    A. Introduction:

    Sometimes, nested if statements may have many layers. For example, if you want to write a program to display the effect of an earthquake, as measured by the Richter scale below.

    You will need 4 nested if statements

    The code may look like:

    if (richter >= 8.0) {
      description = "Most structures fall";
    } else if (richter >= 7.0) {  description = "Many buildings destroyed";
    } else if (richter >= 6.0) {  description = "Many buildings considerably damaged, some collapse"; 
    } else if (richter >= 4.5) {  description = "Damage to poorly constructed buildings"; 
    } else {  description = "No destruction of buildings"; }

    (1). Notice if you reverse the order of if statements, the results will be wrong.

    if (richter >= 4.5) // Tests in wrong order {
      description = "Damage to poorly constructed buildings"; 
      } else if (richter >= 6.0) {  description = "Many buildings considerably damaged, some collapse"; 
      } else if (richter >= 7.0) {  description = "Many buildings destroyed"; 
      } else if (richter >= 8.0) {  description = "Most structures fall"; }

    (2). Skipping the "else" statements will also be wrong:

    // Didn’t use else 
    if (richter >= 8.0) {  description = "Most structures fall"; } 
    if (richter >= 7.0) {  description = "Many buildings destroyed"; } 
    if (richter >= 6.0) {  description = "Many buildings considerably damaged, some collapse"; } 
    if (richter >= 4.5) {  "Damage to poorly constructed buildings"; }

    (3). Sometimes, to keep clarity, we use a "switch" statement instead of a nested if statements. For example, to convert an Arabic digit to a Roman numeral:

    String digitR = "";
    int digit = ...; 
    if (digit == 1) { digitR = "I"; } 
    else if (digit == 2) { digitR = "II"; } 
    else if (digit == 3) { digitR = "III"; } 
    else if (digit == 4) { digitR = "IV"; } 
    else if (digit == 5) { digitR = "V"; } 
    else if (digit == 6) { digitR = "VI"; } 
    else if (digit == 7) { digitR = "VII"; } 
    else if (digit == 8) { digitR = "VIII"; } 
    else if (digit == 9) { digitR = "IX"; } 
    else { System.out.println("The Arabic digit is wrong!"); } 

    You can also use:

    String digitR = "";
    int digit = . . .; 
    switch (digit) {  // the variable can be of type int, char or String
    case 1: digitR = "I"; break;  // case 1 will be case '1' or case "1" if the switch variable is char or String
    case 2: digitR = "II"; break;  
    case 3: digitR = "III"; break;  
    case 4: digitR = "IV"; break;  
    case 5: digitR = "V"; break;  
    case 6: digitR = "VI"; break;  
    case 7: digitR = "VII"; break;  
    case 8: digitR = "VII"; break;  
    case 9: digitR = "IX"; break;  
    default: System.out.println("The Arabic digit is wrong!"); }

    0 comments
    0
    0 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners