Selection Statements Selection Statements Selection statements allows our

  • Slides: 14
Download presentation
Selection Statements

Selection Statements

Selection Statements ► Selection statements allows our program to choose different paths of execution

Selection Statements ► Selection statements allows our program to choose different paths of execution based on the outcome of an expression or the state of the variable. ► The expression or the value upon which a decision is made is always a boolean value(true or false). ► 2 kinds of selection statements § if or if…else § switch

The if structure ►A single if statement is sometimes called a singlealternate if because

The if structure ►A single if statement is sometimes called a singlealternate if because we only perform an action based on one alternative. syntax: if( condition ) statement; or if(condition ){ statement 1; statement 2; }

Example: int grade = 68; if( grade > 60 ) System. out. println("Congratulations!"); ------------------------------------------

Example: int grade = 68; if( grade > 60 ) System. out. println("Congratulations!"); ------------------------------------------ int grade = 68; if( grade > 60 ){ System. out. println("Congratulations!"); System. out. println("You passed!"); }

The if…else structure ► The if…else structure is a dual-alternate if, which requires two

The if…else structure ► The if…else structure is a dual-alternate if, which requires two options for the next course of action. ► It provides the mechanism to perform one action when a boolean expression evaluates as true and performs another action when the boolean expression evaluates as false

syntax: if( boolean_expression ){ statement 1; statement 2; . . . } else{ statement

syntax: if( boolean_expression ){ statement 1; statement 2; . . . } else{ statement 3; statement 4; . . . }

Example: int grade = 68; if( grade > 60 ) System. out. println("Congratulations!"); else

Example: int grade = 68; if( grade > 60 ) System. out. println("Congratulations!"); else System. out. println("Sorry you failed"); ----------------------------------------------- int grade = 68; if( grade > 60 ){ System. out. println("Congratulations!"); System. out. println("You passed!"); } else{ System. out. println("Sorry you failed"); }

Nested if…else Structure | syntax: if( condition 1 ) statement 1; else if( condition

Nested if…else Structure | syntax: if( condition 1 ) statement 1; else if( condition 2 ) statement 2; else statement 3; | | | | | Example: int grade = 68; if( grade > 90 ){ System. out. println("Very good!"); } else if( grade > 60 ){ System. out. println("Very good!"); } else{ System. out. println("Sorry you failed"); }

Common Errors 1. The condition inside the if-statement does not evaluate to a boolean

Common Errors 1. The condition inside the if-statement does not evaluate to a boolean value. For example, //WRONG int number = 0; if( number ){ //some statements here } v The variable number does not hold a boolean value. 2. Writing elseif instead of else if.

3. Using = instead of == for comparison. For example, //WRONG int number =

3. Using = instead of == for comparison. For example, //WRONG int number = 0; if( number = 0 ){ //some statements here } This should be written as, //CORRECT int number = 0; if( number == 0 ){ //some statements here }

Example: public class Grade { public static void main( String[] args ) { double

Example: public class Grade { public static void main( String[] args ) { double grade = 92. 0; if( grade >= 90 ){ System. out. println( "Excellent!" ); } else if( (grade < 90) && (grade >= 80)){ System. out. println("Good job!" ); } else if( (grade < 80) && (grade >= 60)){ System. out. println("Study harder!" ); } else{ System. out. println("Sorry, you failed. "); } } }

The switch structure ► The switch statement is useful when we need to test

The switch structure ► The switch statement is useful when we need to test a single variable against a series of exact integer or character values. § keyword switch starts the structures and is followed immediately by a test expression enclosed in parenthesis § keyword case is followed by one of the possible values for the test expression and a colon § keyword break optionally terminates values of the test expression and a colon § keyword default optionally used to prior to any action that should occur if the test variable does not match any case

syntax: switch( switch_expression ){ case_selector 1: statement 1; // statement 2; //block 1 break;

syntax: switch( switch_expression ){ case_selector 1: statement 1; // statement 2; //block 1 break; case_selector 2: statement 1; // statement 2; //block 2 break; : default: statement 1; // statement 2; //block n }

Example: public class Grade { public static void main( String[] args ) { int

Example: public class Grade { public static void main( String[] args ) { int grade = 92; switch(grade){ case 100: System. out. println( "Excellent!" ); break; case 90: System. out. println("Good job!" ); break; case 80: System. out. println("Study harder!" ); break; default: System. out. println("Sorry, you failed. "); } } }