Sie sind auf Seite 1von 19

Looping

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

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

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

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

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

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)

For Loop
Use when you know a specific count

openTheContainerOfEggs(); for (i=1; i<=12; i++) { cookTheEgg(); eatTheEgg(); } goBuyMoreEggs();

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.

Variable scope in Java


Braces (a block of code) define a variables 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

Variable Scope
//visible: none, Not visible: a, b, c, d int a = 10; //visible: a, not visible: b,c,d for (int b=0; b<10; b++) { //visible: a, b, not visible: c, d while (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

Breaking out of loops break;


break; is applicable to all loops Causes 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 dont need to continue looping Usually used more in iterators (later)

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

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;

More questions
How 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);

More questions
How 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);

Yet More questions


How many times will this loops execute?
int x=1; while (x > 0) { System.out.println(x); }
int x=1; while (x > 0); { System.out.println(x); x--; }

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 JVM int x=1, y=2; while (x < y); { x++; }

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 example

Wrong: 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)

Das könnte Ihnen auch gefallen