variable = expression type name; int x;x____________________ int y;y____________________ int...

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

Upload: silvia-davidson

Post on 27-Dec-2015

302 views

Category:

Documents


3 download

TRANSCRIPT

variable = expression

type name;int x; x____________________int y; y____________________int z; z____________________

variable = expression

type name;int x; x______________3_____int y; y____________________int z; z____________________

X = 3

variable = expression

type name;int x; x______________3_____int y; y____________________int z; z____________________variable = expressionX = 33 = x;X + y = 3;

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

taxRate = MD_TAX_RATE;

name = “Joe”; name ‘J’, ‘o’, ‘e’_______________name = Joe; //incorrect

variable = expression

Expression can be a constant:x = 3;taxrate = MD_TAX_RATE;

Or:expression can be a combination of

variables, constants, operators

int x; x ___________10__int y;int z;x = 10;

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

int x; x ___________11int y; y ___________12int z;x = x + 1;y = x + 1;x+1

int x; x ___________11int y; y ___________12int z; z_____________x = x + 1;INCREMENT -numStudents=numStudents+1;++x;

int x; x ___________11int y; y ___________12int z; z___________6_z = 6;++z; or z = z + 1; => z is 7

numWidget = 10;stuff

name

widget

numWidgets 10

numWidgets = numWidgets + 1;stuff

name

widget

numWidgets 11

name = “Toby”;stuff

name ‘T’ | ‘o’ | ‘b’ |’y’ | NULL

widget

numWidgets 11

name = “Toby”;stuff

name ‘T’, ‘o’, ‘b’,’y’, NULL

widget

numWidgets 11

stuff = numWidgets;stuff 11

name ‘T’, ‘o’, ‘b’,’y’, NULL

widget

numWidgets 11

widget = numWidgets*2;stuff 11

name ‘T’, ‘o’, ‘b’,’y’, NULL

widget

numWidgets 11

int stamp;variable = expressionstamp = 14;14 = stamp; //invalid14 = x; //invalid2 + x= 14; //invalid

+

salesTaxitemPricetotalPricetotalPrice = itemPrice + salesTax;

-

x = x-1; //DECREMENT--x;

grossPaynetPaytaxnetPay= grossPay – tax;

division /

10/3 => 3cout << 10/3;10/3integer/integer integer division10/3 => 30/1 => 00/1.0 => .5 (float division)

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

Modulus %

Remainder10 % 5 => 0 25 |10 10 0 remainder

%

8 % 2 => 095 % 2 => 1138 % 2 => 0Useful for determining even numbersnum % 2 If (num %2 == 0) //even number

PEMDAS

• Please• Excuse• My Dear• Aunt Sally

avg = test1 + test2/2;What is wrong?Division has higher precedencetest2/2 happens firstavg = (test1 + test2)/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));y = ((x * 3) – z) + (5 * (a ^ 2));

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

int main(){ int num1; int num2; int num3; int sum;

Step 1. Declare the variables

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

TRACE1. write the variables across the top of the page

num1 num2 num3 sum Output

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;

TRACE2. run each line

num1 num2 num3 sum OutputEnter 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;

TRACE2. run each line

num1 num2 num3 sum Output1 2 3 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;

TRACE2. run each line

num1 num2 num3 sum Output1 2 3 6 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;

TRACE2. run each line

num1 num2 num3 sum Output1 2 3 6 Enter 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;

Write a program to input the price of an item, the sales tax rate and calculate the total price

 .

Input Process Output

itemPrice Input itemPrice, salesTaxRate totalPrice

salesTaxrate Calculate totalPrice

Display totalPrice

Write a program to input the price of an item, the sales tax rate and calculate the total price

 .

Input Process Output

itemPrice Input itemPrice, salesTaxRate totalPrice

salesTaxrate Calculate totalPrice

Display totalPrice

float itemPrice;float salesTaxRatefloat totalPrice;

Write a program to input the price of an item, the sales tax rate and calculate the total price

 .

Input Process Output

itemPrice Input itemPrice, salesTaxRate totalPrice

salesTaxrate Calculate totalPrice

Display totalPrice

float itemPrice;float salesTaxRatefloat totalPrice;

cout << “Enter item price” << endl;cin >> itemPrice;cout << “enter sales tax rate “ <<endl;cin >> salesTaxRate;

Write a program to input the price of an item, the sales tax rate and calculate the total price

 .

Input Process Output

itemPrice Input itemPrice, salesTaxRate totalPrice

salesTaxrate Calculate totalPrice

Display totalPrice

float itemPrice;float salesTaxRatefloat totalPrice;float salesTaxRate;

cout << “Enter item price” << endl;cin >> itemPrice;cout << “enter sales tax rate “ <<endl;cin >> salesTaxRate;

salesTax = itemPrice * salesTaxRate;totalPrice = itemPrice + salesTax;

Write a program to input the price of an item, the sales tax rate and calculate the total price

 .

Input Process Output

itemPrice Input itemPrice, salesTaxRate totalPrice

salesTaxrate Calculate totalPrice

Display totalPrice

float itemPrice;float salesTaxRatefloat 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 “ …