19
Looping

Java: Week 3

Embed Size (px)

Citation preview

Page 1: Java: Week 3

Looping

Page 2: Java: Week 3

Flowcharting and Psuedocoding

During design, it is usually helpful to either draw a flowchart or psuedocode the procedures you are about to design

This makes coding go faster and helps eliminate errors in flow

Page 3: Java: Week 3

Flowcharting – If statements

If statement without an else will trigger the no condition either way

If-then-else will only trigger the no as the “else”

Page 4: Java: Week 3

Loops

There are three types of loops While Loop Do Loop For Loop

Page 5: Java: Week 3

While Loop

A while loop is sort of like an if statement in that it will only execute if the condition is true

It will continue to loop until the condition is false

while (badDebt()) {

getCallsFromCreditors();

payoffSomeDebt();

}

buySomething();

Page 6: Java: Week 3

Do Loop

A do loop does something, then loops to do it again while a condition is true

Loop will be executed at least once!

goToBar();

do {

haveADrink();

} while (havingFun());

goHome();

Page 7: Java: Week 3

For Loop Used to loop through a block of code for a

specific number of times. Can shorten the code of a while loop:

int i=1;

while (i <= 10){

doSomething();

i++;

}

for (int i=1; i<=10; i++){

doSomething();

}

Java syntax:for (initilization; boolean comparison; iteration)

Page 8: Java: Week 3

For Loop

Use when you know a specific count

openTheContainerOfEggs();

for (i=1; i<=12; i++) {

cookTheEgg();

eatTheEgg();

}

goBuyMoreEggs();

Page 9: Java: Week 3

Braces

The same rules apply to loops that apply to if statements

Braces are not needed if there is a single line of code in the loop

However, braces can still be used either way.

Page 10: Java: Week 3

Variable scope in Java

Braces (a block of code) define a variable’s scope

A variable defined inside a child block is not visible in the parent block

A variable defined inside a parent block is visible in the child block

Page 11: Java: Week 3

Variable Scope

//visible: none, Not visible: a, b, c, dint a = 10;//visible: a, not visible: b,c,dfor (int b=0; b<10; b++) {

//visible: a, b, not visible: c, dwhile (b<5) {

int c = a * b;//visible: a, b, c, not visible: d

}while (b > 5) {

int d = b - a;//visible: a, b, d, not visible: c

}//visible: a, b, not visible: c, d

}//visible: a, not visible: b, c, d

Page 12: Java: Week 3

Breaking out of loops – break;

break; is applicable to all loopsCauses the execution to continue

immediately following the code block that contains the break; statement

Used when you find the item in the collection that you are looking for and don’t need to continue looping

Usually used more in iterators (later)

Page 13: Java: Week 3

Continuing Execution in a loop: continue;

Causes the execution to continue in the next iteration of the loop

Often can be avoided by coding an if statement

Page 14: Java: Week 3

Questions

If x is an int where x =1, what will x be after the following loop terminates:

while (x < 100)

x*=2;If x is an int where x =0, what will x be

after the following loop terminates:

for (int i=0; i<6; i++)

x += i;

Page 15: Java: Week 3

More questionsHow many times will these loops execute?int x=5;

while (x > 0) {

System.out.println(x);

x--;

}

int x=5;

do {

System.out.println(x);

x--;

} while (x > 0);

Page 16: Java: Week 3

More questionsHow many times will these loops execute?int x=0;

while (x < 0) {

System.out.println(x);

x++;

}

int x=0;

do {

System.out.println(x);

x++;

} while (x < 0);

Page 17: Java: Week 3

Yet More questionsHow many times will this loops execute?

int x=1;

while (x > 0); {

System.out.println(x);

x--;

}

int x=1;

while (x > 0) {

System.out.println(x);

}

Page 18: Java: Week 3

Watch out for infinite loops

The creation of infinite loops is the most common mistake in looping

Make sure that an end result will eventually be reached.

If not, you can crash the entire JVMint x=1, y=2;while (x < y);{ x++;

}

Page 19: Java: Week 3

5 common mistakes

Wrong: if (x > y && z)Wrong: if (x = y)Wrong: if (A || B && C || D)

Evaluate to see what happens with an exampleWrong: if (x > y);

Or, while (x>y);Wrong: Unmatched braces (usually caught

by compiler, unless the count matches, but they are in the wrong place)