Loops

advertisement
Chapter 4 Loops
• Write code that prints out the numbers 1100.
• Very often, we want to repeat a (group of)
statement(s). In C++, we have 3 major ways
of doing this: while loops, do-while
loops, and for loops.
while Loop - Syntax
while (condition)
{
stmt;//Loop body
}
Symantics:
1. Evaluate the condition.
2. If condition is true
execute body of loop
go to step 1
3. If condition is false
the loop terminates and the next
statement after the while loop is executed
int a = 1;
while (a <= 100)
{
cout << a << endl;
a = a + 1;
} //while
cout << “Finished”;
What will happen if the statement a = a + 1 is
deleted?
There are several different ways to increment a
value.
a = a + 1;
a += 1;
a++;
++a;
For looping  a++;
Incrementing values
num = num + 1;
num++; //postincrement
++num; //preincrement
See Page 48 for the difference between
postincrement and preincrement.
Decrementing values
num = num – 1;
num--;
--num;
Specialized Assignment
equivalent statement
num += 4;
num = num + 4;
num -= 4;
num = num – 4;
num *= 4;
num = num * 4;
num /= 4;
num = num / 4;
num %= 4;
num = num % 4;
int p = 1;
while (p < 50)
{
cout << p << endl;
p = p * 2;
} //while
cout << “p = “ << p;
Write a while loop to print out ALL the odd numbers
from 15 to 1
Write a while loop that adds up the numbers from 1
to 100, inclusive, then prints out only the answer.
int sum = 0;
int count = 1;
while (count <= 100)
{
sum = sum + count;
count++;
}// while
cout << “Sum = “ << sum;
cout << “Count = “ << count;
• cond can be any legal Boolean expression
• variables in the cond must have a value prior
to entering the loop
• BE SURE TO MODIFY THE LOOP
CONTROL VALUE INSIDE THE LOOP
BODY!!!!
a = 1;
while (a < 10)
cout << a;
cout << “done”;
==============================
a = 1;
while (a < 10)
{
cout << a;
a--;
} //while
cout << “done”;
• Be sure that the loop control variable does
eventually reach the terminating condition!!!
n = 1;
m = 10;
while (n < m)
{
cout << “m =“ << m
<< “ n =“ << n;
m--;
n++;
} //while
================================
int votes = 0;
bool go = true;
while(go)
{
cout << "Enter number of votes: ";
int n;
cin >> n;
go = (n > 0);
votes = votes + n;
}
cout << "Total votes = " + votes;
• Checking for valid input!
cout <<
“Input a number between 1 and 10”;
cin >> num;
while ((num < 1) || (num > 10))
{
cout << “Invalid. Try again”;
cin >> num ;
}//while
cout << “Thank You!”;
do-while loop
• The do-while loop is very similar to the while
loop, except that the test is made at the bottom
of the loop.
do
{
statement;
}
while (cond);
nextStatement;
Execute the statement/body.
Check cond – if true, execute body and recheck
otherwise, go on to nextStatement
int i = 1;
do
{
cout << “i = “ << i << endl;
i++;
} //do
while (i < 10);
Checking for invalid
input with a do-while
do
{
cout << “Enter a number between 0 and 10”;
cin >> num;
if (num < 0 || num > 10)
cout << “Invalid Input << endl;
}
while (num < 0 || num > 10);
The loop will execute at least one time, but
will continue to execute as long as the input
does not meet the condition.
For Loops
int i;
for(i = 1; i <= 100; i = i + 1)
cout << i;
Syntax:
for (init_action; condition; update_action)
stmt;
next_stmt;
where stmt can be a compound statement.
Symantics:
1. Execute initializing action.
2. Evaluate the condition
3. If the condition is true then execute body of
loop;
Else exit loop
4. Execute update action
5. Repeat from step 2.
int sum = 0;
for (y=1; y <=10; y++)
sum += y;
cout << sum;
===================================
int count = 0;
for (int index = 3; index < 8;
index++)
{
cout << index << “ “
<< index * index;
count++;
} //for index
cout << count;
===================================
What will happen if we add the following statement at
the end of the above code fragment?
cout << index;
===================================
cout << “before loop”;
int x = 5;
for (j=x; j<=4; j++)
cout << j;
cout << “After loop ”;
for (x = 1; x <= 5; x++)
cout << “hello”;
cout << “bye”;
====================================
int fact = 1;
cout << “Enter a number”;
cin >> n;
for (f = 1; f <= n; f++)
fact = fact * f;
cout << fact;
====================================
/* BE CAREFUL */
for (x = 1; x <= 10; x++);
cout << “x = “ << x << endl;
====================================
for (m = 10; m > 0; m--)
cout << m;
cout << m;
Nested Loops
for (a = 1; a < 4; a++)
for (b = 1; b < 3; b++)
cout << “a= “ << a
<< “ b= “ << b
<< endl;
for (a = 1; a < 4; a++)
for (b = 1; b < a; b++)
cout << “a= “ << a
<< “ b= “ << b
<< endl;
for (x = 1; x < 9; x += 2)
{
cout << “x = “ + x);
for (y = x; y < 10; y += x)
cout << “ y = “ << y << endl;
} // for x
• Write a loop that prints out the odd numbers
between 1 and 30, inclusive. (Do it using all
3 constructs; while loop, do-while loop and
for loop)
• total = 1;
for (j=1; j <= 5; j++)
{
total *= 2;
cout << j << “ “;
cout << total;
} //for
Rewrite it using while.
Rewrite it using do-while.
What are the differences among for loops,
while loops and do-while loops?
What’s the minimum number of times the body
of each type of loop can be executed?
SKIP the notes at the end of section 4.4
SKIP sections 4.8 and 4.9
Review Questions:
4.3
4.4
4.6-4.8
4.16-4.17
4.19
Download