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( 1 se Boolean Expression ·°····。···。··。······ °。。。。。。。。。 ··················.··着·····················。 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 ) else 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
Relational Operators //less than //less than or equal to equal to //not equal to //greater than //greater than or equal to =350 2大Math. PI radi 359.99 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 6-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 6 testScore = 350 30 //greater than >= //greater than or equal to
Compound statements r Use braces if the or block has multiple statements if (testscore 70 messageBox. show("You did not pass")i Then block messageBox. show(Try harder next time")i else messageBox. show("You did pass")i Else block message Box. show("Keep up the good work")i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 7 if (testScore or block has multiple statements. Then Block Else Block
Style guide f( ) else I Style 1 f( Style 2 else C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 6-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 8 if ( ) { … } else { … } Style Guide if ( ) { … } else { … } Style 1 Style 2
The if-then statement if( Boolean Expression testscore >=95 ····························································· Then block messageBox show("You are an honor student)i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 9 The if-then Statement if ( testScore >= 95 ) messageBox.show("You are an honor student"); if ( ) Then Block Boolean Expression
Control flow of if-then true testscore > 95? messageBox. show false C You are an honor student); C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 6-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 6 - 10 Control Flow of if-then testScore >= 95? false messageBox.show ("You are an honor student"); true