Download presentation
Presentation is loading. Please wait.
1
Computer Programming 1 Repetition
2
Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example First look at algorithm analysis
3
Computer Programming 3 Control flow Flow-control instructions allow the program to decide what action to take and when Control flow if Conditional expression?: switch while do for Selection Repetition
4
Computer Programming 4 Repetition Execute the same sequence of statements multiple times Consider the example of displaying n “A”s, where n is an integer not known at compiling time Consider the example of computing the sum of the first n integers, where n is an integer not known at compile time
5
Computer Programming 5 Looping while a condition is true The simplest form of looping is the while statement while (condition) { //… executed as long as condition is true }
6
Computer Programming 6 Do…while loop while checks the condition first. If it is true, the program does not enter the loop – while is top-tested loop. Use do…while to execute the loop at least once – do…while is bottom-tested loop. Do { //statements inside the loop } while (condition) Expression TF Statement //statements inside the loop while (condition){ //statements inside the loop }
7
Computer Programming 7 The for Loop for (expr1; condition; expr2) { //statements inside the loop } 1. expr1 is evaluated, usually variable initialization 2. condition is evaluated A. If condition is false, leave the for-loop B. If condition is true, I. execute the statements inside the for-loop II. Evaluate expr2 III. Go to step 2 expr1; while (condition) { //statements inside the loop expr2; }
8
Computer Programming 8 The for Loop The for loop most commonly used to count – From one value first to another value last Increment or decrement – To increment/decrement by 1, use ++/-- for (int count = first; count <= last; count++){ //Statements } count = first count <= last Statement count++ F T
9
Computer Programming 9 The for Loop Counter-controlled loops – Increment or decrement a counter
10
Computer Programming 10 Some for Loop options for (;;) { //loop forever //statements inside the loop } – Equivalent to: while (true) { //statements inside the loop } for (; condition; expr2) { //statements inside the loop }
11
Computer Programming 11 Special loop controls: continue and break Use break to exit the current loop immediately Use continue to move back to the top of the loop. The rest of the statements in the loop are ignored for the current iteration while (condition1){ //statements inside the loop if (condition2) break; //statements inside the loop } while (condition1){ //statements inside the loop if (condition2) continue; //statements inside the loop }
12
Computer Programming 12 Special loop controls: continue and break while (condition){ //statements inside the loop } Equivalent to while (true){ if (condition) break; //statements inside the loop }
13
Computer Programming 13 Problem Given an integer n, compute the sum of the numbers from 1 to n. The solution will be provided in class
14
Computer Programming 14 Introduction to Algorithm Analysis Consider once again, Gauss's quick solution (not using repetition) … – Sum of numbers 1 … 5
15
Computer Programming 15 Introduction to Algorithm Analysis So in general: Thus possible to write a non looping version of our sum cin >> n; cout << n * (n + 1) / 2; A better algorithm – Takes many less operations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.