03_loops - Control Flow 1.3 Conditionals and Loops Control...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1.3 Conditionals and Loops Copyright 2004, FoxTrot by Bil Amend www.ucomics.com/foxtrot/2003/10/03 2 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 4 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 ; } can be any sequence of statements statement T true false boolean expression statement F
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 5 If Statement The if statement. A common branching structure. Check boolean condition. If true , execute some statements. If false , execute other statements. 6 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 Math.random() < 0.5) System . out . println ( "Tails" ); } } 7 If Statement Examples 8 The While Loop
Background image of page 2
3 9 While Loop The while loop. A common repetition structure. Check a boolean expression. Execute a sequence of statements. Repeat. while ( boolean expression ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 loop body loop continuation condition 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 ; } % java Powers 1 2 4 8 16 32 64 0 1 i v 1 2 2 4 3 8 true i <= N true true true 4 16 5 32 6 64 7 128 true true true false n = 6 11 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 ( v ); i = i + 1 ; v = 2 * v ; } } } % java PowersOfTwo 4 1 2 4 8 % java PowersOfTwo 6 1 2 4 8 16 32 64 print ith power of two 12 A Wonderful Square Root Copyright 2004, Sidney Harris, http://www.sciencecartoonsplus.com % java Sqrt 60481729 7777.0
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 13 While Loops: Square Root Q. How might we implement Math.sqrt() ? A. To compute the square root of c: Initialize t 0 = c. Repeat until t i = c / t i , up to desired precision: set t i+1 to be the average of t i and c / t i . t 0 = 2.0 t 1 = 1 2 ( t 0 + 2 t 0 ) = 1.5 t 2 = 1 2 ( t 1 + 2 t 1 ) = 1.416666666666665 t 3 = 1 2 ( t 2 + 2 t 2 ) = 1.4142156862745097 t 4 = 1 2 ( t 3 + 2 t 3 ) = 1.4142135623746899 t 5 = 1 2 ( t 4 + 2 t 4 ) = 1.414213562373095 computing the square root of 2 14 public class Sqrt { public static void main ( String [] args ) { double
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

03_loops - Control Flow 1.3 Conditionals and Loops Control...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online