260 likes | 271 Views
Learn how to use if-else statements in Java programming, including nested if statements and compound statements. Explore examples and syntax guides.
E N D
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
true testScore >= 95? System.out.println ("You are a good student"); false Control Flow of if Introduction to Object-Oriented Programming with Java--Wu
Example Program • Praff … out to reality … If.java Introduction to Object-Oriented Programming with Java--Wu
//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
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
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
Example Program • Aaaieeeeooo … out to reality … IfElse.java Introduction to Object-Oriented Programming with Java--Wu
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
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
Example Program • Zzzzzzatong … out to reality … NestedIf.java Introduction to Object-Oriented Programming with Java--Wu
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
if ( <boolean expression> ) { … } else { … } if ( <boolean expression> ) { … } else { … } Style Guide Style 1 Style 2 Introduction to Object-Oriented Programming with Java--Wu
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
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
Example Program • Ooorgh … out to reality … IfElseIf.java Introduction to Object-Oriented Programming with Java--Wu
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
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
Example Program • Kleeeptong … out to reality … MatchingElse.java Introduction to Object-Oriented Programming with Java--Wu
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
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
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
Example Program • Vrootytoot … out to reality … Switch.java Introduction to Object-Oriented Programming with Java--Wu
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
Example Program • Nic … out to reality … SwitchDefault.java Introduction to Object-Oriented Programming with Java--Wu
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
Example Program • Lapa-lapa … out to reality … SwitchFall.java Introduction to Object-Oriented Programming with Java--Wu