ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient % Mod = gives the remainder for integer division 1
Division Integer Division 6 r 3 4 ) 27 ` Floating Point Division 6.75 4 ) 27 int Q1 = 27 / 4; // quotient = 6 int R1 = 27 % 4; // remainder = 3 double Q2 = 27.0 / 4.0; // quotient = 6.75 Data Type Mismatch int Q1 = 27 / 4; // int quotient = 6 int R1 = 27 % 4; // int remainder = 3 double Q2 = 27.0 / 4.0; // double quotient = 6.75 Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 2
Data Type Mismatch Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 If either the 27 is a double (27.0) or the 4 is a double (4.0), or both, the result would be 67.5 double Q2 = 27 / 4.0; // double quotient = 6.75 Data Type Mismatch Compilers will promote an integer to a double, but will either issue a Loss of Precision Warning or an Error when converting from a double to an integer. If only a warning is issued, the program will still compile and run, but you may not get the results you were expecting. int Q1 = 27.0 / 4; // double quotient = 6.75 is // converted to an integer 6 by // the assignment = statement 3
Standard Calculator Left to Right 2 * 3 + 4 * 5 2*3=6 6+4=10 10*5=50 Order of Operation Scientific Calculator Multiply & Divide before Add & Subtract 2 * 3 + 4 * 5 6 + 20 26 Order of Operation Multiply and Divide before Add and Subtract 2 * 3 + 4 * 5 6 + 20 26 Precedence ( ) Parentheses ( ) * / % Multiply, Divide, Mod + Add, Subtract = Assignment You can change the order of computation by using parentheses int x = 2 * ( 3 + 4 ) * 5 ; // answer is 70 // 2 * ( 3 + 4 ) * 5 2 * 7 * 5 70 int x = ( 2 * 3 + 4 ) * 5 ; // answer is 50 // ( 2 * 3 + 4 ) * 5 (6+4) * 5 20 * 5 50 4
Arithmetic Assignment Operators int a = 10; int b = 20; a = a + b; // now a = 30 int a = 10; int b = 20; a += b; // now a = 30 a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; Arithmetic Assignment Operators a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; a /= b; is the same as a = a / b; a %= b; is the same as a = a % b; Other arithmetic and logical operators work the same. 5
Auto Increment Auto Decrement The C language introduced the auto-increment and auto-decrement. The auto-increment adds one to a numeric variable. For example: int a = 5; a++; // after the statement, a equals 6 int b = 10; b--; // after the statement, b equals 9 Pre-Increment Post-Increment The increment (or decrement) can be applied either at the beginning of the statement or after everything else in the statement has executed int a = 5; int x = ++a + 10; // a = 6, x = 16; int b = 5; int x = b++ + 10; // b = 6, x = 15 The variable b does not get incremented to a 6 until the rest of the statement has executed. The value of b was still a 5 when 10 was added to get a 15. The 15 was stored into x before b was incremented. 6
The if Statement if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; 7
if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is C" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 8
if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 9
Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. 10
Compound if / else if (score >= 70) cout << "Good work." << endl; cout << "You passed." << endl; else cout << "Try harder." << endl; cout << "You can do it." << endl; Programming Style The opening curly brace can be on the same line as the if if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; -------- or ----------- if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; 11
Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; 0 1 2 3 4 Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 0 1 2 3 4 Done Repetition Without Loops // Input and add three numbers int total = 0; int number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; 12
Repetition With at Loop // Input and add three numbers int total = 0; int number; int = 0; while (i < 3) cout << "Enter a number: "; total += number; Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; 0 1 2 3 4 Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 0 1 2 3 4 Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. 13
Simple Loops int i = 10; while ( i < 5 ) cout << i << endl; cout << "Done << endl; Done int i = 10; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 10 Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. The Sum of Input Values int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter 14
Be Careful of the Semi-colon int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 5 5 times in the loop Done 15
Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 (at start of loop) value = 12 (from the user) total = 12 i++ = 1 (at end of loop) i<5 (y/n) y Loop? (y/n) y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 value = 12 23 total = 12 35 i++ = 1 2 i<5 (y/n) y y Loop? (y/n) y y 16
Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 value = 12 23 9 total = 12 35 44 i++ = 1 2 3 i<5 (y/n) y y y Loop? (y/n) y y y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 value = 12 23 9-6 total = 12 35 44 38 i++ = 1 2 3 4 i<5 (y/n) y y y y Loop? (y/n) y y y y 17
Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 value = 12 23 9-6 37 total = 12 35 44 38 75 i++ = 1 2 3 4 5 i<5 (y/n) y y y y n Loop? (y/n) y y y y n Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 5 value = 12 23 9-6 37 total = 12 35 44 38 75 75 i++ = 1 2 3 4 5 i<5 (y/n) y y y y n Loop? (y/n) y y y y n 18
Counters Start from Zero It is most common to start counting from zero because the first index (position) in an array starts with zero. = 0 This may be confusing for most Americans who count the ground floor of a building as the first floor, but the rest of the world counts the first floor as the floor above ground. The End The End The do while loop will be covered later 19