05Iteration - CSE 114 Computer Science I Iteration Cape Breton Nova Scotia What is Iteration Repeating a set of instructions a specified number of

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

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Iteration Cape Breton, Nova Scotia
Background image of page 1

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

View Full DocumentRight Arrow Icon
What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps? Imagine 3 instructions A, B, & C Instruction C can be jump A, meaning go back to A Iteration might result in: Execute A Execute B Execute C Execute A
Background image of page 2
Why use iteration? To make our code more practical and efficient To make our code more flexible and dynamic
Background image of page 3

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

View Full DocumentRight Arrow Icon
For Example How would we write code to print N!, where N is a number entered by the user. Without iteration (or recursion) this would be impractical!
Background image of page 4
Is this practical? Without iteration or recursion: System.out.print("Enter N: "); int N = Keyboard.readInt(); int factorial = 1; if ((N == 1) || (N == 0)) factorial = 1; else if (N == 2) factorial = 2 * 1; else if (N == 3) factorial = 3 * 2 * 1; else if (N == 4) factorial = 4 * 3 * 2 * 1; else if (N == 5) factorial = 5 * 4 * 3 * 2 * 1; System.out.println(factorial); Inefficient coding!
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using Iteration and Methods import java.util.Scanner; public class FactorialPrinter { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter N: "); int N = input.readInt(); int factorial = calculateFactorial(N); System.out.println(factorial); } public static int calculateFactorial(int n) { int count = n; int fact = 1; while (count > 0) fact *= count--; return fact; } Helper method return type return value Method argument type and name
Background image of page 6
Can Anyone guess what 1000! is? 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799886 1019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950595099527612087497 5462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323 9584630755574091142624174743493475534286465766116677973966688202912073791438537195882498081268678383745597317 4613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760967587134831202 5478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810 2130927612448963599287051149649754199093422215668325720808213331861168115536158365469840467089756029009505376 1647584772842188967964624494516076535340819890138544248798495995331910172335555660213945039973628075013783761 5307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892 1879602238389714760885062768629671466746975629112340824392081601537808898939645182632436716167621791689097799 1190375403127462228998800519544441428201218736174599264295658174662830295557029902432415318161721046583203678 6906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958 6526822728070757813918581788896522081643483448259932660433676601769996128318607883861502794659551311565520360 9398818061213855860030143569452722420634463179746059468257310379008402443243846565724501440282188525247093519 0620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814 9001407673104466402598994902222217659043399018860185665264850617997023561938970178600408118897299183110211712 2984590164192106888438712185564612496079872290851929681937238864261483965738229112312502418664935314397013742 8531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602 7898643211390835062170950025973898635542771967428222487575867657523442202075736305694988250879689281627538488 6339690995982628095612145099487170124451646126037902930912088908694202851064018215439945715680594187274899809 4254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105 9339838357779394109700277534720000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000 Impossible to calculate using Java’s primitive data types, instead we would have to use the BigInteger class double type can estimate up until 170! (7.257415615308004 E306)
Background image of page 7

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

View Full DocumentRight Arrow Icon
All programs need iteration Any exceptions? one and done programs Why do they all need iteration? because the start up, then wait for the user to act Most programs work as follows: 1. Initialize variables 2. Present options to user 3. Wait for user input 4. Respond to user input 5. Go back to step 2
Background image of page 8
GUIs and Iteration Think of a GUI. How does it work? 1. Draw the GUI 2. Check for input from user 3. Respond to user input, if any 4. Go back to Step 1
Background image of page 9

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

View Full DocumentRight Arrow Icon
Java and iteration We have 3 types of statements while loop do … while loop for loop All 3 can be used to do similar things Which one should you use? a matter of individual preference/convenience
Background image of page 10
The while Statement Syntax Diagram: while ‘(’ boolean Expr ‘)’ Statement Statement ‘{’ ‘}’ boolean Expr Statement(s) false true Loop Condition Loop Body Flowchart:
Background image of page 11

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

View Full DocumentRight Arrow Icon
Bracketing is again important Use either: while (boolean expression) { statement; statement; } while (boolean expression) statement; Or: Be careful NOT to put a semicolon, ‘;’, here
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2010 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 42

05Iteration - CSE 114 Computer Science I Iteration Cape Breton Nova Scotia What is Iteration Repeating a set of instructions a specified number of

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

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