Title: Loops
1Programming
2Loops Iterative Constructs
- Provide
- Ability to control how many times a statement
list is executed - Three constructs
- while statement
- for statement
- do-while statement
3The while Statement
- Syntax
- while (Expression)
- Action
- How it works
- If Expression is true then execute Action
- Repeat this process until Expression evaluates to
false - Action is either a single statement or a group of
statements within braces
4Example N! (while)
- int number, factorial, n
- cout ltlt "Enter number "
- cin gtgt number
- factorial 1
- n 1
- while(n lt number)
- factorial n
- n
-
- cout ltlt "The factorial of " ltlt number
- ltlt " is " ltlt factorial ltlt endl
5The for Statement
- Syntax
- for (ForInit ForExpression PostExpression)
- Action
- How it works
- Execute ForInit statement
- As long as ForExpression
- is true
- Execute Action
- Execute PostExpression
6Example N! (for)
- int number, factorial, n
- cout ltlt "Enter number "
- cin gtgt number
- factorial 1
- for (n1 nltnumber n)
- factorial n
- cout ltlt "The factorial of " ltlt number
- ltlt " is " ltlt factorial ltlt endl
7The Do-While Statement
- Syntax
- do Action
- while (Expression)
- How it works
- Execute Action
- if Expression is true then execute Action again
- Repeat this process until Expression evaluates to
false - Action is either a single statement or a group of
statements within braces
8Example N! (do-while)
- int number, factorial, n
- cout ltlt "Enter number "
- cin gtgt number
- factorial 1
- n 1
- do
- factorial n
- n
- while(n lt number)
- cout ltlt "The factorial of " ltlt number
- ltlt " is " ltlt factorial ltlt endl
9Which Loop to Use?
- For loop
- Usually best for sums, products, and counting
loops. - While loop
- You want to repeat an action without knowing
exactly how many times it will be repeated. - There are situations when the action should not
be executed. - Do-while loop
- The action should always be executed at least
once. - Otherwise, the do-while loops and while loops are
used in similar situations.
10How to Stop a Loop
- Known number of iterations before the loop stops
(for) - Test for a user-controlled
- condition before or after
- each iteration (while, do-while)
- Use the break command.
11Stop a loop break
- The break command is the same as the one used
previously in switch. - break leaves the current loop immediately. It is
recommended that break be used for situations
where the loop needs to be terminated immediately
(e.g., due to user intervention or if a fatal
error occurs). - Use with care !!
12Example Maximum (while with break)
int value //input value int max0 //maximum
value while(true) cout ltlt "Enter a value (-1
to stop) " cin gtgt value if(value gt
max) max value if(value-1) break
cout ltlt "The maximum value found is " ltlt " is
" ltlt max ltlt endl
13Common Loop Errors
- while(balance ! 0.0)
-
- balance balance - amount
-
- This will lead to an infinite loop!
- for(n1 nltcount n)
-
- cout ltlt "hello" ltlt endl
-
- "hello" only printed once!
- while(balance ! 0.0)
- balance balance - amount
-
- balance may not become equal zero due to
numerical inaccuracies
14Nested Loops
- Nested loops are loops within loops.
// Program to output the // multiplication
table int i //Outer loop counter int
j //Inner loop counter for(i1 ilt10
i) for(j1 jlt10 j) cout ltlt ij ltlt "
" cout ltlt endl
Output 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16
18 20 3 6 9 12 15 18 21 24 27 30
15Programming
- Recursion
- (Ch.6, Section 6-9)
16What is Recursion?
- A function calls itself.
- Recursion is one way to decompose a task into
smaller subtasks. - The smallest example of the same task has a
non-recursive solution. - Example The factorial function
- n! n (n-1) (n-2) ... 1
- or
- n! n (n-1)! and 1! 1
- A recursive solution may be simpler to write
(once you get used to the idea) than a
non-recursive solution. - But a recursive solution may not be as efficient
as a non-recursive solution of the same problem.
17Real Life Recursions
- Recursive dreamer
- He dreams .
- He dreams that he dreams .
- He dreams that he dreams that he dreams ..
. - Natural Numbers Definition
- 1 1
- 2 1 1
- 3 1 1 1
18Other Recursion Example
- Fibonacci numbers
- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- - where each number is the sum of the preceding
two. - Recursive definition
- F(0) 0
- F(1) 1
- F(n) F(n-1) F(n-2)
19Recursion General Form
- How to write recursively?
- int rec(parameters)
- if(stopping condition)
- return stopping value
- value rec(revised parameters)
- return value
20Example N! Recursion
- int fac(int n)
- int product
- // the base case
- if(n lt 1)
- return 1
- // the recursive part
- product n fac(n-1)
- return product
-
- void main()
- int number 3
-
- cout ltlt fac(number) ltlt endl
21Execution trace
- fac(3)
- 3 lt 1 ? No.
- product3 3 fac(2)
- fac(2)
- 2 lt 1 ? No.
- product2 2 fac(1)
- fac(1)
- 1 lt 1 ? Yes.
- return 1
- product2 2 1 2
- return product2
- product3 3 2 6
- return product3
- fac(3) has the value 6
22Exercise 4.1
- Write a recursive function for xy
- Suppose both x and y are intergers
- int exp(int x, int y)
-
23Exercise 4.2
- zeros(10200) ?
- int zeros(int n)
- if(n0)
- return 1
- if(n lt 10)
- return 0
- if(n10 0)
- return 1 zeros(n/10)
- else
- return zeros(n/10)
24Exercise 4.3
Write a recursive function to determine how many
factors m are part of n. For example, if n48
and m4, then the result is 2 (48443).