18
1

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

Embed Size (px)

Citation preview

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

1

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

Agendafor loopShort-handed notationHow to use break and continue

2

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

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

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

Syntaxfor (expression1; expression2; expression3){ 1st statement; 2nd statement; … nth statement;}whereexpression1: starting conditionexpression2: In-the-loop conditionexpression3: 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);

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

SyntaxRecall that while loop uses the following formatexpression1;while (expression2) { 1st statement; … nth statement;

3rd expression;}

5

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

Flowchart for (counter = 1; counter <=10; counter = counter+1) { statement;}

6

Entry

counter = 1

counter <= 10

statement

counter = counter + 1

Exit

True

False

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

ExampleConsider 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

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

Example#include <stdio.h> 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 101 2 3 4 5 6 7 8 9 10

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

Short-handed notation

Operators Expressions Meanings Final values

+= c += 7 c = c + 7 10

-= d -= 4 d = d – 4 1

*= e *= 5 e = e * 5 20

/= f /= 3 f = f / 3 2

%= g %= 9 g = g % 9 3

9

Useful notations in statements (assignment operators)

Let c = 3, d = 5, e = 4, f = 6, g = 12

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

More on short-handed notationsOperators Examples Meanings

++ ++a Increase a by 1, and use the new value of a

++ a++ Use the value of the current a, and add 1 to a afterward

-- --b Decrease 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

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

(Precedence) Order of operations Operators Order

++ -- + - ! 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

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

Exampleon using ++

12

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

Example#include <stdio.h>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

556

566

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

Using break and continuebreak 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

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

breakIn 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

ExampleThe following program displays the counter using for

loop, when the counter is 5, leave the loop immediately.

15

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

Example #include <stdio.h> 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 4Broke out of loop at x == 5

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

continue

In the loop (while, do while, for), at the nth 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.

ExampleThe following program displays the counter. At the 5th

iter, printf is skipped, and continue on the 6th iter.

17

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

Example #include <stdio.h> 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 10Used continue to skip printing the value 5