Introduction to Computer Science I.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Types of Recursive Methods
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Lecture 2: Static Methods, if statements, homework uploader.
1.3 Conditionals and Loops Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
CSC 204 Programming I Loop I The while statement.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
Repetition Statements while and do while loops
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1.3 Conditionals and Loops Copyright 2004, FoxTrot by Bill Amend
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CompSci 100e2.1 1 N-Body Simulation l Applications to astrophysics.  Orbits of solar system bodies.  Stellar dynamics at the galactic center.  Stellar.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
2.4 A Case Study: Percolation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 6: While Loops and the Math Class
Chapter 4 Repetition Statements (loops)
Introduction to programming in java
Chapter 6 More Conditionals and Loops
JavaScript: Control Statements I
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
User input We’ve seen how to use the standard output buffer
CiS 260: App Dev I Chapter 4: Control Structures II.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Outline Altering flow of control Boolean expressions
LRobot Game.
Lecture Notes – Week 3 Lecture-2
Repetition Control Structure
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.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Problem 1 Given n, calculate 2n
CSS 161: Fundamentals of Computing
Presentation transcript:

Introduction to Computer Science I

1.3 Conditionals and Loops Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 2/6/11 12:44 PM

Construct cool programs graphics, sound, and image I/O Arrays Recursion objects functions and modules conditionals and loops Math & text I/O primitive data types & assignment statements last lecture: equivalent to a calculator

Control Flow Control flow. Sequence of statements that are actually executed in a program. Conditionals and loops: enable us to create control flow. 4

Control Flow Control flow. Sequence of statements that are actually executed in a program. Conditionals and loops: enable us to create control flow. boolean 1 statement 1 statement 2 statement 3 statement 4 true false statement 1 true boolean 2 statement 2 false statement 3 straight-line control flow control flow with conditionals and loops 5

Conditionals

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

If Statement The if statement. A common branching structure. Evaluate a boolean expression. If true, execute some statements. If false, execute other statements. 8

If Statement 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"); } Else { System.out.println("Tails"); % java Flip Heads Tails 9

Nesting

Nested If Statements Ex. Pay a certain tax rate depending on income level. Income Rate 0 - 47,450 22% 47,450 – 114,650 25% 114,650 – 174,700 28% 174,700 – 311,950 33% 311,950 - 35% 5 mutually exclusive alternatives double rate; if (income < 47450) rate = 0.22; else 114650) 0.25; 174700) 0.28; 311950) 0.33; 0.35; graduated income tax calculation

Nested If Statements Use nested if statements to handle multiple alternatives. 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; }

Nested If Statements Need all those braces? Not always. (income < 47450) rate = 0.22; else 114650) 0.25; 174700) 0.28; 311950) 0.33; 0.35; 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; } but be careful when nesting if-else statements. [See Q+A on p. 75.]

Nested If Statement Challenge Q. What's wrong with the following for income tax calculation? Income Rate 0 - 47,450 22% 47,450 – 114,650 25% 114,650 – 174,700 28% 174,700 – 311,950 33% 311,950 - 35% double rate = 0.35; if (income < 47450) rate = 0.22; 114650) 0.25; 174700) 0.28; 311950) 0.33; wrong graduated income tax calculation 30

Switch statements

Loops While loops

While Loop loop body The while loop. A common repetition structure. Evaluate a boolean expression. If true, execute some statements. Repeat. loop continuation condition statement 2 while (boolean expression) { statement 1; statement 2; loop body true statement 1 } boolean expression false 17

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v 1 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 6 64 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 6 64 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 6 64 int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 128 N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 6 64 1 2 true int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); 2 4 true 3 8 true i = + 1; } v 2 * v; 4 16 true 5 32 true 6 64 true 7 128 false N = 6

Powers of Two: Trace Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 1 true 0 1 1 2 2 4 3 8 4 16 5 32 6 64 1 2 true int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); 2 4 true 3 8 true i = + 1; } v 2 * v; 4 16 true 5 32 true 6 64 true 7 128 false N = 6

While Loop: Powers of Two Ex. Print powers of 2 that are  2N. Increment i from 0 to N. Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 1 true int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); 1 2 true 2 4 true 3 8 true i = + 1; } v 2 * v; 4 16 true 5 32 true 6 64 true 7 128 false N = 6 50

public static void main(String[] args) { Powers of Two % java PowersOfTwo 3 0 1 1 2 2 4 3 8 % java PowersOfTwo 6 4 16 5 32 6 64 public class PowersOfTwo { public static void main(String[] args) { // last power of two to print int N = Integer.parseInt(args[0]); int i = 0; // loop control counter int v = 1; // current power of two while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } print i and ith power of two } 51

While Loop Challenge Q. Anything wrong with the following code for printing powers of 2? int i = 0; int v = 1; while (i <= N) System.out.println(i + " " + v); i = i + 1; v = 2 * v; int i = 0; int v = 1; while (i <= N) { System.out.println(i + " " + v); i = + 1; } v 2 * v; 52

While Loops: Square Root Goal. Implement Math.sqrt(). % java Sqrt 2.0 1.414213562373095 Newton-Raphson method to compute the square root of c: Initialize t0 = c. Repeat until ti = c / ti, up to desired precision: set ti+1 to be the average of ti and c / ti. 15 decimal digits of accuracy in 5 iterations 16

While Loops: Square Root Goal. Implement Math.sqrt(). % java Sqrt 2.0 1.414213562373095 Newton-Raphson method to compute the square root of c: Initialize t0 = c. Repeat until ti = c / ti, up to desired precision: set ti+1 to be the average of ti and c / ti. 15 decimal digits of accuracy in 5 iterations public class Sqrt { public static void main(String[] args) { double epsilon = 1e-15; double c = Double.parseDouble(args[0]); double t = c; while (Math.abs(t - c/t) > t*epsilon) { t = (c/t + t) / 2.0; } System.out.println(t); relative error tolerance } 17

For Loops

For Loops The for loop. Another common repetition structure. Execute initialization statement. Evaluate a boolean expression. If true, execute some statements. And then the increment statement. Repeat. loop continuation condition init increment for (init; boolean expression; increment) { statement 1; statement 2; statement 2 } body true statement 1 boolean expression false 20

Anatomy of a For Loop Q. What does it print? A.

The For Loop Copyright 2004, FoxTrot by Bill Amend www.ucomics.com/foxtrot/2003/10/03 19

Loop Examples

Monte Carlo Simulation

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? One approach. Monte Carlo simulation. Flip digital coins and see what happens. Repeat and compute statistics.

Gambler' s Ruin public class Gambler { public static void main(String[] args) { int stake int goal int T int wins = Integer.parseInt(args[0]); = Integer.parseInt(args[1]); = Integer.parseInt(args[2]); = 0; // repeat experiment T times for (int t = 0; t < T; t++) { } // do one gambler's ruin experiment int cash = stake; while (cash > 0 && cash < goal) { } if (cash == goal) wins++; // flip coin and update if (Math.random() < 0.5) cash++; else cash--; System.out.println(wins + " wins of " + T); }

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

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. Control Flow Description Examples straight-line programs all statements are executed in the order given certain statements are executed depending on the values of certain variables certain statements are executed repeatedly until certain conditions are met if if-else conditionals loops while for do-while