Docsity

Control Flow: Sequence, Conditionals, and Loops in Programming, Slides of Computer Programming

Partial preview of the text

Download Control Flow: Sequence, Conditionals, and Loops in Programming and more Slides Computer Programming in PDF only on Docsity!

1.3 Conditionals and Loops

!

2

A Foundation for Programming

objects functions and modules graphics, sound, and image I/O arrays conditionals and loops Math text I/O primitive data types assignment statements last lecture: equivalent to a calculator any program you might want to write

4

Control Flow

Control flow.

! Sequence of statements that are actually executed in a program.

! Conditionals and loops: enable us to choreograph control flow.

statement 2 statement 1 statement 4 statement 3 boolean 2 true false statement 2 boolean 1 statement 3 false statement 1 true straight-line control flow control flow with conditionals and loops

Conditionals

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"); elseMath.random() < 0.5) System.out.println("Tails"); } } % java Flip Heads % java Flip Heads % java Flip Tails % java Flip Heads

10

The While Loop

11

While Loop

The while loop. A common repetition structure.

! Evaluate a boolean expression.

! If true, execute some statements.

! Repeat.

while ( boolean expression ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 loop body loop continuation condition

13

Powers of Two

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; } } } % java PowersOfTwo 3 0 1 1 2 2 4 3 8 % java PowersOfTwo 6 0 1 1 2 2 4 3 8 4 16 5 32 6 64 print i and ith power of two

14

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;

Goal. Implement Math.sqrt().

Newton-Raphson method to compute the square root of c:

! Initialize t 0 = c.

! Repeat until ti = c / ti, up to desired precision:

set ti+1 to be the average of ti and c / ti.

17 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); } } % java Sqrt 2.

relative error tolerance 15 decimal digits of accuracy in 5 iterations

While Loops: Square Root

18

Square root method explained.

! Goal: find root of any function f(x).

! Start with estimate t 0.

! Draw line tangent to curve at x= ti.

! Set ti+1 to be x-coordinate where line hits x-axis.

! Repeat until desired precision.

Technical conditions. f(x) must be smooth; t 0 must be good estimate.

Newton-Raphson Method

f(x) = x^2 - c to compute "c

20

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.

for ( init ; boolean expression ; increment ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 init increment body loop continuation condition

21

Anatomy of a For Loop

Q. What does it print?

A.