1 / 26

The if Statement

Learn how to use if-else statements in Java programming, including nested if statements and compound statements. Explore examples and syntax guides.

dboyster
Download Presentation

The if Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Boolean Expression Then Block The if Statement if ( <boolean expression> ) { <then block> } if ( testScore >= 95 ) { System.out.println("You are a good student"); } Introduction to Object-Oriented Programming with Java--Wu

  2. true testScore >= 95? System.out.println ("You are a good student"); false Control Flow of if Introduction to Object-Oriented Programming with Java--Wu

  3. Example Program • Praff … out to reality … If.java Introduction to Object-Oriented Programming with Java--Wu

  4. //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 less than 50. This statement is executed if the testScore is 50 or higher. The if-else Statement Introduction to Object-Oriented Programming with Java--Wu

  5. if (testScore < 50) { System.out.println("You did not pass"); } else { System.out.println("You did pass"); } Boolean Expression Then Block Else Block Syntax for the if-else Statement if ( <boolean expression> ) { <then block> } else { <else block> } Introduction to Object-Oriented Programming with Java--Wu

  6. true false testScore < 50 ? System.out.println ("You did pass"); System.out.println ("You did not pass"); Control Flow Introduction to Object-Oriented Programming with Java--Wu

  7. Example Program • Aaaieeeeooo … out to reality … IfElse.java Introduction to Object-Oriented Programming with Java--Wu

  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"); } Introduction to Object-Oriented Programming with Java--Wu

  9. inner if true false testScore >= 50 ? false true studentAge < 10 ? System.out.println ("You did not pass"); System.out.println ("You did pass"); System.out.println ("You did a great job"); Control Flow of Nested-if Statement Introduction to Object-Oriented Programming with Java--Wu

  10. Example Program • Zzzzzzatong … out to reality … NestedIf.java Introduction to Object-Oriented Programming with Java--Wu

  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 <then> or <else> block has multiple statements. Then Block Else Block Introduction to Object-Oriented Programming with Java--Wu

  12. if ( <boolean expression> ) { … } else { … } if ( <boolean expression> ) { … } else { … } Style Guide Style 1 Style 2 Introduction to Object-Oriented Programming with Java--Wu

  13. 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"); } } } } if - else- if Introduction to Object-Oriented Programming with Java--Wu

  14. 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"); } if - else- if Introduction to Object-Oriented Programming with Java--Wu

  15. Example Program • Ooorgh … out to reality … IfElseIf.java Introduction to Object-Oriented Programming with Java--Wu

  16. 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"); Matching else really means Introduction to Object-Oriented Programming with Java--Wu

  17. if (x < y) { if (x < z) { System.out.println("Hello"); } } else { System.out.println("Good bye"); } Matching else if (x < y) { if (x < z) System.out.println("Hello"); } else { System.out.println("Good bye"); } means Introduction to Object-Oriented Programming with Java--Wu

  18. Example Program • Kleeeptong … out to reality … MatchingElse.java Introduction to Object-Oriented Programming with Java--Wu

  19. 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'. The switch Statement Introduction to Object-Oriented Programming with Java--Wu

  20. Arithmetic Expression 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; } Case Label Case Body Syntax for the switch Statement switch ( <arithmetic expression> ) { <case label 1> : <case body 1> … <case label n> : <case body n> } Introduction to Object-Oriented Programming with Java--Wu

  21. true N == 1 ? x = 10; false break; true N == 2 ? switch ( N ) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 20; false break; true N == 3 ? x = 30; false break; switch With break Statements Introduction to Object-Oriented Programming with Java--Wu

  22. Example Program • Vrootytoot … out to reality … Switch.java Introduction to Object-Oriented Programming with Java--Wu

  23. 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; } The switch Statement with default switch ( <arithmetic expression> ) { <case label 1> : <case body 1> … <case label n> : <case body n> default: <default body> } Introduction to Object-Oriented Programming with Java--Wu

  24. Example Program • Nic … out to reality … SwitchDefault.java Introduction to Object-Oriented Programming with Java--Wu

  25. true N == 1 ? x = 10; false switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } true N == 2 ? x = 20; false true N == 3 ? x = 30; false switch With No break Statements Introduction to Object-Oriented Programming with Java--Wu

  26. Example Program • Lapa-lapa … out to reality … SwitchFall.java Introduction to Object-Oriented Programming with Java--Wu

More Related