Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 while Loops Programming

2 COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation to a variable and then storing the result back into that variable. l 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;

3 COMP102 Prog Fundamentals I: while Loops/Slide 3 Shortcut Assignments l Examples int i = 3; i += 4; // i = i + 4 cout << i << endl; // i is now 7 double a = 3.2; a *= 2.0; // a = a * 2.0 cout << a << endl; // a is now 6.4 int change = 1265; change %= 100; // change = change % 100 cout << change << endl; // change is now 65

4 COMP102 Prog Fundamentals I: while Loops/Slide 4 Loops (Iterative Constructs) l Loops allow a code segment to be executed many times. l Three constructs n while statement n for statement n do-while statement

5 COMP102 Prog Fundamentals I: while Loops/Slide 5 The while Statement l Syntax while (condition) action l How it works: n if condition is true then execute action n repeat this process until condition evaluates to false l action is either a single statement or a group of statements within braces. condition action true false

6 COMP102 Prog Fundamentals I: while Loops/Slide 6 The while Loop while (it's raining){ }

7 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 )

8 COMP102 Prog Fundamentals I: while Loops/Slide 8 N! int number, factorial, counter; cout > number; factorial = 1; counter = 1; while(counter <= number){ factorial *= counter; counter += 1; //counter = counter + 1; } cout << "The factorial of " << number << " is " << factorial << endl;

9 COMP102 Prog Fundamentals I: while Loops/Slide 9 Compute 2 N 2 n - raise 2 to the nth power. 2 n = 2 n-1 x 2 Example: 2 0 = 1 2 2 = 2 x 2 = 4 2 4 = 2 x 2 x 2 x 2 = 16 How to compute 2 4 ? 2 0 = 1 ( store the temporary result in T, T is 2 0 ) 2 1 = 2 0 x 2 = T x 2 = 2 ( store the temporary result in T, T is 2 1 ) 2 2 = 2 1 X 2 = T x 2 = 4 ( store the temporary result in T, T is 2 2 ) 2 3 = 2 2 X 2 = T x 2 = 8 ( store the temporary result in T, T is 2 3 ) 2 4 = 2 3 X 2 = T x 2 = 16 ( final result )

10 COMP102 Prog Fundamentals I: while Loops/Slide 10 2N2N int number, result, counter; cout > 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;

11 COMP102 Prog Fundamentals I: while Loops/Slide 11 Find Maximum Value int value=0;// input value int max=0;// maximum value while(value!=-1){ cout << "Enter a positive integer” > value; if(value > max) max = value; } cout << "The maximum value found is " << max << endl;

12 //find the average of a list of integers #include 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; }

13 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


Download ppt "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."

Similar presentations


Ads by Google