Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variable = expression type name; int x;x____________________ int y;y____________________ int z;z____________________.

Similar presentations


Presentation on theme: "Variable = expression type name; int x;x____________________ int y;y____________________ int z;z____________________."— Presentation transcript:

1 variable = expression type name; int x;x____________________ int y;y____________________ int z;z____________________

2 variable = expression type name; int x;x______________3_____ int y;y____________________ int z;z____________________ X = 3

3 variable = expression type name; int x;x______________3_____ int y;y____________________ int z;z____________________ variable = expression X = 3 3 = x; X + y = 3;

4 Constant const float PI = 3.141517; float j; j = PI; const float MD_TAX_RATE =.06; salesTax= price * MD_TAX_RATE; const string UNIV_NAME = “Towson University”;

5 taxRate = MD_TAX_RATE; name = “Joe”; name ‘J’, ‘o’, ‘e’_______________ name = Joe; //incorrect

6 variable = expression Expression can be a constant: x = 3; taxrate = MD_TAX_RATE; Or: expression can be a combination of variables, constants, operators

7 int x;x ___________10__ int y; int z; x = 10;

8 int x;x ___________10__=> 11 int y; int z; x = x + 1;

9 int x;x ___________11 int y; y ___________12 int z; x = x + 1; y = x + 1; x+1

10 int x;x ___________11 int y; y ___________12 int z;z_____________ x = x + 1; INCREMENT -numStudents=numStudents+1; ++x;

11 int x;x ___________11 int y; y ___________12 int z;z___________6_ z = 6; ++z; or z = z + 1; => z is 7

12 numWidget = 10; stuff name widget numWidgets10

13 numWidgets = numWidgets + 1; stuff name widget numWidgets11

14 name = “Toby”; stuff name‘T’ | ‘o’ | ‘b’ |’y’ | NULL widget numWidgets11

15 name = “Toby”; stuff name‘T’, ‘o’, ‘b’,’y’, NULL widget numWidgets11

16 stuff = numWidgets; stuff11 name‘T’, ‘o’, ‘b’,’y’, NULL widget numWidgets11

17 widget = numWidgets*2; stuff11 name‘T’, ‘o’, ‘b’,’y’, NULL widget numWidgets11

18 int stamp; variable = expression stamp = 14; 14 = stamp; //invalid 14 = x; //invalid 2 + x= 14; //invalid

19 + salesTax itemPrice totalPrice totalPrice = itemPrice + salesTax;

20 - x = x-1; //DECREMENT --x; grossPay netPay tax netPay= grossPay – tax;

21 division / 10/3 => 3 cout << 10/3; 10/3 integer/integer integer division 10/3 => 3 0/1 => 0 0/1.0 =>.5 (float division)

22 int test1; int test2; float avg; avg = (test1+test2)/2; coercion avg = (float)(test1+test2)/2;

23 Modulus % Remainder 10 % 5 => 0 2 5 |10 10 0 remainder

24 % 8 % 2 => 0 95 % 2 => 1 138 % 2 => 0 Useful for determining even numbers num % 2 If (num %2 == 0) //even number

25 PEMDAS Please Excuse My Dear Aunt Sally

26 avg = test1 + test2/2; What is wrong? Division has higher precedence test2/2 happens first avg = (test1 + test2)/2;

27 y = x * 3 – z + 5 * a ^ 2; y = x * 3 – z + 5 * (a ^ 2); y = (x * 3) – z + 5 * (a ^ 2); y = (x * 3) – z + (5 * (a ^ 2)); y = ((x * 3) – z) + (5 * (a ^ 2));

28

29 int main() { int num1; int num2; int num3; int sum; Step 1. Declare the variables

30 int main() { int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; Step 2. write code for each processing step

31 int main() { int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; Step 2. write code for each processing step

32 int main() { int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << sum; Step 2. write code for each processing step

33 int main() { int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << “sum is “” << sum; Step 2. write code for each processing step

34 int main() { int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; //echo the input cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl; Step 2. write code for each processing step

35 TRACE 1. write the variables across the top of the page num1num2num3sumOutput int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl;

36 TRACE 2. run each line num1num2num3sumOutput Enter two numbers int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl;

37 TRACE 2. run each line num1num2num3sumOutput 123Enter two numbers int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl;

38 TRACE 2. run each line num1num2num3sumOutput 1236Enter two numbers int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl;

39 TRACE 2. run each line num1num2num3sumOutput 1236Enter two numbers 1 + 2 + 3 = 6 int num1; int num2; int num3; int sum; cout << “Enter two numbers “ << endl; cin >> num1 >> num2 >> num3; sum = num1 + num2 + num3; cout << num1 << “ + “ << num2 << “ + “ << num3 << “ = “ << sum << endl;

40 Write a program to input the price of an item, the sales tax rate and calculate the total price. InputProcessOutput itemPriceInput itemPrice, salesTaxRatetotalPrice salesTaxrateCalculate totalPrice Display totalPrice

41 Write a program to input the price of an item, the sales tax rate and calculate the total price. InputProcessOutput itemPriceInput itemPrice, salesTaxRatetotalPrice salesTaxrateCalculate totalPrice Display totalPrice float itemPrice; float salesTaxRate float totalPrice;

42 Write a program to input the price of an item, the sales tax rate and calculate the total price. InputProcessOutput itemPriceInput itemPrice, salesTaxRatetotalPrice salesTaxrateCalculate totalPrice Display totalPrice float itemPrice; float salesTaxRate float totalPrice; cout << “Enter item price” << endl; cin >> itemPrice; cout << “enter sales tax rate “ <<endl; cin >> salesTaxRate;

43 Write a program to input the price of an item, the sales tax rate and calculate the total price. InputProcessOutput itemPriceInput itemPrice, salesTaxRatetotalPrice salesTaxrateCalculate totalPrice Display totalPrice float itemPrice; float salesTaxRate float totalPrice; float salesTaxRate; cout << “Enter item price” << endl; cin >> itemPrice; cout << “enter sales tax rate “ <<endl; cin >> salesTaxRate; salesTax = itemPrice * salesTaxRate; totalPrice = itemPrice + salesTax;

44 Write a program to input the price of an item, the sales tax rate and calculate the total price. InputProcessOutput itemPriceInput itemPrice, salesTaxRatetotalPrice salesTaxrateCalculate totalPrice Display totalPrice float itemPrice; float salesTaxRate float totalPrice; float salesTaxRate; cout << “Enter item price” << endl; cin >> itemPrice; cout << “enter sales tax rate “ <<endl; cin >> salesTaxRate; salesTax = itemPrice * salesTaxRate; totalPrice = itemPrice + salesTax; Cout << “item price is “ << itemPrice << “ sales tax rate is “ …


Download ppt "Variable = expression type name; int x;x____________________ int y;y____________________ int z;z____________________."

Similar presentations


Ads by Google