Chapter 6 Selection Statements 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 6-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 1 Chapter 6 Selection Statements
Chapter 6 objectives After, you have read and studied this chapter, you shoula be able to e Implement selection control in a program using if statements e Implement selection control in a program using switch statements e Write boolean expressions using relational and boolean operators. e Evaluate given boolean expressions correctly e Nest an if statement inside another if statement's then or else part correctly e Choose the appropriate selection control statement for a gIven task e Write applications using the List Box class from javabook and the Color class from the standard java. awt package C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 2 Chapter 6 Objectives After you have read and studied this chapter, you should be able to Implement selection control in a program using if statements. Implement selection control in a program using switch statements. Write boolean expressions using relational and boolean operators. Evaluate given boolean expressions correctly. Nest an if statement inside another if statement’s then or else part correctly. Choose the appropriate selection control statement for a given task. Write applications using the ListBox class from javabook and the Color class from the standard java.awt package
The if statement //Assume messageBox and inputBox are declared and created //Assume testscore is declared testscore inputBox. getInteger("Enter test score: )i f(testscore 70) This statement is messageBox. show("You did not pass)i executed if the testscore ess than 70 else This statement is messageBox. show("You did pass") executed if the testscore is 70 or higher C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 3 The if Statement //Assume messageBox and inputBox are declared and created //Assume testScore is declared testScore = inputBox.getInteger("Enter test score:"); if (testScore < 70) messageBox.show("You did not pass"); else messageBox.show("You did pass"); This statement is executed if the testScore is 70 or higher. This statement is executed if the testScore is less than 70
Syntax for the if statement if( <boolean expression> <then block> 1 se Boolean Expression <else block> ·°····。···。··。······ °。。。。。。。。。 ··················.··着·····················。 Then block messageBox. show("You did not pass")i else Else block messageBox. show( You did pass )i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 4 if ( testScore < 70 ) messageBox.show("You did not pass"); else messageBox.show("You did pass"); Syntax for the if Statement if ( <boolean expression> ) <then block> else <else block> Then Block Else Block Boolean Expression
Control flow se true testscore< 70? messageBox. show messageBox.show C You did pass"); C You did not pass"); C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 6-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 5 Control Flow messageBox.show ("You did pass"); false testScore < 70 ? messageBox.show ("You did not pass"); true