You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
928 B

11 months ago
When should you use break or continue in a loop?
When it makes your code easier to read/maintain/understand as opposed to working it into the boolean condition for the loop.
The danger of using break or continue in a large loop is that it becomes 'hidden' and people might not realize that something inside the loop affects its control flow.
What are the advantages/disadvantages of switch...case?
+ The expression is only checked once in one place, and would only need to changed in one place
- You have to keep putting 'break;' at the end of each case if you don't want the case to "fall through". If you forget, it still compiles.
+ You can exploit the fall-through behavior, as follows:
for(int days = 1; days <= 12; days++) {
switch (days) {
case 12: ...three french hens...
...
case 3: ...three french hens...
case 2: ...two turtle doves...
case 1: ...partridge in a pear tree...
}
}