Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMBC CMSC 104 – Section 01, Fall 2016

Similar presentations


Presentation on theme: "UMBC CMSC 104 – Section 01, Fall 2016"— Presentation transcript:

1 UMBC CMSC 104 – Section 01, Fall 2016
More Loops

2 Notes & Announcements Project 5 (including extra credit) is due before next class! Clarification - answers to the truth tables should be either “true” or “false” (alternatively, 0 or 1) Remember, you can write code to check your answer! Still a good idea to know how to do it without writing code Any questions?

3 Practice Truth Table a b (a || a) && (a && !b) 1

4 Practice Truth Table a b (a || a) && (a && !b) False 1 True

5 Counter-Controlled Repetition (Definite Repetition)
If it is known in advance exactly how many times a loop will execute, it is known as a counter-controlled loop. int i = 1 ; while ( i <= 10 ) { printf(“i = %d\n”, i) ; i = i + 1 ; }

6 Counter-Controlled Repetition (con’t)
Is the following a counter controlled loop? while ( x != y ) { printf(“x = %d”, x) ; x = x + 2 ; }

7 Event-Controlled Repetition (Indefinite Repetition)
If it is NOT known in advance exactly how many times a loop will execute, it is known as an event-controlled loop. sum = 0 ; printf(“Enter an integer value: “) ; scanf(“%d”, &value) ; while ( value != -1) { sum = sum + value ; printf(“Enter another value: “) ; }

8 Event-Controlled Repetition (con’t)
An event-controlled loop will terminate when some event occurs. The event may be the occurrence of a sentinel value, as in the previous example. There are other types of events that may occur, such as reaching the end of a data file.

9 The 3 Parts of a Loop #include <stdio.h> int main () {
int i = 1 ; initialization of loop control variable /* count from 1 to 100 */ while ( i < 101 ) test of loop termination condition printf (“%d “, i) ; i = i + 1 ; modification of loop control } variable return 0 ; }

10 The for Loop Repetition Structure
The for loop handles details of the counter-controlled loop “automatically”. The initialization of the loop control variable, the termination condition test, and control variable modification are handled in the for loop structure. for ( i = 1; i < 101; i = i + 1) { initialization modification } test

11 When Does a for Loop Initialize, Test and Modify?
Just as with a while loop, a for loop initializes the loop control variable before beginning the first loop iteration, modifies the loop control variable at the very end of each iteration of the loop, and performs the loop termination test before each iteration of the loop. The for loop is easier to write and read for counter-controlled loops.

12 A for Loop That Counts From 0 to 9
for ( i = 0; i < 10; i = i + 1 ) { printf (“%d\n”, i) ; }

13 We Can Count Backwards, Too
for ( i = 9; i >= 0; i = i - 1 ) { printf (“%d\n”, i) ; }

14 We Can Count By 2’s ... or 11’s … or Whatever
for ( i = 0; i < 100; i = i + 11 ) { printf (“%d\n”, i) ; }

15 A note on counter variable names
You’ll notice we’ve frequently used the variable “i” as a counter variable. Isn’t this bad practice? Yes and no It’s true that single-letter variable names are generally a bad idea, however, convention allows “i” and “j” for very simple counter variables Typically you use “i” as a counter variable for a very simple for loop, and “j” if you’ve got nested for loops

16 What is the output of this?
int i; for ( i = 5 ; i < 10 ; i = i + 1) { printf("%d ", i); }

17 What is the output of this?
int i; for ( i = 5 ; i < 10 ; i = i + 1) { if(i % 2 == 1) printf("%d ", i); }

18 What is the output of this?
int i; for ( i = 5 ; i < 10 ; i = i + 2) { if(i % 2 == 1) printf("%d ", i); }

19 What is the output of this?
int i; for ( i = 5 ; i < 10 ; i = i + 2) { if(i % 2 == 0) printf("%d ", i); }

20 What is the output of this?
int i; for ( i = 5 ; i < 10 ; i = i + 3) { if(i % 2 == 1) printf("%d ", i); }

21 What is the output of this?
for ( i = 0 ; i < 3 ; i = i + 1) { for ( j = 0 ; j < 3 ; j = j + 1) printf("%d ", i + j); } printf("\n");

22 What is the output of this?
for ( i = 0 ; i < 3 ; i = i + 1) { for ( j = 3 ; j < 0 ; j = j - 1) printf("%d ", i + j); } printf("\n");

23 The do-while Repetition Structure
{ statement(s) } while ( condition ) ; The body of a do-while is ALWAYS executed at least once. Is this true of a while loop? What about a for loop?

24 Example do { printf (“Enter a positive number: “) ;
scanf (“%d”, &num) ; if ( num <= 0 ) printf (“\nThat is not positive. Try again\n”) ; } } while ( num <= 0 ) ;

25 An Equivalent while Loop
printf (“Enter a positive number: “) ; scanf (“%d”, &num) ; while ( num <= 0 ) { printf (“\nThat is not positive. Try again\n”) ; } Notice that using a while loop in this case requires a priming read.

26 An Equivalent for Loop printf (“Enter a positive number: “) ; scanf (“%d”, &num) ; for ( ; num <= 0; ) { printf (“\nThat is not positive. Try again\n”) ; } A for loop is a very awkward choice here because the loop is event-controlled.

27 Any loop you desire Anything you can accomplish with a while loop can also be accomplished with a for loop or a do-while loop. All three are interchangeable. Just because you can, doesn’t mean you should! Some situations lend themselves to a specific type of loop.

28 So, Which Type of Loop Should I Use?
Use a for loop for counter-controlled repetition. Use a while or do-while loop for event-controlled repetition. Use a do-while loop when the loop must execute at least one time. Use a while loop when it is possible that the loop may never execute.

29 Nested Loops Loops may be nested (embedded) inside of each other.
Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. It is common to see nested for loops.

30 How many times is the “if” statement executed?
Nested for Loops for ( i = 1; i < 5; i = i + 1 ) { for ( j = 1; j < 3; j = j + 1 ) if ( j % 2 == 0 ) printf (“O”) ; } else printf (“X”) ; printf (“\n”) ; How many times is the “if” statement executed? What is the output ?

31 The break Statement The break statement can be used in while, do-while, and for loops to cause premature exit of the loop. THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

32 Example break in a for Loop
#include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) break ; } printf (“%d “, i) ; printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; OUTPUT: Broke out of loop at i = 5.

33 The continue Statement
The continue statement can be used in while, do-while, and for loops. It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop. THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

34 Example continue in a for Loop
#include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) continue ; } printf (“%d ”, i) ; printf (“\nDone.\n”) ; return 0 ; OUTPUT: Done.

35 int i = 5; while (i < 3) { printf("%d ", i); }
What is the output? int i = 5; while (i < 3) { printf("%d ", i); }

36 int i = 5; do { printf("%d ", i); } while (i < 3);
What is the output? int i = 5; do { printf("%d ", i); } while (i < 3);

37 int i = 5; for ( i = 0 ; i < 3 ; i = i + 1 ) { printf("%d ", i); }
What is the output? int i = 5; for ( i = 0 ; i < 3 ; i = i + 1 ) { printf("%d ", i); }

38 int i = 5; for ( ; i < 3 ; i = i + 1 ) { printf("%d ", i); }
What is the output? int i = 5; for ( ; i < 3 ; i = i + 1 ) { printf("%d ", i); }

39 int i = 5; for ( i = 0 ; 1 ; i = i + 1 ) { printf("%d ", i); }
What is the output? int i = 5; for ( i = 0 ; 1 ; i = i + 1 ) { printf("%d ", i); }

40 What is the output? int i = 5; for ( i = 0 ; 1 ; i = i + i ) { if ( i > 20 ) break; } printf("%d ", i);

41 What is the output? int i = 5; for ( i = 1 ; 1 ; i = i + i ) { if ( i > 20 ) break; } printf("%d ", i);

42 Write as a while and do-while loop
int i; for ( i = 0 ; i < 3 ; i = i + 1 ) { printf("%d ", i); }

43 int i = 0; while (i < 3) { i = i + 1; printf("%d ", i); }
As a while loop int i = 0; while (i < 3) { i = i + 1; printf("%d ", i); }

44 int i = 0; do { printf("%d ", i); i = i + 1; } while (i < 3);
As a do-while loop int i = 0; do { printf("%d ", i); i = i + 1; } while (i < 3);

45 Questions?


Download ppt "UMBC CMSC 104 – Section 01, Fall 2016"

Similar presentations


Ads by Google