Rewrite this if/else if code segment into a switch statement int num = 0; int a = 10, b = 20, c = 20, d = 30, x = 40; if (num &g t; 101 && num <= 105) { a += 1; } else if (num == 208) { b += 1; x = 8; } else if (num > 208 && num < 210) { c = c * 3; } else { d += 1004; }
1 answer:
Answer:
public class SwitchCase {
public static void main(String[] args) {
int num = 0;
int a = 10, b = 20, c = 20, d = 30, x = 40;
switch (num){
case 102: a += 1;
case 103: a += 1;
case 104: a += 1;
case 105: a += 1;
break;
case 208: b += 1; x = 8;
break;
case 209: c = c * 3;
case 210: c = c * 3;
break;
default: d += 1004;
}
}
}
Explanation:
Given above is the equivalent code using Switch case in Java The switch case test multiple levels of conditions and can easily replace the uses of several if....elseif.....else statements. When using a switch , each condition is treated as a separate case followed by a full colon and the the statement to execute if the case is true. The defaul t statement handles the final else when all the other coditions are false
You might be interested in
Answer:
Insert
Explanation:
Insert
Answer: Question 12: Answer is tables
Question 13: Answer is Command + C
Explanation:
Answer:
A
Explanation:
© is the symbol for copyright or Copr. for abbreviation.
Answer:
Explanation:
The one which best describes it is Co-opetition. Co-opetition often takes place when companies which are in the same market work put aside their differences and work together in the gathering of knowledge and research of new products, but they still compete for market-share of their products and in the application of the joint knowledge created.
Answer: Website Bar
Explanation: