Boolean Expressions and If Statements Flow of Control
Boolean Expressions and If Statements
Flow of Control System. out. print("Enter your major: "); major = keyboard. next. Line(); System. out. print("How may credit hours have you taken? "); hours. Taken = keyboard. next. Int(); Sequential System. out. print("How many grade points have you earned? "); points. Earned = keyboard. next. Double(); gpa = points. Earned / hours. Taken; hours. Remaining = GRADUATION_HRS - hours. Taken; System. out. println("Your GPA is: " + gpa); System. out. println("You need " + hours. Remaining + " more credit hours to complete your degree in " + major);
Conditional Execution
Conditional Execution Is it cold outside? Java code Yes Wear a coat. if (cold. Outside) wear. Coat();
Boolean Expression? Yes Statement if ( Boolean. Expression ) Statement;
Is it cold outside? Yes Wear a coat. Wear a hat. Wear gloves.
Is it cold outside? Yes Wear a coat. Wear a hat. Wear gloves. if (cold. Outside) { wear. Coat(); wear. Hat(); wear. Gloves(); }
Think/Pair/Share Write down at least two instances where you might have used a conditional in one of your previous lab assignments or pair exercises.
Relational Operators
Relational Operator > < >= <= == != Meaning is greater than is less than is greater than or equal to is less than or equal to is not equal to
Boolean Expression Meaning x > y x < y x >= y Is x greater than y? x <= y x == y x != y Is x less than or equal to y. Is x less than y? Is x greater than or equal to y? Is x not equal to y?
Relational Operators - Practice Value of x Value of y Expression Result 12 2 x + 3 <= y * 10 _____ 7 1 x + 3 != y * 10 _____ 17 2 x + 3 == y * 10 _____ 100 5 x + 3 >= y * 10 _____
System. out. print("Enter your major: "); major = keyboard. next. Line(); System. out. print("How may credit hours have you taken? "); hours. Taken = keyboard. next. Int(); System. out. print("How many grade points have you earned? "); points. Earned = keyboard. next. Double(); gpa = points. Earned / hours. Taken; hours. Remaining = GRADUATION_HRS - hours. Taken; System. out. println("Your GPA is: " + gpa); if ( gpa >= 3. 7 ) System. out. println("That’s a great gpa!"); System. out. println("You need " + hours. Remaining + " more credit hours to complete your degree in " + major); if ( hours. Remaining < 0 ) { System. out. println("Isn’t it time for you to graduate? "); System. out. println("Go see your advisor ASAP!"); }
System. out. print("Enter your major: "); major = keyboard. next. Line(); System. out. print("How may credit hours have you taken? "); hours. Taken = keyboard. next. Int(); System. out. print("How many grade points have you earned? "); points. Earned = keyboard. next. Double(); gpa = points. Earned / hours. Taken; hours. Remaining = GRADUATION_HRS - hours. Taken; System. out. println("Your GPA is: " + gpa); REMEMBER: Indentation does not a block make!!! if ( gpa >= 3. 7 ) System. out. println(“That’s a great gpa!”); System. out. println("You need " + hours. Remaining + " more credit hours to complete your degree in " + major); if ( hours. Remaining < 0 ) System. out. println(“Isn’t it time for you to graduate? ”); System. out. println(“Go see your advisor ASAP!”); What’s the output if hours. Remaining is 20?
If Practice
Write an if statement to calculate quiz. Ave (sum divided by num. Quizzes) if num. Quizzes is greater than 0.
Write an if statement to calculate tax (total. Sales times TAXRATE) if the user input for. Profit is’Y’.
Write an if statement to print “Play golf!” and set cost to $75 if the temperature is above 70 degrees.
if-else
No Wear shorts. Is it cold outside? Yes Wear a coat. if ( boolean. Expression ) statement. Or. Block. If. True; else statement. Or. Block. If. False;
System. out. print("Enter your major: "); major = keyboard. next. Line(); System. out. print("How may credit hours have you taken? "); hours. Taken = keyboard. next. Int(); System. out. print("How many grade points have you earned? "); points. Earned = keyboard. next. Double(); if ( hours. Taken > 0 ) gpa = points. Earned / hours. Taken; else gpa = 0. 0; hours. Remaining = GRADUATION_HRS - hours. Taken; System. out. println("Your GPA is: " + gpa); System. out. println("You need " + hours. Remaining + " more credit hours to complete your degree in " + major);
if-else Practice
Write an if-else statement to print “passing grade” if quiz. Ave is at least 70%, otherwise print “work harder”.
Write an if-else statement to calculate tax (total. Sales times TAXRATE) if the user input for. Profit is’Y’, otherwise set tax to 0.
Write an if-else statement to print “Go Swimming!” and set cost to $5 if the temperature is at least 80 degrees, otherwise print “Take a hike” and set cost to $0
if and if-else Pair Programming Practice If. Practice. java
Next time… • • Comparing characters Boolean flags Logical operators – and or and not Nested if-else statements
Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.
- Slides: 28