Download presentation
Presentation is loading. Please wait.
Published byCory Robbins Modified over 9 years ago
1
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 1 The if Statement if ( testScore >= 95 ) { System.out.println("You are a good student"); } if ( ) { } Then Block Boolean Expression
2
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 2 Control Flow of if testScore >= 95? false System.out.println ("You are a good student"); System.out.println ("You are a good student"); true
3
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 3 Example Program Praff … out to reality … If.java
4
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 4 The if-else Statement //Assume messageBox and inputBox are declared and created //Assume testScore is declared testScore = SavitchIn.readLineDouble(); if (testScore < 50) { System.out.println("You did not pass"); } else { System.out.println("You did pass"); } This statement is executed if the testScore is 50 or higher. This statement is executed if the testScore is less than 50.
5
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 5 if (testScore < 50) { System.out.println("You did not pass"); } else { System.out.println("You did pass"); } Syntax for the if-else Statement if ( ) { } else { } Then Block Else Block Boolean Expression
6
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 6 Control Flow System.out.println ("You did pass"); System.out.println ("You did pass"); false testScore < 50 ? System.out.println ("You did not pass"); System.out.println ("You did not pass"); true
7
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 7 Example Program Aaaieeeeooo … out to reality … IfElse.java
8
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 8 The Nested-if Statement The then and else block of an if statement can contain any valid statements, including other if statements. An if statement containing another if statement is called a nested-if statement. if (testScore >= 50) { if (studentAge < 10) { System.out.println("You did a great job"); } else { System.out.println("You did pass"); } } else { //test score < 70 System.out.println("You did not pass"); }
9
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 9 Control Flow of Nested-if Statement System.out.println ("You did not pass"); System.out.println ("You did not pass"); false inner if System.out.println ("You did pass"); System.out.println ("You did pass"); false testScore >= 50 ? true studentAge < 10 ? System.out.println ("You did a great job"); System.out.println ("You did a great job"); true
10
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 10 Example Program Zzzzzzatong … out to reality … NestedIf.java
11
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 11 if (testScore < 70) { messageBox.show("You did not pass"); messageBox.show("Try harder next time"); } else { messageBox.show("You did pass"); messageBox.show("Keep up the good work"); } Compound Statements You have to use braces if the or block has multiple statements. Then Block Else Block
12
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 12 if ( ) { … } else { … } Style Guide if ( ) { … } else { … } Style 1 Style 2
13
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 13 if - else- if if (score >= 85) { System.out.println(”Grade is A"); } else { if (score >= 75) { System.out.println(”Grade is B"); } else { if (score >= 65) { System.out.println(”Grade is C"); } else { if (score >= 50) { System.out.println(”Grade is D"); } else { System.out.println(”Grade is N"); } Test ScoreGrade 85 score A 75 score 85 B 65 score 75 C 50 score 65 D score 50 N
14
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 14 if - else- if if (score >= 85) { System.out.println(”Grade is A"); } else if (score >= 75) { System.out.println(”Grade is B"); } else if (score >= 65) { System.out.println(”Grade is C"); } else if (score >= 50) { System.out.println(”Grade is D"); } else { System.out.println(”Grade is N"); } Test ScoreGrade 85 score A 75 score 85 B 65 score 75 C 50 score 65 D score 50 N
15
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 15 Example Program Ooorgh … out to reality … IfElseIf.java
16
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 16 Matching else if (x < y) if (x < z) System.out.println("Hello"); else System.out.println("Good bye"); if (x < y) { if (x < z) { System.out.println("Hello"); } else { System.out.println("Good bye"); } really means
17
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 17 Matching else if (x < y) { if (x < z) System.out.println("Hello"); } else { System.out.println("Good bye"); } if (x < y) { if (x < z) { System.out.println("Hello"); } } else { System.out.println("Good bye"); } means
18
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 18 Example Program Kleeeptong … out to reality … MatchingElse.java
19
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 19 The switch Statement char standing; System.out.println("(F)reshman, (S)ophmore, (J)unior, s(E)nior : "); standing = SavitchIn.readLineNonwhiteChar(); switch (standing) { case 'F': System.out.println("Go to the Wellness Center"); break; case 'S': System.out.println("Go to the Cox Building"); break; case 'J': System.out.println("Go to Ashe"); break; case 'E': System.out.println("Work it out yourself"); break; } This statement is executed if the standing is equal to 'F'. This statement is executed if the standing is equal to 'E'.
20
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 20 Syntax for the switch Statement switch ( fanSpeed ) { case 1: System.out.println("That's low"); break; case 2: System.out.println("That's medium"); break; case 3: System.out.println("That's high"); break; } switch ( ) { : … : } Case Body Arithmetic Expression Case Label
21
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 21 switch With break Statements switch ( N ) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;
22
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 22 Example Program Vrootytoot … out to reality … Switch.java
23
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 23 The switch Statement with default switch ( binaryDigit ) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); break; default: System.out.println("That's not a binary digit"); break; } switch ( ) { : … : default: }
24
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 24 Example Program Nic … out to reality … SwitchDefault.java
25
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 25 switch With No break Statements switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true
26
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter 6 - 26 Example Program Lapa-lapa … out to reality … SwitchFall.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.