Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data types, assignment statements, and math functions allow you to compute You write a sequence of statements that tell the computer what needs to be done.

Similar presentations


Presentation on theme: "Data types, assignment statements, and math functions allow you to compute You write a sequence of statements that tell the computer what needs to be done."— Presentation transcript:

1 Data types, assignment statements, and math functions allow you to compute
You write a sequence of statements that tell the computer what needs to be done. Issues: Some computations make sense only under certain conditions at occur when the program is running. The computations may need to be done repeatedly to solve a big problem. statement 1 statement 2 statement 3 statement 4

2 1.3 Conditionals and Loops
Copyright 2004, FoxTrot by Bill Amend

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 straight-line control flow statement 2 statement 1 statement 4 statement 3 straight-line programs: akin to calculator true false statement 1 boolean 2 true statement 2 false statement 3 control flow with conditionals and loops

4 Conditionals

5 If Statement The if statement. A common branching structure.
Check boolean condition. If true, execute some statements. If false, execute other statements. if (boolean expression) { statement T; } else { statement F; boolean expression can be any sequence of statements true false statement T statement F

6 If Statement The if statement. A common branching structure.
Check boolean condition. If true, execute some statements. If false, execute other statements. Notice no curly braces Again no curly braces

7 If Statement Example – Flipping a Coin
Ex. Take different action depending on value of variable. public class Flip { public static void main(String[] args) { if (Math.random() < 0.5) System.out.println("Heads"); elseMath.random() < 0.5) System.out.println("Tails"); }

8 The While Loop

9 While Loop The while loop. A common repetition structure.
Check a boolean expression. Execute a sequence of statements. Repeat. loop continuation condition straight-line program: amount of data processed proportional to number of lines of codes written, even with conditionals loops: brings you to infinite statement 2 while (boolean expression) { statement 1; statement 2; } loop body boolean expression true statement 1 false

10 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } Note: assignment is not mathematical equality. Curly braces delimit a block – unlike before, curly braces are needed since block has three statements. n = 6

11 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i % java Powers int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } n = 6

12 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v 1 % java Powers int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } n = 6

13 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } n = 6

14 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } n = 6

15 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 n = 6

16 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 n = 6

17 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true n = 6

18 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 2 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true n = 6

19 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 2 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true 2 n = 6

20 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 2 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true 2 4 n = 6

21 While Loops: Powers of Two
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= n 1 true % java Powers 1 2 int i = 0; int v = 1; while (i <= n) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true n = 6

22 While Loops: Powers of Two (Final State)
Ex. Print first n powers of 2. Increment i from 1 to n. Double v each time. i v i <= N 1 true % java Powers 1 2 4 8 16 32 64 int i = 0; int v = 1; while (i <= N) { System.out.println(v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true Note: assignment is not mathematical equality. 3 8 true 4 16 true 5 32 true 6 64 true 7 128 false n = 6

23 Anything wrong with the following code for printing powers of 2?
Question Anything wrong with the following code for printing powers of 2? int i = 0; int v = 1; while (i <= N) System.out.println(v); i = i + 1; v = 2 * v; YES. Need braces around statements in while loop. Indentation does not imply braces!Without braces, program enters an infinite loop printing 1s. Beginning programmer's first moment of panic: how to stop it?

24 Q. Anything wrong with the following code for printing powers of 2?
While Loop Question Q. Anything wrong with the following code for printing powers of 2? A. Need curly braces around statements in while loop; otherwise it enters an infinite loop, printing 1s. int i = 0; int v = 1; while (i <= N) System.out.println(v); i = i + 1; v = 2 * v; Indentation does not imply braces! Tools -> Reset Interactions in DrJava Ctrl-c from command line

25 The For Loop

26 For Loops The for loop. Another common repetition structure.
Execute initialization statement. Check boolean expression. Execute sequence of statements. Execute increment statement. Repeat. loop continuation condition init Compact notation for common looping scheme. increment for (init; boolean expression; increment) { statement 1; statement 2; } statement 2 body boolean expression true statement 1 false

27 Powers of Two Code using While Loop Code using For Loop int i = 0;
int v = 1; while (i <= N) { System.out.println(v); i = i + 1; v = 2 * v; } int v = 1; for (int i = 0; i <= N; i++) { System.out.println(v); v = 2 * v; } Code using While Loop Code using For Loop

28 Recap - Conditionals and Loops
Conditionals enable you to do one of 2n sequences of operations with n lines. Loops enable you to do an operation n times using only 2 lines of code. if (a0 > 0) System.out.print(0); if (a1 > 0) System.out.print(1); if (a2 > 0) System.out.print(2); if (a3 > 0) System.out.print(3); if (a4 > 0) System.out.print(4); if (a5 > 0) System.out.print(5); if (a6 > 0) System.out.print(6); if (a7 > 0) System.out.print(7); if (a8 > 0) System.out.print(8); if (a9 > 0) System.out.print(9); double sum = 0.0; for (int i = 1; i <= 1024; i++) sum = sum / i; computes 1/1 + 1/ /1024 Nth harmonic number is approximately ln(n) 210 = 1024 possible results, depending on input

29 Calculating Graduated Income Tax (Group Activity)
Pay a certain tax rate depending on income level. Income Rate ,450 22% 47,450 – 114,650 25% 114,650 – 174,700 28% IS THIS CODE CORRECT? WHAT IS WRONG WITH IT? HOW SHOULD WE REALLY CALCULATE IT? 174,700 – 311,950 33% 311,950 - 35% double rate = 0.35; if (income < ) rate = 0.22; if (income < ) rate = 0.25; if (income < ) rate = 0.28; if (income < ) rate = 0.33; IS THIS CODE CORRECT?

30 Nesting

31 Nested If Statements Ex. Pay a certain tax rate depending on income level. Income Rate ,450 22% 47,450 – 114,650 25% 5 mutually exclusive alternatives 114,650 – 174,700 28% nested conditionals: this is actually 4 different if-else statements, but no need for braces 174,700 – 311,950 33% 311,950 - 35% double rate; if (income < ) rate = 0.22; else if (income < ) rate = 0.25; else if (income < ) rate = 0.28; else if (income < ) rate = 0.33; else if (income < ) rate = 0.35; graduated income tax calculation

32 Nesting Conditionals and Loops
Nest conditionals within conditionals. Nest loops within loops. Nest conditionals within loops within loops.

33 Putting it All Together: Monte Carlo Simulation

34 Monte Carlo Simulation
Developed in the 1940s by physicists at Los Alamos National Lab For modeling phenomena with significant uncertainty in the inputs Uses random sampling to compute the results

35 Steps in a Monte Carlo Simulation
Define a domain of possible inputs. Generate inputs randomly from the domain. Perform a deterministic computation using the inputs. Aggregate the results of the individual computations into the final result.

36 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? Monte Carlo simulation. Flip digital coins and see what happens. Repeat and compute statistics. discrete analog of Brownian motion (applications to stock market – Black-Scholes, behavior of atomic particles predicted by quantum physics, dispersion of ink flowing in water) Simulations of this sort are widely used in economics, science and engineering

37 Gambler's Ruin public class Gambler {
public static void main(String[] args) { int stake = Integer.parseInt(args[0]); int goal = Integer.parseInt(args[1]); int trials = Integer.parseInt(args[2]); int wins = 0; System.out.println(wins + " wins of " + trials); } // repeat experiment N times for (int i = 0; i < trials; i++) { } // do one gambler's ruin experiment int t = stake; while (t > 0 && t < goal) { } if (t == goal) wins++; This program is worthy of careful study. Conditional within a while loop within a for loop. Inner loop uses && to continue as long as gambler is not broke and didn't reach goal // flip coin and update if (Math.random() < 0.5) t++; else t--;

38 Digression: Simulation and Analysis
Fact. Probability of winning = stake  goal. Fact. Expected number of bets = stake  desired gain. Ex. 20% chance of turning $500 into $2500, but expect to make one million $1 bets. Remark. Both facts can be proved mathematically; for more complex scenarios, computer simulation is often the best plan of attack. stake goal trials % java Gambler 191 wins of 1000 203 wins of 1000 % java Gambler 197 wins of 1000 after a substantial wait…. substantial wait = hours 500/2500 = 20% 500 * ( ) = 1 million


Download ppt "Data types, assignment statements, and math functions allow you to compute You write a sequence of statements that tell the computer what needs to be done."

Similar presentations


Ads by Google