110 likes | 311 Views
Selection Statements. choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement selects one block or leaves it out based on boolean expression if-else statement selects one from two or more blocks based on boolean expressions
E N D
Selection Statements • choice of one among several blocks of code • Java supports 3 kinds of selection statements: • if statement • selects one block or leaves it out • based on boolean expression • if-else statement • selects one from two or more blocks • based on boolean expressions • switch statement • selects a block from • based on integer value
if Statement • syntax: • if (<boolean expression>) { //code} • the body can contain several statements • theexpression is evaluated • if it is true then the code is executed • if it is false then the code doesn't get executed • the next statement after curly bracket is executed afterwards • if there is any one statement in the body, you can omit {} • but we recommend to use the curly bracket in any case • it's less error prone
if-else Statement • syntax: • if (<boolean expression>) { //code1} else { //code2} • expression is evaluated • if it is true then the code1 is executed • if it is false then the code2 is executed
if-else Statement • syntax: • if (<boolean expression1>) { //code1 } else if (<boolean expression2>) { //code2 } else if (<boolean expression3>) { //code3 } else if (<boolean expressionN>) { //codeN } else { //codeN+1 } • expression1 is evaluated • if it is true then the code1 is executed • if it is false then expression2 is evaluated • if it is true then the code2 is executed • if it is false then expression3 is evaluated • etc., etc. • if all expressions are false then codeN+1 is executed • the last else can be omitted • then if all expressions are false none of the code-blocks is executed
Nested if Statements • if statements can occur within other if statements • e.g.: • if (<boolean expression1>) { if (<boolean expression2>) { //code1 } else { //code2 }} else { //code3 } • if statements can occur within else statements, too • e.g.: • if (<boolean expression1>) { //code1 } else { if (<boolean expression2>) { //code1 } else { //code2 }} • correct indenting is essential
Conditions in if Statements • don't use • if (b == true) { //code} • this is equivalent toif (b) { //code} • this is simpler and less error prone • if (b = true) {} • b is always true • similarly, don't use • if (b == false) { //code} • this is equivalent toif (! b) { //code} • again, this is simpler and less error prone • if (b = false) {} • b is always false • also: • if (b) {x = y;} else {x = z;} • is equivalent to x = b ? y : z;
switch Statement • chooses where to continue execution • syntax: • switch (<integer expression>) { case <integer constant1>: { //code1 } case <integer constant2>: { //code2 } case <integer constant3 >: { //code3 } case <integer constantN >: { //codeN } default: //codeN+1 }} • expression is evaluated and the result compared to all constants then execution continues at the code with the matching constantif no constant matches then at default: clause • default: clause can be omitted • last statement of each codemust be either break; or return; • otherwise execution continues with the code of following case: ! • switch is error prone; use it in disciplined way!
Disciplined switch Statement • switch (<integer expression>) { case <integer constant1>: { //code1 break; } case <integer constant2>: { //code2 break; }} • switch (<integer expression>) { case <integer constant1>: { //code1 return <expression>; } case <integer constant2>: { //code2 return <expression>; }} • we recommend break; or return;even in the last case: • what it we append another case: later!