While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++...

13
while Loops Programming

Transcript of While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++...

Page 1: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

while Loops

Programming

Page 2: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

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;

Page 3: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 3

Shortcut Assignments

Examplesint i = 3;i += 4; // i = i + 4cout << i << endl; // i is now 7

double a = 3.2;a *= 2.0; // a = a * 2.0cout << a << endl; // a is now 6.4

int change = 1265;

change %= 100; // change = change % 100cout << change << endl; // change is now 65

Page 4: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

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

Page 5: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

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

action

true

false

Page 6: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 6

The while Loop

while (it's raining){<keep the umbrella up>

}

Page 7: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 7

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 )

Page 8: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 8

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;

Page 9: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 9

Compute 2N

2n - raise 2 to the nth power. 2n = 2n-1 x 2

Example:20 = 122 = 2 x 2 = 424 = 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 )

Page 10: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 10

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;

Page 11: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 11

Find Maximum Valueint value=0; // input valueint max=0; // maximum valuewhile(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;

Page 12: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

//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;

}

Page 13: While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.

COMP102 Prog Fundamentals I: while Loops/Slide 13

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