Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.

Similar presentations


Presentation on theme: "Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves."— 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 ( ) { //code } – the body can contain several statements the expression 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 ( ) { //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 ( ) { //code1 } else if ( ) { //code2 } else if ( ) { //code3 } else if ( ) { //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 ( ) { if ( ) { //code1 } else { //code2 } } else { //code3 } if statements can occur within else statements, too – e.g.: if ( ) { //code1 } else { if ( ) { //code1 } else { //code2 } } correct indenting is essential

6 Conditions in if Statements don't use if (b == true) { //code } this is equivalent to if (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 to if (! 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 ( ) { case : { //code1 } case : { //code2 } case : { //code3 } case : { //codeN } default: //codeN+1 } } expression is evaluated and the result compared to all constants then execution continues at the code with the matching constant if no constant matches then at default: clause default: clause can be omitted last statement of each code must 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 ( ) { case : { //code1 break; } case : { //code2 break; } } – switch ( ) { case : { //code1 return ; } case : { //code2 return ; } } we recommend break; or return; even in the last case: – what it we append another case: later!


Download ppt "Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves."

Similar presentations


Ads by Google