Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following three code snippets: Version 1 if (x == 1) { System.out.p

ID: 3538135 • Letter: C

Question

Consider the following three code snippets:


Version 1


if (x == 1) {
        System.out.println("Siege Tank");
}
if (x == 2) {
        System.out.println("Goliath");
}
if (x == 3) {
        System.out.println("Battlecruiser");
}  

Version 2


if (x == 1) {
        System.out.println("Siege Tank");
} else if (x == 2) {
        System.out.println("Goliath");
} else if (x == 3) {
        System.out.println("Battlecruiser");
}


Version 3


if (x == 1) {
        System.out.println("Siege Tank");
} else {
      if (x == 2) {
        System.out.println("Goliath");
      } else {
            if (x == 3) {
            System.out.println("Battlecruiser");
            }   
      }
}


Do all three versions produce the same output? Do all three versions make your computer take the same sequence of actions ( in terms of which boolean expressions are evaluated)? Explain your answer.


Explanation / Answer

yes all the 3 gives you the same output. but its a better to use the 2as its a better programing style