1 / 7

Repetition Statements

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

kipling
Download Presentation

Repetition Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 1st time • do statement • repeats a block until boolean expression becomes false • checks at the and, after 1st 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(<boolean expression>) { //code} • the body can contain several statements • theexpression 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 (<boolean expression>); • the body can contain several statements • the code is executed • then theexpression 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 (<init statement>; <condition>; <end statement>) { //code} • the body can contain several statements • the init statement is executed • then thecondition 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 (<declare var>; <check var>; <increment var>) { //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, continueStatements • break immediately finishes the loop • control continues after the loop's body • continueskips 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

More Related