# L5 - CSE 114 Computer Science I Iteration Cape Breton Nova...

This preview shows pages 1–8. Sign up to view the full content.

CSE 114 – Computer Science I Iteration Cape Breton, Nova Scotia

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

View Full Document
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? Instruction C can be jump A, meaning go back to A Iteration might result in: Execute A Execute B Execute C Execute A
Why use iteration? To make our code more practical and efficient To make our code more flexible and dynamic

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

View Full Document
For Example How would we write code to print N! (factorial), where N is a number entered by the user? Without iteration (or recursion) this would be impractical! We do not know N, when we are about to write the program.
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!

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

View Full Document
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
Can Anyone guess what 1000! is? 4023872600770937735437024339230039857193748642107146325437999104299385123986290205920442084869694048004799886 1019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950595099527612087497 5462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323 9584630755574091142624174743493475534286465766116677973966688202912073791438537195882498081268678383745597317 4613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760967587134831202 5478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810 2130927612448963599287051149649754199093422215668325720808213331861168115536158365469840467089756029009505376 1647584772842188967964624494516076535340819890138544248798495995331910172335555660213945039973628075013783761 5307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892 1879602238389714760885062768629671466746975629112340824392081601537808898939645182632436716167621791689097799 1190375403127462228998800519544441428201218736174599264295658174662830295557029902432415318161721046583203678 6906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958 6526822728070757813918581788896522081643483448259932660433676601769996128318607883861502794659551311565520360

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/29/2011 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

### Page1 / 42

L5 - CSE 114 Computer Science I Iteration Cape Breton Nova...

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

View Full Document
Ask a homework question - tutors are online