Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Agenda for loop Short-handed notation How to use break and continue 2.

Similar presentations


Presentation on theme: "1. Agenda for loop Short-handed notation How to use break and continue 2."— Presentation transcript:

1 1

2 Agenda for loop Short-handed notation How to use break and continue 2

3 for loop There are statements that are needed to be repeated. The exact amount of loops (repetitions) is known. Example I: Calculate the sum of 1 to 1000. 3

4 Syntax for (expression1; expression2; expression3) { 1 st statement; 2 nd statement; … n th statement; } where expression1: starting condition expression2: In-the-loop condition expression3: variable update condition 4 Example: int counter, sum = 0; for (counter = 1; counter <=1000; counter = counter+1) { sum = sum + counter; } printf("The sum is %d \n",sum);

5 Syntax Recall that while loop uses the following format expression1; while (expression2) { 1 st statement; … n th statement; 3 rd expression; } 5

6 Flowchart for (counter = 1; counter <=10; counter = counter+1) { statement; } 6 Entry counter = 1 counter <= 10 statement counter = counter + 1 Exit True False

7 Example Consider the following program that displays the number from 1 to 10. Compare the codes with the do loop and do-while loop that perform the same task. 7

8 Example #include int main() { int counter; printf("Print counter from 1 to 10\n"); for (counter = 1; counter <=10; counter = counter + 1) { printf("%d ", counter); } return 0; } 8 Print counter from 1 to 10 1 2 3 4 5 6 7 8 9 10

9 Short-handed notation OperatorsExpressionsMeaningsFinal values +=c += 7c = c + 7 10 -=d -= 4d = d – 4 1 *=e *= 5e = e * 520 /=f /= 3f = f / 3 2 %=g %= 9g = g % 9 3 9 Useful notations in statements (assignment operators) Let c = 3, d = 5, e = 4, f = 6, g = 12

10 More on short-handed notations OperatorsExamplesMeanings ++++aIncrease a by 1, and use the new value of a ++a++Use the value of the current a, and add 1 to a afterward ----bDecrease b by 1, and use the new value of b --b--Use the value of the current b, and subtract 1 from b 10 Let a = 10, b = 5 x = ++a; // The results are: x = 11, and a = 11 y = a++; // The results are: y = 10, and a = 11 u = --b; // The results are: u = 4, and b = 4 v = b--; // The results are: v = 5, and b = 4

11 (Precedence) Order of operations OperatorsOrder ++ -- + - !right to left * / %left to right + -left to right >=left to right == !=left to right &&left to right ||left to right ? :right to left = += -= *= /= %=right to left,left to right 11

12 Example on using ++ 12

13 Example #include int main() { int c; /* demonstrate postincrement */ c = 5; printf(“%d\n”, c); printf(“%d\n”, c++); /* print c then increase */ printf(“%d\n\n”, c); /* demonstrate preincrement */ c = 5; printf(“%d\n”, c); printf(“%d\n”, ++c); /* increase then print c */ printf(“%d\n”, c); return (0); } 13 556566556566

14 Using break and continue break or continue is used to re-route/jump the path under a special circumstance. For more organized programming, avoid using break and continue if possible. 14

15 break In the loop (while, do-while, for) or switch statement, using break results in the leaving the loop immediately. The program continues at the statement after the loop Example The following program displays the counter using for loop, when the counter is 5, leave the loop immediately. 15

16 Example #include void main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) { /* if x is 5, terminate loop */ break; } printf("%d", x); } printf("\n Broke out of loop at x == %d\n", x); } 16 1 2 3 4 Broke out of loop at x == 5

17 continue In the loop (while, do while, for), at the n th iteration, using continue results in the program skips the rest of statements in the current iteration. However, the program continues on the next (n+1) th iteration. Example The following program displays the counter. At the 5 th iter, printf is skipped, and continue on the 6 th iter. 17

18 Example #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) { /* if x is 5, skip remaining code in loop body*/ continue; } printf("%d", x); } printf("\n Used continue to skip printing the value 5\n"); return; } 18 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5


Download ppt "1. Agenda for loop Short-handed notation How to use break and continue 2."

Similar presentations


Ads by Google