1 / 8

Selection Statements

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

reinhardtj
Download Presentation

Selection Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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;

  7. 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!

  8. 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!

More Related