Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAM FLOWCHART Selection Statements.

Similar presentations


Presentation on theme: "PROGRAM FLOWCHART Selection Statements."— Presentation transcript:

1 PROGRAM FLOWCHART Selection Statements

2 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

3 The if structure A single if statement is sometimes called a single-alternate if because we only perform an action based on one alternative. syntax: if( condition ) statement; or if(condition ){ statement1; statement2; }

4 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!"); }

5 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

6 syntax: if( boolean_expression ){ statement1; statement2; . . . } else{ statement3; statement4;

7 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");

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

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

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

11 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.");

12 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

13 syntax: switch( switch_expression ){ case case_selector1: statement1;// statement2;//block 1 break; case case_selector2: statement2;//block 2 : default: statement2;//block n }

14 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!" ); case 80: System.out.println("Study harder!" ); default: System.out.println("Sorry, you failed."); }


Download ppt "PROGRAM FLOWCHART Selection Statements."

Similar presentations


Ads by Google