Transcript while

Programming
while Loops
COMP102 Prog Fundamentals I: while Loops/Slide 2
Shortcut Assignments


C++ has a set of shortcut operators for applying an
operation to a variable and then storing the
result back into that variable.
Shortcut assignments (a and b are integers):
shortcut
same as
*=
a *= b;
a = a*b;
/=
a /= b;
a = a/b;
+=
a += b;
a = a+b;
-=
a -= b;
a = a-b;
%=
a %= b;
a = a%b;
COMP102 Prog Fundamentals I: while Loops/Slide 3
Shortcut Assignments

Examples
int i = 3;
i += 4;
cout << i << endl;
// i = i + 4
// i is now 7
double a = 3.2;
a *= 2.0;
cout << a << endl;
// a = a * 2.0
// a is now 6.4
int change = 1265;
change %= 100;
// change = change % 100
cout << change << endl;
// change is now 65
COMP102 Prog Fundamentals I: while Loops/Slide 4
Loops (Iterative Constructs)


Loops allow a code segment to be executed
many times.
Three constructs



while statement
for statement
do-while statement
COMP102 Prog Fundamentals I: while Loops/Slide 5
The while Statement

Syntax
while (condition)
action


How it works:
 if condition is true then
execute action
 repeat this process
until condition
evaluates to false
action is either a single
statement or a group of
statements within braces.
condition
true
action
false
COMP102 Prog Fundamentals I: while Loops/Slide 6
The while Loop
while (it's raining){
<keep the umbrella up>
}
COMP102 Prog Fundamentals I: while Loops/Slide 7
Example 0
#include <iostream>
using namespace std;
int main()
{
char answer;
cout << "Do you want to play the game: (y/n) ";
cin >> answer;
while ( answer == 'y')
{
cout << "Play game ...." << endl;
cout << "Do you want to play again: (y/n) ";
cin >> answer;
}
return 0;
}
COMP102 Prog Fundamentals I: while Loops/Slide 8
Enter a number between 1-- 4
#include <iostream>
using namespace std;
int main()
{
int answer, counter = 1;
cout << "Please enter a number between 1--4:";
cin >> answer;
while ( answer < 1 || answer > 4)
{
cout << "Please enter a number between 1--4:";
cin >> answer;
counter +=1; }
cout << "Thank you for your splendid choice after "
<< counter << " try !" << endl;
return 0;
}
COMP102 Prog Fundamentals I: while Loops/Slide 9
Compute N!
n! (n factorial) is defined as the product of all the integers from
1 to n.
n! = 1*2*3*...*n
or
n! = (n-1)!*n
Example:
5! = 1 x 2 x 3 x 4 x 5 = 120
How to compute 5! ?
1! = 1
( store the temporary result in T, T is 1! )
2! = 1! x 2 = T x 2 = 2
( store the temporary result in T, T is 2! )
3! = 2! X 3 = T x 3 = 6
( store the temporary result in T, T is 3! )
4! = 3! X 4 = T x 4 = 24 ( store the temporary result in T, T is 4! )
5! = 4! X 5 = T x 5 = 120 ( final result )
COMP102 Prog Fundamentals I: while Loops/Slide 10
N!
int number, factorial, counter;
cout << "Enter a positive integer:";
cin >> number;
factorial = 1;
counter = 1;
while(counter <= number){
factorial *= counter;
counter += 1; //counter = counter + 1;
}
cout << "The factorial of " << number
<< " is " << factorial << endl;
COMP102 Prog Fundamentals I: while Loops/Slide 11
Compute 2N
2n - raise 2 to the nth power.
2n = 2n-1 x 2
Example:
20 = 1
22 = 2 x 2 = 4
24 = 2 x 2 x 2 x 2 = 16
How to compute 24 ?
20 = 1
( store the temporary result in T, T is 20 )
21 = 20 x 2 = T x 2 = 2 ( store the temporary result in T , T is 21 )
22 = 21 X 2 = T x 2 = 4 ( store the temporary result in T , T is 22 )
23 = 22 X 2 = T x 2 = 8 ( store the temporary result in T , T is 23 )
24 = 23 X 2 = T x 2 = 16 ( final result )
COMP102 Prog Fundamentals I: while Loops/Slide 12
2N
int number, result, counter;
cout << "Enter a positive integer:";
cin >> number;
result = 1;
counter = 1;
while(counter <= number){
result *= 2; // result = result * 2
counter += 1;
}
cout << "Two raised to the " << number
<< " power is " << result << endl;
COMP102 Prog Fundamentals I: while Loops/Slide 13
Find Maximum Value
int value=0;
// input value
int max=0;
// maximum value
while(value!=-1){
cout << "Enter a positive integer”
<< " (-1 to stop):";
cin >> value;
if(value > max)
max = value;
}
cout << "The maximum value found is "
<< max << endl;
//find the average of a list of integers
#include <iostream>
using namespace std;
int main() {
int listSize = 0;
int value;
double sum = 0;
double average;
cout << "Provide a list of numbers (-1 to stop) "
<< endl;
cin >> value;
while (value != -1) {
sum += value;
listSize += 1;
cin >> value;
}
if(listSize > 0){
average = sum / listSize;
cout << "Average: " << average << endl;
}
else
cout << "No list to average" << endl;
return 0;
}
COMP102 Prog Fundamentals I: while Loops/Slide 15
Paper, Scissors, Rock
"If I'm going to play a sport, I
want one that provides more
exercise, such as
"Paper, Scissors, Rock."
- Dave Barry , SCMP, 4/4/98