Overview - PowerPoint PPT Presentation

About This Presentation
Title:

Overview

Description:

i is the control variable for the for loop in this example. Initialize control variable ... Note: Put only expressions involving the control variables in the ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 18
Provided by: engi47
Learn more at: http://web.cs.wpi.edu
Category:
Tags: iis | involved | is | overview

less

Transcript and Presenter's Notes

Title: Overview


1
Overview
  • Go over parts of quiz?
  • Another iteration structure for loop

2
for loop
for (expression1 expression2 expression3)
statement Step 1 Evaluate expression1 Step 2
Evaluate expression2. If true, execute
statement If false, continue execution at next
statement following the for loop. Step3
Evaluate expression3 Step 4 Repeat Step 2 and
3 Example Display the numbers from 1 to 10 for
(int i 1 i lt 10 i) cout ltlt i ltlt endl

3
for loop with compound statement
for (expression1 expression2 expression3)
statement1 statement2 Example
Input 10 numbers and calculate the total int i,
number, total 0 for (i 1 i lt 10 i)
cout ltlt \nEnter next number cin gtgt
number total total number cout ltlt The
total of the numbers entered is ltlt total
ltlt endl
4
Exercises
  • Use a for loop structure to solve the following
  • problems
  • Display the numbers from 7 to 77 in steps of 7
  • Display the numbers from 20 to 2 in steps of 2
  • Display the following numbers 2,5,8,11,14,17,20
  • Display the following numbers 99,88,77,66,55,
  • 44,33,22,11,0

5
control variable for the for loop
i is the control variable for the for loop in
this example
Initialize control variable
Final value of control variable
Update control variable
for (i 1 i lt 10 i) cout ltlt i ltlt endl
6
Declaration of loop control variable inside for
structure
for (int i 1 i lt 10 i) cout ltlt i ltlt
endl instead of int i for (i 1 i lt 10
i) cout ltlt i ltlt endl
7
for loops
for (expression1 expression2 expression3)
statement   is equivalent to   expression1
//Initialize loop control
variable   while (expression2) //Test loop
control variable statement
expression3 // Update loop control
variable
8
Example for loop vs while loop
Display the numbers from 1 to 10 for (int i 1
i lt 10 i) cout ltlt i ltlt endl OR int
i 1 while (i lt 10) cout ltlt i ltlt endl
i
9
Example change while loop to a for loop
int sum 0 int count -5   while (count lt
15) sum sum count count
int sum 0 for (int count -5 count lt
15 count) sum sum count
10
When to use for loop
Use the for loop for a counter controlled
repetition Use the while loop for repetition
when there is no counter. (i.e reading in
input). for (int i 1 i lt 10 i) cout ltlt
i ltlt endl bool notDone true while
(notDone) cin ltlt input if (-1 input)
notDone false
11
Omitting expression1
for ( expression2 expression3)
statement Can omit expression 1 if loop control
variable is initialized some place else. Note
place holder Example Display the numbers from 1
to 10 int i 1 for ( i lt 10 i) cout ltlt i
ltlt endl
12
Omitting expression2
for (expression1 expression3)
statement When expression2 is omitted, C
assumes the condition is always true, thereby
creating an infinite loop Example Display the
numbers from 1 to infinity and beyond for (int i
1 i) cout ltlt i ltlt endl
13
Omitting expression3
for (expression1 expression2)
statement When expression3 is omitted, this
loop also becomes an infinite loop unless the
loop control variable is updated inside the
loop Example Display the numbers from 1 to
infinity and beyond for (int i 1 i lt
10) cout ltlt i ltlt endl
14
comma separated lists of expressions
Expression1 and Expression3 may be a comma
separated list of expressions. For example
int inputData   for (int i 0, sum 0 (i lt
10) (sum lt 1000) i) cin gtgt
inputData sum sum inputData Note
Put only expressions involving the control
variables in the initialization and increment
expressions of the for structure.
15
common programming error
Changing the loop control variable inside the
body of the loop is allowed but dangerous. for
(int i 0 i lt 10 i) cin gtgt i
16
common programming error
Problem Vary the control variable over the
following sequence of values 2, 5, 8, 11, 14,
17, 20 Solution1 for (int i 2 i lt 20 i
3) Solution2 for (int i 2 i lt 22 i 3)
Solution2 more confusing. More likely to lead
to boundary condition problems.
17
Exercises
  1. Write a program that inputs 10 grades, determines
    the highest grade and displays the result. Use a
    for loop.
  2. Write the same program as in problem number 1
    using a while loop.
Write a Comment
User Comments (0)
About PowerShow.com