1 / 25

Outline

Outline. Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements. Repetition statements. A repetition statement allows us to execute a statement multiple times Often is referred to as loop Like conditional statements

thalassa
Download Presentation

Outline

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. Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements

  2. Repetition statements • A repetition statement • allows us to execute a statement multiple times • Often is referred to as loop • Like conditional statements • They are controlled by Boolean expressions • Java has three types of repetition statements • The while, do, and for loops

  3. The while statement • A while statement has the following syntax • If the condition is true, the statement is executed • Then the condition is evaluated again • And if it is still true, the statement is executed again • The statement is executed repeatedly • Until the condition becomes false while ( condition ) statement;

  4. condition evaluated true false statement Logic of a while Loop

  5. The while statement: example • An example of a while statement • If the condition of a while loop is false initially • the statement is never executed • Therefore, the body of a while loop will execute • Zero or more times int count = 1; while (count <= 5) { System.out.println (count); count++; }

  6. Use of a while loop • Sample program • Compute the average of integer values • Algorithm • Read a series of integer values from the user • Sum them up • Compute their average • See (Average.java)

  7. Infinite Loops • The body of a while loop • eventually must make the condition false • If not, it is called an infinite loop, • which will execute until the user interrupts the program • This is a common logical error • You should always • double check the logic of a program • to ensure that your loops will terminate normally

  8. Infinite Loops • An example of an infinite loop: int count = 1; while (count <= 25) { System.out.println (count); count = count - 1; } • This loop will continue executing • until interrupted (Control-C) or • until an underflow error occurs

  9. Nested Loops • Similar to nested if statements, • loops can be nested as well • That is, the body of a loop can contain another loop • For each iteration of the outer loop, the inner loop iterates completely

  10. Nested Loops • How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; } count1++; } 10 * 20 = 200

  11. break, and continue keywords • breakplaced in the body of any loop • The execution of the loop is stopped, and • the statement following the loop is executed • However => not a good practice • int count = 0; while (count < 5) { System.out.println(count); count++; break; }

  12. break, and continue keywords • continueplaced in the body of any loop • The loop condition is evaluated again, and • the loop body is executed again if it is till true • int count = 0; while (count <=5) { if (count == 4) { count++; continue; } System.out.println(count); count++; }

  13. Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements

  14. The do Statement • A do statement has the following syntax: do { statement; } while ( condition ) • The statement • is executed once initially, then the condition is evaluated • The statement is executed repeatedly • until the condition becomes false

  15. statement true condition evaluated false Logic of a do loop

  16. The do statement: example • An example of a do loop • The body of a do loop executes at least once • See ReverseNumber.java int count = 0; do { count++; System.out.println (count); } while (count < 5);

  17. The while Loop The do Loop condition evaluated statement true true false condition evaluated statement false Comparing while and do

  18. Outline Altering flow of control Boolean expressions Conditional Statements The while Statement Other Repetition Statements

  19. The for Statement • An example of a for loop: for (int count=1; count <= 5; count++) System.out.println (count); • The initialization section • can be used to declare a variable • Like a while loop, the condition of a for loop • is tested prior to executing the loop body • Therefore, the body of a for loop • will execute zero or more times

  20. The statement is executed until the condition becomes false The initialization is executed once before the loop begins The increment portion is executed at the end of each iteration The for Statement • A for statement has the following syntax: for ( initialization ; condition ; increment ) statement;

  21. initialization condition evaluated true false statement increment Logic of a for loop

  22. The for Statement • A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

  23. The for Statement • The increment section can perform any calculation for (int num=100; num > 0; num -= 5) System.out.println (num); • A for loop is well suited • for executing statements a specific number of times • that can be calculated or determined in advance • See Multiples.java • See Stars.java

  24. The for Statement • Each expression in the header • of a for loop is optional • If the initialization is left out, • no initialization is performed • If the condition is left out, it is always considered to • be true, and therefore creates an infinite loop • If the increment is left out, • no increment operation is performed

  25. Comparing loops • The 3 loops are equivalent • i.e., any loop written using one type • can be written using the other two loop types • Usage • Use for when the number of iterations is fixed • To execute the loop body at least once, Use do • Use while for loops executed 0 or more times

More Related