CSE 100

32
CSE 100 Common Programming Errors Assignment Statements Incrementing & Decrementing

description

CSE 100. Common Programming Errors Assignment Statements Incrementing & Decrementing. How many bytes?. #include void main () { // the columns below are just for showing on one screen char ch; float num4; unsigned int num3; int num1; double num5; - PowerPoint PPT Presentation

Transcript of CSE 100

Page 1: CSE 100

CSE 100

Common Programming Errors Assignment Statements

Incrementing & Decrementing

Page 2: CSE 100

How many bytes?#include <iostream.h>void main (){ // the columns below are just for showing on one screen char ch; float num4; unsigned int num3; int num1; double num5; long int num2; long num6;

cout <<"\nBytes of storage used by a character:\t\t\t " <<sizeof(ch) <<"\nBytes used by integer:\t\t\t " <<sizeof(num1) <<"\nBytes used by long integer:\t\t " <<sizeof(num2) <<"\nBytes used by unsigned integer:\t\t " <<sizeof(num3) <<"\nBytes used by floating point number:\t " <<sizeof(num4) <<"\nBytes used by double floating point:\t " <<sizeof(num5) <<"\nBytes used by long floating point:\t\t " <<sizeof(num6)<<‘\n’; }

Page 3: CSE 100

How many bytes?

Bytes of storage used by a character: 1

Bytes used by an integer: 4

Bytes used by a long integer: 4

Bytes used by an unsigned integer: 4

Bytes used by a floating point number: 4

Bytes used by a double floating point: 8

Bytes used by a long floating point: 4

Page 4: CSE 100

Assignment Operator

The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement.

This operator assigns from right to left.

valid invalid riker = 5.6 5 = riker

*

Page 5: CSE 100

Assignment Statement

Syntax: variable = value;variable = value;

Examples:compputer = “dumb”;Al_E_Newman = “dumber”;count = count +1;

*

Page 6: CSE 100

Assignment Example 1

#include <iostream.h>#include <iostream.h>void main(void)void main(void){{ int sum;int sum;

sum = 25;sum = 25; cout << “The number stored in sum is " cout << “The number stored in sum is "

<< sum;<< sum; sum = sum + 10;sum = sum + 10; cout << "\nThe number now stored in sum is " cout << "\nThe number now stored in sum is "

<< sum<< ‘\n’; << sum<< ‘\n’;}

Page 7: CSE 100

Assignment Example 1

Output:

The number stored in sum is 25

The number now stored in sum is 35

_ Press any key to continue

Page 8: CSE 100

Assignment Example 2int sum;int sum;

sum = 0;sum = 0;cout << "\nThe value of sum is initially set to " << sum;cout << "\nThe value of sum is initially set to " << sum;sum = sum + 96;sum = sum + 96;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 70;sum = sum + 70;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 85;sum = sum + 85;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 60;sum = sum + 60;cout << "\nThe final sum is " << sum;cout << "\nThe final sum is " << sum;

Page 9: CSE 100

Assignment Example 2

Output:

The value of sum is initially set to 0

sum is now 96

sum is now 166

sum is now 251

The final sum is 311

Page 10: CSE 100

Practice Assignment

An aquarium consists of four panels, all of which are metal except for the largest which is glass. All three side panels are rectangles. The bottom panel is a right triangle. There is no top panel. After getting user input on the dimension, calculate the area of the glass panel.

Page 11: CSE 100

Assume the user enters positive real numbers expressed in inches. Comments are not required.

sample outputHeight: [10.0]Width: [9.0]Length: [12]The glass is 150.0 square inches.Press any key to continue._

Practice Assignment

Page 12: CSE 100

Assignment Operators

A shorthand notation for certain assignments.

They all have right-to-left associativity.

variable op= (expression)

is equivalent to

variable = variable op (expression)

Page 13: CSE 100

+= add then assign-= subtract then assign

*= multiply then assign

/= divide then assign

X -= 3 X = X - 3

pay *= 0.35 pay = pay * 0.35

Assignment Operators

Page 14: CSE 100

Assignment Operators

+= -= *= /= %=1. i += 2 i = i + 22. r *= 7 r = r * 73. j *= (k + 3) j = j * (k + 3)

4. x /= y - 4 x = x /y - 45. hour %= 12 hour = hour % 126. left -= t_out left = left - t_out

Assignment Operators

* *

Page 15: CSE 100

Subtotals

Syntax: variable = variable + variable = variable + new_value;new_value;

Examplesyear_pay = year_pay + pay;balance = balance - debit;counter = counter + 1;counter += 1;

*

} same

Page 16: CSE 100

Increment/Decrement

++ increment

-- decrement

uniary operators

take a single operand

ex. num++num++, num--num--

++num++num, --num--num

Page 17: CSE 100

Increment/Decrement

k = k + 1 k = k + 3

k += 1 k += 3

k ++ no equivalent

Page 18: CSE 100

Increment/Decrement

num = num + 1num = num + 1

num++num++

i = i + 1i = i + 1

i++i++

num = num - 1num = num - 1

num--num--

i = i - 1i = i - 1

i--i--

* * ** *

num += 1num += 1 num num --=1=1

i += 1 i += 1 i i --= 1= 1

Page 19: CSE 100

Increment/Decrementvalue after execution

kk g g

1. k = 7;

2. g = 2;

3. k = g;

4. g = g + 1;

7 %#@$

7 2

2 2

2 3

* * ** *

or combineor combine 3 & 4 3 & 4

k = g++k = g++

Page 20: CSE 100

Increment/Decrement

postfix:postfix: first use it, then alter valuefirst use it, then alter value

z = 10;

v = z--;

cout <<v<<‘\t’<<z;

v z

10 9

count = 10;

k = count++;

cout<<k<<‘\t’<<count;

k count

10 11

* * * *

Page 21: CSE 100

or combineor combine 3 & 4 3 & 4

k = ++gk = ++g

Increment/Decrementvalue after execution

kk g g

1. k = 7;

2. g = 2;

3. g = g + 1;

4. k = g;

7 %#@$

7 2

7 3

3 3

* * ** *

Page 22: CSE 100

Increment/Decrement

prefix:prefix: first alter value, then use itfirst alter value, then use it

z = 10;

v = --z;

cout <<v<<‘\t’<<z;

v z

9 9

count = 10;

k = ++count;

cout<<k<<‘\t’<<count;

k count

11 11

* * * *

Page 23: CSE 100

outputoutput

11 cout << cnt++<<'\n';cout << cnt++<<'\n';

22 cout<<cnt<<'\n';cout<<cnt<<'\n';

33 cout<<(cnt++==guess)<<'\n';cout<<(cnt++==guess)<<'\n';

44 cout<<cnt<<'\n';cout<<cnt<<'\n';

55 cout<<cnt++<<'\n';cout<<cnt++<<'\n';

66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';

Increment/Decrement

* * ** * *

10 // print then inc// print then inc

11

1 // check then inc// check then inc

12

12

13

int cnt = 10, guess = 11;int cnt = 10, guess = 11;

Page 24: CSE 100

outputoutput

11 cout << ++cnt<<'\n';cout << ++cnt<<'\n';

22 cout<<cnt<<'\n';cout<<cnt<<'\n';

33 cout<<(++cnt==guess)<<'\n';cout<<(++cnt==guess)<<'\n';

44 cout<<cnt<<'\n';cout<<cnt<<'\n';

55 cout<<++cnt<<'\n';cout<<++cnt<<'\n';

66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';

Increment/Decrement

* * ** * *

11 // inc then print// inc then print

11

0 // inc then check// inc then check

12

13

13

int cnt = 10, guess = 11;int cnt = 10, guess = 11;

Page 25: CSE 100

Area of Circle -- 1

double radius = 5, area;

const double PI = 3.1416;

area = PI * radius * radius;cout << "The area of a circle of radius ” << radius << " is "<< area <<"\n";

* *

Page 26: CSE 100

Area of Circle -- 1

Output:

The area of a circle of radius 5 is 78.54

Page 27: CSE 100

ºF - ºC Conversion -- 1

double celsius, faren;

faren = 98.6;

celsius = 5.0/9.0 * (faren - 32.0);

cout << faren <<" degrees Fahrenheit equals "

<< celsius <<" degrees celsius.\n";

Page 28: CSE 100

ºF - ºC Conversion -- 1

Output:

98.6 degrees Fahrenheit equals 37 degrees celsius.

Page 29: CSE 100

Increment/Decrementa) cout << j++

b) cout << ++j

c) cout << j += 14

d) cout << j /= 10

e) cout << j *= 10

f) cout << j -= 6

g) cout << (j = 5) + j

h) cout << (j == 5) + j* * * ** * * *

int j = 5;int j = 5;

a)a) 5

b)b) 6

c)c) 19

d)d) 0

e)e) 50

f) f) -1

g) g) 10

h) h) 6

Page 30: CSE 100

Common Programming ErrorsCommon Programming Errors

not declaring all variables

storing data of one type in a variable of a different type. The variable data type is kept.

using a variable before assigning it a value

mixing data types in an operation

in integer division 4/5 = 0

Page 31: CSE 100

MoreCommon Programming Errors

forgetting <<<< and ;;

not initializing variables before use

applying ++++ or ---- to an expression

forgetting >>>> to separate variables in cin

Page 32: CSE 100

dummy box for extra sound

““Sleeping is Sleeping is notnot a waste a waste of time.”of time.”

Deepak Chopra

““Except in Except in C++C++ class” class”Joseph DeLibero