160 likes | 186 Views
Programming. while Loops. 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;
E N D
Programming while Loops
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): shortcutsame 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;
Shortcut Assignments • Examples int 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 % 100 cout << change << endl; // change is now 65
Loops (Iterative Constructs) • Loops allow a code segment to be executed many times. • Three constructs • while statement • for statement • do-while statement
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 false true action
The while Loop while(it's raining){ <keep the umbrella up> }
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; }
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; }
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 )
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;
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 )
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;
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; }
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