1 / 17

The for Statement

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:.

jdugger
Download Presentation

The for Statement

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. 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;

  2. The for Statement • A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; } • Example: for(inti = 0; i < 10; i++) { //some code } inti = 0; while(i < 10) { //some code i++; }

  3. initialization condition evaluated true false statement increment Flowchart of a for loop

  4. Flowchart - example for (int i = 0; i < 100; i++) { System.out.println( "Welcome to Java!"); }

  5. The for Statement • 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 • The increment section can be used to increment or decrement a variable, for example: for (int num=100; num > 0; num -= 5) System.out.println(num);

  6. Infinite for-loop • If the condition is left out, it is always considered to be true, and therefore creates an infiniteloop

  7. animation Trace for Loop, cont. Execute initializer i is now 0 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  8. animation Trace for Loop, cont. (i < 2) is true since i is 0 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  9. animation Trace for Loop, cont. Print Welcome to Java for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  10. animation Trace for Loop, cont. Execute increment statement i now is 1 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  11. animation Trace for Loop, cont. (i < 2) is still true since i is 1 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  12. animation Trace for Loop, cont. Print Welcome to Java for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  13. animation Trace for Loop, cont. Execute increment statement i now is 2 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  14. animation Trace for Loop, cont. (i < 2) is false since i is 2 for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  15. animation Trace for Loop, cont. Exit the loop. Execute the next statement after the loop for (int i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); }

  16. Which Loop to Use? Use the one that is most intuitive and comfortable for you. In general: • A for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. • A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. • A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.

  17. The break statement • The break statement causes execution to “break” out of the repetitive loop execution (goes to just outside the loop’s closing “}”)

More Related