chapter 11 Flashcards
what happens in a switch statement?
a switch statement is made up of cases and depending on the input for the switch statement will determine which case is selected or if none, a default clause can be selected outside of the switch statement.
what is a break in a case of a switch statement and how does it work?
if the input matches a case in the switch statement, then if the case has a break it will jump out of the switch statement to continue with the rest of your code whether there is a default or not or other codes in your program.
what is “fall through” in a switch statement?
if there are no breaks in any or some cases, and the input for the switch statement matches some cases then as long as there are no breaks it will execute the case that is selected plus any other subsequent case as long as there are no breaks and also whatever comes after the switch statement.
does every switch statement must contain breaks for every case and default clause??
no breaks are optional and you can put them wherever and the last break in the default is not necessary but you can put it for overall tidiness.
what is the basic format form for writing a switch statement?
switch(Expression){
case FirstValue;
Statements
case SecondValue;
MoreStatements
//…more cases…
default: EvenMoreStatements
}
what can you use for the Expression within the cases of a switch statement?
variables such as; byte, short, int, long, char or a string
does the expression within a switch statement, need to be only one variable?
You can use multiple variables inside that expression if they combine to produce a single value. but You can’t use multiple expressions or multiple variables in the switch itself.
do you need the cases in a switch statement to be in order? and why wouldn’t you want them to be in an unorderly fashion?
yes you don’t have to have the cases in the right order but to keep them in order will be easier to read and understand the program.
do you need to have a case for each expected value of the expression?
no you dont. if it is expected that the value of the expression contains A, B, C, D, and in the cases clause your only have A, B, and D. that is fine as some expected vaules such as D will be set to the default so it will jump out of the switch to perform a default if there is one or to continue the rest of the code.
does every switch need a default clause?
no it is optional and if there isn’t any, and no case is selected, then the code will continue to run until the end of the program.
how is a switch statement not as versatile than using if statements?
with if statements you can use a condition for the input but in a switch statement’s expression for the input you cannot use a condition.
What is a conditional operator and how does it work?
A conditional operator consists of a colon and a question mark.
If the stuff before the question mark is true, the whole expression’s value is whatever comes between the question mark and the colon.
If the stuff before the question mark is false, the whole expression’s value is whatever comes after the colon.