1 / 19

Flow of Control by Loops

Flow of Control by Loops. Java Loop Statements. A portion of a program that repeats a statement or a group of statements is called a loop . The while statement The do-while statement The for Statement

nelly
Download Presentation

Flow of Control by Loops

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. Flow of Control by Loops

  2. Java Loop Statements • A portion of a program that repeats a statement or a group of statements is called a loop. • The while statement • The do-while statement • The for Statement • The statement or group of statements to be repeated is called the body of the loop. For instance, a loop can be used to compute grades for each student in a class. • There must be a means of exiting the loop. A loop which repeats without ever ending is called an infinite loop!

  3. The while Statement • Syntax: while (Boolean_Expression) Body_Statement; or while (Boolean_Expression) { 1st_Statement; 2nd_Statement; … }

  4. The while statement repeats while a controlling boolean expression remains true. • The loop body contains an action that ultimately causes the controlling boolean expression to become false.

  5. The do-while Statement • It is similar to a while statement, except that the loop body is executed at least once. • Syntax: do Body_Statement while (Boolean_Expression); • Do not forget the semicolon.

  6. The for Statement • A for statement executes the body of a loop a fixed number of times. For example: for (count = 1; count < 3; count++) System.out.println(count); • Syntax: for (Initialization, Condition, Update) Body_Statement • Corresponding whilestatement Initialization while (Condition) Body_Statement_Including_Update

  7. More On for Statement • Possible to declare variables within a for statement int sum = 0; for (int n = 1 ; n <= 10 ; n++) // note that variable n is local to the loop sum = sum + n * n; • A comma separates multiple initializations: for (n = 1, product = 1; n <= 10; n++) product= product * n; • Only one boolean expression is allowed; it can be &&, ||, !. • Also, multiple update actions are allowed: for (n = 1, product = 1; n <= 10; product= product * n, n++);

  8. Nested Loops • The body of a loop can contain any kind of statements, including another loop:

  9. Nested Loops (Cont.)

  10. Ending a Loop: break • Break statement ends only the innermost loop or switch statement that contains the break statement. • Break statements make loops more difficult to understand.

  11. Number of Iterations • If the number of iterations is known before the loop starts, the loop is called a count-controlled loop. • Use a for loop. • Asking the user before each iteration if it is time to end the loop is called the ask-before-iterating technique. • Appropriate for a small number of iterations. • Use a while loop or a do-while loop. • For large input lists, a sentinel value can be used to signal the end of the list. • The sentinel value must be different from the other inputs. E.g., a list of nonnegative scores followed by a negative number, 90, …, 10, -1.

  12. Tracing and Bugs • Tracing variables means checking the variables while the program is running. • Insert temporaryoutput statements in your code to check variables. • Learn to use the debugging facility that may be provided by your IDE. • Common loop bugs: • Unintended infinite loops. • Loops which are off by one iteration. • Testing equality of floating-pointnumbers. • Subtle infinite loops: • Loop may terminate for some inputs, but not for others, e.g., you cannot get out of debt when the penalty exceeds the payment.

More Related