Title: Chapter 2: Using Objects
1Chapter 3
2Program Statements
- We will now examine some other program statements
- Chapter 3 focuses on
- the flow of control through a method
- decision-making statements
- operators for making complex decisions
- repetition statements
- more drawing techniques
3Flow of Control
- Unless indicated otherwise, the order of
statement execution through a method is linear
one after the other in the order they are written - Some programming statements modify that order,
allowing us to - decide whether or not to execute a particular
statement, or - perform a statement over and over repetitively
- The order of statement execution is called the
flow of control
4Conditional Statements
- A conditional statement lets us choose which
statement will be executed next - Conditional statements give us the power to make
basic decisions - Java's conditional statements are the if
statement, the if-else statement, and the switch
statement
5The if Statement
- The if statement has the following syntax
if ( condition ) statement
6The if Statement
- An example of an if statement
if (sum gt MAX) delta sum -
MAX System.out.println ("The sum is " sum)
First, the condition is evaluated. The value of
sum is either greater than the value of MAX, or
it is not.
If the condition is true, the assignment
statement is executed. If it is not, the
assignment statement is skipped.
Either way, the call to println is executed next.
7Example if Statement
import cs1.Keyboard public class Age
//------------------------------------------------
- // Reads the user's age and prints comments
accordingly. //--------------------------------
--------------------------------- public
static void main (String args) final int
MINOR 21 System.out.print ("Enter your
age ") int age Keyboard.readInt()
System.out.println ("You entered "
age) if (age lt MINOR)
System.out.println ("Youth is a wonderful thing.
Enjoy.") System.out.println ("Age is a
state of mind.")
8Logic of an if statement
9Boolean Expressions
- A condition often uses one of Java's equality
operators or relational operators, which all
return boolean results - equal to
- ! not equal to
- lt less than
- gt greater than
- lt less than or equal to
- gt greater than or equal to
- Note the difference between the equality operator
() and the assignment operator ()
10The if-else Statement
- An else clause can be added to an if statement to
make it an if-else statement
if ( condition ) statement1 else
statement2
- If the condition is true, statement1 is executed
if the condition is false, statement2 is executed
- One or the other will be executed, but not both
11Example if-else
// Demonstrates the use of an if-else
statement. //
import
java.text.NumberFormat import cs1.Keyboard publ
ic class Wages //----------------------------
------------------------------------- //
Reads the number of hours worked and calculates
wages. //--------------------------------------
--------------------------- public static void
main (String args) final double RATE
8.25 // regular pay rate final int
STANDARD 40 // standard hours in a work
week double pay 0.0
12If-else
System.out.print ("Enter the number of
hours worked ") int hours
Keyboard.readInt() System.out.println
() // Pay overtime at "time and a half"
if (hours gt STANDARD) pay
STANDARD RATE (hours-STANDARD) (RATE
1.5) else pay hours
RATE NumberFormat fmt
NumberFormat.getCurrencyInstance()
System.out.println ("Gross earnings "
fmt.format(pay))
13Logic of an if-else statement
14Block Statements
- Several statements can be grouped together into a
block statement - A block is delimited by braces ( )
- A block statement can be used wherever a
statement is called for in the Java syntax - For example, in an if-else statement, the if
portion, or the else portion, or both, could be
block statements
15Example Block Statement
import cs1.Keyboard public class Guessing
//------------------------------------------------
----------------- // Plays a simple guessing
game with the user. //-------------------------
----------------------------------------
public static void main (String args)
final int MAX 10 int answer, guess
answer (int) (Math.random() MAX) 1
System.out.print ("I'm thinking of a number
between 1 and " MAX
". Guess what it is ") guess
Keyboard.readInt() if (guess answer)
System.out.println ("You got it! Good
guessing!") else
System.out.println ("That is not correct,
sorry.") System.out.println ("The
number was " answer)
16Nested if Statements
- The statement executed as a result of an if
statement or else clause could be another if
statement -
- These are called nested if statements
- An else clause is matched to the last unmatched
if (no matter what the indentation implies)
17Example nested if
// Demonstrates the use of nested if
statements. //
import
cs1.Keyboard public class MinOfThree
//------------------------------------------------
----------------- // Reads three integers
from the user and determines the smallest //
value. //--------------------------------------
--------------------------- public static void
main (String args) int num1, num2,
num3, min 0 System.out.println ("Enter
three integers ") num1
Keyboard.readInt() num2
Keyboard.readInt() num3
Keyboard.readInt()
18Nested if
if (num1 lt num2) if (num1 lt num3)
min num1 else
min num3 else if (num2 lt
num3) min num2
else min num3
System.out.println ("Minimum value " min)
19Repetition Statements
- Repetition statements allow us to execute a
statement multiple times repetitively - They are often simply referred to as loops
- Like conditional statements, they are controlled
by boolean expressions - Java has three kinds of repetition statements
the while loop, the do loop, and the for loop - The programmer must choose the right kind of loop
for the situation
20The while Statement
- The while statement has the following syntax
while ( condition ) statement
The statement is executed repetitively until the
condition becomes false.
21Logic of a while loop
22The while Statement
- Note that if the condition of a while statement
is false initially, the statement is never
executed - Therefore, the body of a while loop will execute
zero or more times
23Example while loop
// Demonstrates the use of a while
loop. //
public class Counter
//------------------------------------------------
----------------- // Prints integer values
from 1 to a specific limit. //-----------------
------------------------------------------------
public static void main (String args)
final int LIMIT 5 int count 1
while (count lt LIMIT)
System.out.println (count) count
count 1 System.out.println
("Done")
24Example while loop with sentinel
// Demonstrates the use of a while loop, a
sentinel value, and a // running
sum. //
import
java.text.DecimalFormat import
cs1.Keyboard public class Average
//------------------------------------------------
----------------- // Computes the average of
a set of values entered by the user. // The
running sum is printed as the numbers are
entered. //------------------------------------
----------------------------- public static
void main (String args) int sum 0,
value, count 0 double average
System.out.print ("Enter an integer (0 to quit)
") value Keyboard.readInt()
25Example cont
while (value ! 0) // sentinel value of 0 to
terminate loop count
sum value System.out.println
("The sum so far is " sum)
System.out.print ("Enter an integer (0 to quit)
") value Keyboard.readInt()
System.out.println ()
System.out.println ("Number of values entered "
count) average (double)sum / count
DecimalFormat fmt new DecimalFormat
("0.") System.out.println ("The average
is " fmt.format(average))
26Example loop for Input Validation
// Demonstrates the use of a while loop for
input validation. //
import
java.text.NumberFormat import cs1.Keyboard publi
c class WinPercentage //---------------------
--------------------------------------------
// Computes the percentage of games won by a
team. //---------------------------------------
-------------------------- public static void
main (String args) final int
NUM_GAMES 12 int won double
ratio
27Example cont.
System.out.print ("Enter the number of games won
(0 to " NUM_GAMES ")
") won Keyboard.readInt() while
(won lt 0 won gt NUM_GAMES)
System.out.print ("Invalid input. Please reenter
") won Keyboard.readInt()
ratio (double)won / NUM_GAMES
NumberFormat fmt NumberFormat.getPercentInstance
() System.out.println ()
System.out.println ("Winning percentage "
fmt.format(ratio))
28Infinite Loops
- The body of a while loop must eventually make the
condition false - If not, it is an infinite loop, which will
execute until the user interrupts the program - This is a common type of logical error
- You should always double check to ensure that
your loops will terminate normally
29Example Infinite Loops
// Demonstrates an infinite loop.
//
public class Forever
//------------------------------------------------
----------------- // Prints ever decreasing
integers in an INFINITE LOOP!
//------------------------------------------------
----------------- public static void main
(String args) int count 1
while (count lt 25)
System.out.println (count) count
count - 1 System.out.println
("Done") // this statement is never reached
30Logical Operators
- Boolean expressions can also use the following
logical operators - ! Logical NOT
- Logical AND
- Logical OR
- They all take boolean operands and produce
boolean results - Logical NOT is a unary operator (it has one
operand), but logical AND and logical OR are
binary operators (they each have two operands)
31Logical NOT
- The logical NOT operation is also called logical
negation or logical complement - If some boolean condition a is true, then !a is
false if a is false, then !a is true - Logical expressions can be shown using truth
tables
32Logical AND and Logical OR
- The logical and expression
- a b
- is true if both a and b are true, and false
otherwise - The logical or expression
- a b
- is true if a or b or both are true, and false
otherwise
33Truth Tables
- A truth table shows the possible true/false
combinations of the terms - Since and each have two operands, there are
four possible combinations of true and false
34Logical Operators
- Conditions in selection statements and loops can
use logical operators to form complex expressions
if (total lt MAX !found) System.out.println
("Processing")
- Logical operators have precedence relationships
between themselves and other operators
35Truth Tables
- Specific expressions can be evaluated using truth
tables