Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.

Similar presentations


Presentation on theme: "Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a."— Presentation transcript:

1 Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a block until boolean expression becomes false – checks upfront before 1 st time do statement – repeats a block until boolean expression becomes false – checks at the and, after 1 st time for statement – contains initialization statement – contains end statement (e.g. increment) – repeats a block until boolean expression becomes false

2 while Statement syntax: – while( ) { //code } – the body can contain several statements the expression is evaluated if it is false then the code doesn't get executed while it is true, the code is executed and control goes back to evaluation of the expression once expression is false then control continues – code after the next statement after curly bracket is executed if there is any one statement in the body, you can omit {} – but we recommend to use the curly bracket in any case it's less errorprone

3 do Statement syntax: – do { //code } while ( ); – the body can contain several statements the code is executed then the expression is evaluated while it is true, control goes back to beginning of the code once it is false, control continues – code after the next statement after curly bracket is executed if there is any one statement in the body, you can omit {} – but we recommend to use the curly bracket in any case it's less errorprone

4 for Statement syntax: – for ( ; ; ) { //code } – the body can contain several statements the init statement is executed then the condition is evaluated while it is true, code and then end statement are executed and control goes back to evaluation of the expression once it is false, control continues – code after the next statement after curly bracket is executed if there is any one statement in the body, you can omit {} – but we recommend to use the curly bracket in any case it's less errorprone

5 for Statement most often used form: – for ( ; ; ) { //code } for (int i = 0; i < n; i++) {…} – goes through values 0, 1, 2, …, n-1 for (int i = 0; i < n; i += 2) {…} – goes through values 0, 2, 4, 8, …, n-1 for (int i = n - 1; i >= 0; i--) {…} – goes through values n-1, n-2, …, 2, 1, 0 e.g. suppose we have a linked list of nodes – starts with a head node – each node has a next field that points to the next node in the list – for (Node node = head; node != null; node=node.next) {…}

6 Repetition Statements: Scope, etc. the scope of variables is the entire repetition statement variable declared within parentheses () is valid in block {} – e.g. for (int i = 0; i < n; i++) { System.out.println (i + " sq = " + i * i); } note that condition within all loops tests for true – repeat while condition is true – stop when condition is false beware of endless loops – make sure the condition becomes true eventually it you really, really need an endless loop: – while (true) {…} there is a for-each loop – see later

7 break, continue Statements break immediately finishes the loop – control continues after the loop's body continue skips the remaining statements in the loop – but doesn't finish the loop – control continues with evaluation of the condition – next iteration return immediately finishes the entire method – control continues after the method call typically within an if statement – e.g.: for (int i = 0; i < 10; i++) { if (shape(i).isOutside()) {continue;} shape(i).draw()) } use condition rather then break


Download ppt "Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a."

Similar presentations


Ads by Google