repetition statements l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Repetition Statements PowerPoint Presentation
Download Presentation
Repetition Statements

Loading in 2 Seconds...

play fullscreen
1 / 7

Repetition Statements - PowerPoint PPT Presentation


  • 149 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Repetition Statements' - kipling


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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 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
while s tatement
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
do s tatement
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
for s tatement
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
for s tatement5
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) {…}
repetition statements scope etc
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
break continue statements
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