Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+21 votes
asked in CSC211_Winter2018 by (8 points)

5 Answers

+15 votes

If the the first part of an if/else statement is true is does skip over the next parts.

answered by (8 points)
+15 votes

If you mean code like the following, then yes, if x < 5 is true, then execute its body to output an A and skip the rest. So x = 2 outputs A.

if (x < 5){
     System.out.print("A");
} else if (x < 10){
      System.out.print("B");
} else {
      System.out.print("C");
}

If you mean code like the following,

if (x < 5){
     System.out.print("A");
} else {
      System.out.print("B");
}
if (x < 10){
     System.out.print("C");
} else {
      System.out.print("D"); 
}
if (x < 20){
     System.out.print("E");
} else {
      System.out.print("F");
}

In this case, any value of x will ouput 3 letters because each if...else would output a letter.
If x = 2, output is ACE. if x = 8, ouput is BCE. If x = 21, output is BDF.

answered by (524 points)
+14 votes

If any of the if/else conditions are true you execute the block of code under that condition. Then you can skip the rest of the conditions.

answered by (8 points)
+13 votes

If the method contains an if/else if/else statement then if any part of the statement is true, then it will skip the rest. If the method has if/if/if statements then it would go through each if statement regardless true or false

answered by (8 points)
+9 votes

No if the statements you're putting is and if/else statement but if there is an if/else if syntax that doesn't apply.

answered by (8 points)
...