Académique Documents
Professionnel Documents
Culture Documents
Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 20022010 2/6/11 12:44 PM!
A Foundation for Programming
objects
arrays
2
A Foundation for Programming
objects
arrays
to infinity
conditionals and loops and beyond!
3
Control Flow
Control flow.
!Sequence of statements that are actually executed in a program.
!Conditionals and loops: enable us to choreograph control flow.
boolean 1
statement 1
true false
statement 2
statement 1
true
boolean 2 statement 2
statement 3
false
statement 4
statement 3
4
Conditionals
If Statement
6
If Statement
7
If Statement
% java Flip
Heads
% java Flip
Tails
% java Flip
Heads
8
If Statement Examples
9
The While Loop
10
While Loop
Repeat.
!
statement 2
while (boolean expression) {
statement 1;
loop body
statement 2; true
} boolean expression statement 1
false
11
While Loop: Powers of Two
0 1 true 0 1
1 2
int i = 0; 1 2 true 2 4
int v = 1; 3 8
2 4 true
while (i <= N) { 4 16
System.out.println(i + " " + v); 3 8 true 5 32
i = i + 1; 6 64
v = 2 * v; 4 16 true
} 5 32 true
6 64 true
7 128 false
N = 6
12
Powers of Two
13
While Loop Challenge
int i = 0;
int v = 1;
while (i <= N)
System.out.println(i + " " + v);
i = i + 1;
v = 2 * v;
14
While Loops: Square Root
t0 = 2.0
t1 = 1 (t0 + 2 ) = 1.5
2 t0
t2 = 1 (t1 + 2 ) = 1.416666666666665
2 t1
t3 = 1 (t2 + 2 ) = 1.4142156862745097
2 t2
t4 = 1 (t3 + 2 ) = 1.4142135623746899
2 t3
t5 = 1 (t4 + 2 ) = 1.414213562373095
2 t4
! 16
While Loops: Square Root
18
The For Loop
19
For Loops
Repeat.
!
false
20
Anatomy of a For Loop
21
For Loops: Subdivisions of a Ruler
i ruler
public class RulerN {
public static void main(String[] args) { " "
int N = Integer.parseInt(args[0]);
1 " 1 "
String ruler = " ";
for (int i = 1; i <= N; i++) { 2 " 1 2 1 "
ruler = ruler + i + ruler;
3 " 1 2 1 3 1 2 1 "
}
System.out.println(ruler);
}
}
22
For Loops: Subdivisions of a Ruler
% java RulerN 1
1
% java RulerN 2
1 2 1
% java RulerN 3
1 2 1 3 1 2 1
% java RulerN 4
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
% java RulerN 5
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
23
Loop Examples
24
Nesting
25
Nested If Statements
Income Rate
0 - 47,450 22%
311,950 - 35%
double rate;
if (income < 47450) rate = 0.22;
else if (income < 114650) rate = 0.25;
else if (income < 174700) rate = 0.28;
else if (income < 311950) rate = 0.33;
else if (income < 311950) rate = 0.35;
27
Nested If Statements
28
Nested If Statements
is shorthand for
if (income < 47450) rate = 0.22;
else {
if (income < 114650) rate = 0.25;
else {
if (income < 174700) rate = 0.28;
else {
if (income < 311950) rate = 0.33;
else rate = 0.35;
}
}
}
29
Nested If Statement Challenge
Income Rate
0 - 47,450 22%
311,950 - 35%
30
Monte Carlo Simulation
31
Gambler's Ruin
Gambler's ruin. Gambler starts with $stake and places $1 fair bets
until going broke or reaching $goal.
!What are the chances of winning?
!How many bets will it take?
32
Gambler's Ruin
33
Digression: Simulation and Analysis
stake goal T
34
Control Flow Summary
Control flow.
!Sequence of statements that are actually executed in a program.
!Conditionals and loops: enable us to choreograph the control flow.
35