Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops.

Similar presentations


Presentation on theme: "Loops."— Presentation transcript:

1 Loops

2 While Loops Loops enable programs to repeat certain sections of code as long as a certain condition is true. while (not_done_with_homework()) { work_on_homework(); }

3 While Loop Syntax while (condition) { ... }

4 Problem Write a program which will sum up all the positive integers whose square is less than or equal to 100.

5 Problem Write a program which will sum up all the positive integers whose square is less than or equal to 100. Strategy: 1. Determine what action is to be repeated and what changes in each iteration of the loop. 2. Determine how to know when to stop repeating this action

6 Problem Write a program which will print out an entire string by printing out each character one at a time using a while loop.

7 Problem Write a program which will print out an entire string by printing out each character one at a time using a while loop. 1. The repeated behavior is to print out a character and then update position in the string. 2. We know to exit once we have reached the end of the string.

8 Program Write a program which will print out the all the characters in a given string up until and including the second ‘a’. Things to consider: 1. What is the behavior that will be repeated via looping? 2. How will we know when to exit the loop?

9 For Loops The following structure in while loops is very common for iterating over numbers, strings, lists: int i = 0; while (i < n) { do_something(i); i++; }

10 For loop example The prior example in “for loop” form.
for (int i = 0; i < n; i++) { do_something(i); } For loops are composed of 4 parts: 1. Initialization Statement 2. Exit Condition 3. Update Condition 4. Loop Body

11 For loops Very useful for iterating over structures.
The following code iterates over the characters in a string to print out the entire string. string s = “example”; int n = s.length(); for (int i = 0; i < n; i++) { cout << s[i]; }

12 What do each of the loops print out?
string s = “example”; int n = s.length(); for (int i = 0; i < n; i += 2) { cout << s[i]; } for (int i = n-1; i >= 0; i--) for (int i = 0; i > n; i++)

13 For Loops How to construct a for loop:
1. Determine what you are iterating over (number range, string, list, etc.) 2. Determine where to start (this is the initialization) 3. Determine when to exit the loop (this is the exit condition) 4. Determine what is updated at the end of each loop (this is the update condition) 5. Determine what is to be repeated (this is the body of the loop)

14 Challenge Programs Write a program which will replace every ‘a’ in a string with an ‘o’. Start with the following base: string s = “This is an example”; for (_____; ______; _____) { } What is being iterated over? Where to start, end, update, and what is being done in the body?

15 Challenge Program Write a program which will find the sum of all numbers that are divisible by both 3 and 5 that are less than 100. Things to consider: 1. How to know when to stop 2. What should we be doing in the body of the loop.

16 For Loops Consider the following program which attempts to reverse a string. string s = “example”; int n = s.length(); for (int i = 0; i < n; i++) { s[i] = s[n-i-1]; } cout << s; What will it actually do to the string “example”? What is the content of the string after each loop? How might the program be corrected to actually produce the right result?


Download ppt "Loops."

Similar presentations


Ads by Google