Presentation is loading. Please wait.

Presentation is loading. Please wait.

Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.

Similar presentations


Presentation on theme: "Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most."— Presentation transcript:

1 Datalogi A 5: 6/10

2 while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most commonly, the statement is a block statement (set of statements delimited by { })

3 Calculating the Growth of an Investment Invest $10,000, 5% interest, compounded annually YearBalance 0$10,000 1$10,500 2$11,025 3$11,576.25 4$12,155.06 5$12,762.82

4 While loop When has the bank account reached a particular balance? while (balance < targetBalance) { year++; double interest = balance * rate / 100 balance = balance + interest; }

5 Flow of control

6 Common Error: Infinite Loops int years = 0; while (years < 20) { double interest = balance * rate / 100; balance = balance + interest; } int years = 20; while (years > 0) { years++; // Oops, should have been years-- double interest = balance * rate / 100; balance = balance + interest; } Loops run forever–must kill program

7 Java int years = 0; while (balance < 2 * initialBalance) { years++; double interest = balance * rate / 100; balance = balance + interest; } System.out.println("The investment reached the target after " + years + " years."); Should years start at 0 or 1? Should the test be < or <=?

8 do Loops Executes loop body at least once: do statement while (condition); Example: Validate input double value; do { System.out.print( "Please enter a positive number: "); value = in.nextDouble(); } while (value <= 0);

9 do loop

10 Spaghetti code – not in java years=1; goto a: b: years++; a: balance+=interest; if(balance<targetBalance) goto b;

11 for Loops for (initialization; condition; update) statement Example: for (int i = 1; i <= n; i++){ double interest = balance * rate / 100; balance = balance + interest; }

12 for loops for (initialization; condition; update) statement Equivalent to initialization; while (condition) { statement; update; } Other examples: for (years = n; years > 0; years--)... for (x = -10; x <= 10; x = x + 0.5)...

13 for loops

14 Common Errors: Semicolons A semicolon that shouldn't be there sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum);

15 Nested loops Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i <= n; i++) { // make triangle row } Make triangle row is another loop for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n"; Put loops together → Nested loops

16 Processing Sentinel Values Sentinel value: Can be used for indicating the end of a data set 0 or -1 make poor sentinels; better use Q System.out.print("Enter value, Q to quit: "); String input = in.next(); if (input.equalsIgnoreCase("Q")) We are done else { double x = Double.parseDouble(input);... }

17 Loop and a half Sometimes termination condition of a loop can only be evaluated in the middle of the loop Then, introduce a boolean variable to control the loop: boolean done = false; while (!done){ Print prompt String input = read input ; if ( end of input indicated ) done = true; else{ // Process input }

18 JCreator IDE: Integrated Development Editor Features: Compile and run from editor Manage multiple files Can look up api documentation Selective hiding of parts of the code

19 Workspace Concepts: Workspace: the editor window. A workspace may have several projects. A project: a java program. A project may consist of several java files. A file. A single java file. A file may contain several classes.

20 Workspace

21 Make a java program Create a new workspace Create a project – give it a name, specify where to save its files, and a type (Basic Java application). It generates a ”*.java” file and a ”*Frame.java” file (forget about the last one) Click on the first java file Replace the body of the main method to System.out.println(”Hello world”);

22 Compile and run Compile (called ”build”): F7 Run (”execute”): F5 If there were errors click on red cross in ”Build output” and the line is marked in the program. Click on ”System” in ”System.out….” Press ”Ctrl-F1” and you will get context-sensitive help

23 Editor features Word completion: ”ctrl-space” Match bracket, parentheses: ”ctrl-egu” Hide imports, comments, method body, class content. ’ctrl-f1’ on classes to get api-documentation ’ctrl-f1’ on method name to list methods


Download ppt "Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most."

Similar presentations


Ads by Google