Presentation is loading. Please wait.

Presentation is loading. Please wait.

“For” loops. Common loop form 6 zMost common loop form: z i = start; zwhile (i < = end) z{i++;} zThere is a special form for it that reinforces the following.

Similar presentations


Presentation on theme: "“For” loops. Common loop form 6 zMost common loop form: z i = start; zwhile (i < = end) z{i++;} zThere is a special form for it that reinforces the following."— Presentation transcript:

1 “For” loops

2 Common loop form 6 zMost common loop form: z i = start; zwhile (i < = end) z{i++;} zThere is a special form for it that reinforces the following pattern:

3 for loop 7 zfor (i=start; i<=end; i++) z{… z }  Syntax:  for (expression1;expression2;expression3;) statement(s) zi.e., for (initialization;condition;update) z statement

4 Note: 2 zThe expressions in the for loop are optional:  1. If expression1 is omitted, the initialization (and declaration) of the loop control variable must take place before entry into the loop.  2. If expression2 is omitted, then the loop does not terminate unless it contains a break statement.  3. If expression3 is omitted, then increasing or decreasing the loop variable must take place within the body of the loop.

5 Note: 1 zCounter can be defined in the for loop header--counter variable is now local-- only used inside the loop:

6 For 6 zEx: zfor (int counter = 1;counter <=5;counter ++); z{ screen.print(counter + “\t”); z screen.flush();}  The output from this code is: y1 2 3 4 5


Download ppt "“For” loops. Common loop form 6 zMost common loop form: z i = start; zwhile (i < = end) z{i++;} zThere is a special form for it that reinforces the following."

Similar presentations


Ads by Google