datalogi a 5: 6/10. while loops executes a block of code repeatedly a condition controls how often...

23
Datalogi A 5: 6/10

Post on 21-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

Datalogi A 5: 6/10

Page 2: 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

while Loops • Executes a block of code repeatedly

• A condition controls how often the loop is executedwhile (condition)   statement;

• Most commonly, the statement is a block statement (set of statements delimited by { })

Page 3: 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

Calculating the Growth of an Investment

Invest $10,000, 5% interest, compounded annually

Year Balance0 $10,0001 $10,5002 $11,0253 $11,576.254 $12,155.065 $12,762.82

Page 4: 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

While loopWhen has the bank account reached a particular

balance?

while (balance < targetBalance)

{year++;double interest = balance * rate / 100

  balance = balance + interest;

}

Page 5: 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

Flow of control

Page 6: 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

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

Page 7: 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

Javaint 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 <=?

Page 8: 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

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);

Page 9: 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

do loop

Page 10: 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

Spaghetti code – not in java years=1;

goto a:

b:

years++;

a:

balance+=interest;

if(balance<targetBalance)

goto b;

Page 11: 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

for Loops • for (initialization; condition; update)

   statement

Example:

for (int i = 1; i <= n; i++){double interest = balance * rate / 100;balance = balance + interest;

}

Page 12: 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

for loops• for (initialization; condition; update)

   statement

• Equivalent toinitialization;while (condition) { statement; update; }

• Other examples:

for (years = n; years > 0; years--) . . .

for (x = -10; x <= 10; x = x + 0.5) . . .

Page 13: 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

for loops

Page 14: 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

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

Page 15: 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

Nested loops• Create triangle pattern[][][][][][][][][][]

• Loop through rowsfor (int i = 1; i <= n; i++){   // make triangle row}

• Make triangle row is another loopfor (int j = 1; j <= i; j++)   r = r + "[]";r = r + "\n";

• Put loops together → Nested loops

Page 16: 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

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);

. . .

}

Page 17: 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

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

} }

Page 18: 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

JCreatorIDE: Integrated Development Editor

Features:

• Compile and run from editor

• Manage multiple files

• Can look up api documentation

• Selective hiding of parts of the code

Page 19: 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

WorkspaceConcepts:

• 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.

Page 20: 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

Workspace

Page 21: 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

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 toSystem.out.println(”Hello world”);

Page 22: 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

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

Page 23: 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

Editor featuresWord 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