chapter 11 Flashcards

1
Q

what happens in a switch statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is a break in a case of a switch statement and how does it work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is “fall through” in a switch statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

does every switch statement must contain breaks for every case and default clause??

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the basic format form for writing a switch statement?

A

switch(Expression){
case FirstValue;
Statements

case SecondValue;
MoreStatements

//…more cases…

default: EvenMoreStatements
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what can you use for the Expression within the cases of a switch statement?

A

variables such as; byte, short, int, long, char or a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

does the expression within a switch statement, need to be only one variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

do you need to have a case for each expected value of the expression?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

does every switch need a default clause?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how is a switch statement not as versatile than using if statements?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a conditional operator and how does it work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly