Chapter 3: Program Statements Notes
Control Flow:
- A conditional statement is sometimes called a selection statement.
- Each decision is based on a boolean expression.
- Three types of loop statements: while, do and for statements.
The if statement:
Shorthand “if” statement: a ternary operator, inline if a or ternary
y = ( x <= 0 ) ? x : -x
which is a convenient way of writing
if ( x <= 0 )
- Equality operators
- Relational operators
if (a < b) if (a < c) x = a; else x = c; else if (b < c) x = b; else x = c;
Classwork: you can use this code snippet to write a program minMax_YI.java to prompt the user for three numbers and displays them in order from smallest to largest.Write the test/driver program if you use OOD.
NOTE: Assume the numbers are different. Include this assumption in your documentation.
Boolean expressions:
Logical operators:
Truth Table:
Logical AND and OR operators:
Example of truth table for a specific condition:
Booelean Expression Exercise: Work on this boolean expression using the table attached here Builtintypestruthtable2
. Visit Canvas to access the doc version.
While Statement:
while ( condition ) { // statements block }
A common mistake: an infinite loop. The loop will keep going forever.
The do loop:
do { // statements block } while ( condition );
The for loop:
for ( initialization; condition; update ) { // block statements }
It is a sign of unbelievably bad taste to put unrelated conditions into the loop. ( lol )
Assignment:
Using the code snippet from the lesson write a program MinMax_YI.java to prompt the user for three numbers and displays them in order from smallest to largest. OOP is not required. However, write the test/driver program if you use OOD.
Assignment: Programming Project 3.2
Design and implement an application that reads an integer value representing a year input by the user. The purpose of the program is to determine if the year is a leap year (and therefore has 29 days in February) in the Gregorian calendar. A year is a leap year if it is divisible by 4 unless it is also divisible by 100 but not 400. For example, the year 2003 is not a leap year, but 2004 is. The year 1900 is not a leap year because it is divisible by 100, but the year 2000 is a leap year because even though it is divisible by 100, it is also divisible by 400. Produce an error message for any input value less than 1582 (the year the Gregorian calendar was adopted).
Reading and Writing Assignments:
Read pages 124 through 140, submit the short answers 3.1 through 3.5.
Short Answers:
3.1 What happens in the MinOfThree program if two or more of
the values are equal? If exactly two of the values are equal, does
it matter whether the equal values are lower or higher than the
third?
3.2 What is wrong with the following code fragment? Rewrite it so
that it produces correct output.
if (total == MAX) if (total < sum) System.out.println ("total == MAX and is < sum."); else System.out.println ("total is not equal to MAX");
3.3 What is wrong with the following code fragment? Will this code
compile if it is part of a valid program? Explain.
if (length == MIN_LENGTH) System.out.println ("The length is minimal.");
3.4 What output is produced by the following code fragment?
int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear");
3.5 What output is produced by the following code fragment?
int limit = 100, num1 = 15, num2 = 40; if (limit <= limit) { if (num1 == num2) System.out.println ("lemon"); System.out.println ("lime"); } System.out.println ("grape");