Download presentation
Presentation is loading. Please wait.
1
Common Programming Errors Assignment Statements
CSE 100 Common Programming Errors Assignment Statements Incrementing & Decrementing
2
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’; }
3
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
4
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 = = riker *
5
Assignment Statement Syntax: variable = value;
Examples: compputer = “dumb”; Al_E_Newman = “dumber”; count = count +1; *
6
Assignment Example 1 #include <iostream.h> void main(void) {
int sum; sum = 25; cout << “The number stored in sum is " << sum; sum = sum + 10; cout << "\nThe number now stored in sum is " << sum<< ‘\n’; }
7
Assignment Example 1 Output: The number stored in sum is 25
The number now stored in sum is 35 _ Press any key to continue
8
Assignment Example 2 int sum; sum = 0;
cout << "\nThe value of sum is initially set to " << sum; sum = sum + 96; cout << "\nsum is now " << sum; sum = sum + 70; sum = sum + 85; sum = sum + 60; cout << "\nThe final sum is " << sum;
9
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
10
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.
11
Practice Assignment Assume the user enters positive real numbers expressed in inches. Comments are not required. sample output Height: [10.0] Width: [9.0] Length: [12] The glass is square inches. Press any key to continue._
12
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)
13
-= subtract then assign
Assignment Operators += add then assign -= subtract then assign *= multiply then assign /= divide then assign X -= 3 º X = X - 3 pay *= º pay = pay * 0.35
14
Assignment Operators += -= *= /= %=
1. i += 2 i = i r *= 7 r = r * j *= (k + 3) j = j * (k + 3) 4. x /= y - 4 x = x /y hour %= 12 hour = hour % left -= t_out left = left - t_out * *
15
} same Subtotals Syntax: variable = variable + new_value;
Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; } same *
16
++ increment -- decrement
uniary operators take a single operand ex. num++, num num, --num
17
Increment/Decrement k = k + 1 k = k + 3 k += 1 k += 3
k ++ no equivalent
18
Increment/Decrement num = num + 1 num++ num = num - 1 num--
i = i + 1 i++ num = num - 1 num-- i = i - 1 i-- num += num -=1 i += i -= 1 * * * * *
19
Increment/Decrement value after execution k g 1. k = 7; 2. g = 2; 3. k = g; 4. g = g + 1; 7 or combine 3 & 4 k = g++ * * * * *
20
Increment/Decrement postfix: first use it, then alter value
z = 10; v = z--; cout <<v<<‘\t’<<z; v z count = 10; k = count++; cout<<k<<‘\t’<<count; k count * * * *
21
Increment/Decrement value after execution k g 1. k = 7; 2. g = 2; 3. g = g + 1; 4. k = g; 7 or combine 3 & 4 k = ++g * * * * *
22
Increment/Decrement prefix: first alter value, then use it
z = 10; v = --z; cout <<v<<‘\t’<<z; v z count = 10; k = ++count; cout<<k<<‘\t’<<count; k count * * * *
23
Increment/Decrement output 10 // print then inc 11 1 // check then inc
1 cout << cnt++<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(cnt++==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<cnt++<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; int cnt = 10, guess = 11; 10 // print then inc 11 1 // check then inc 12 13 * * *
24
Increment/Decrement output 11 // inc then print 11 0 // inc then check
1 cout << ++cnt<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(++cnt==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<++cnt<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; int cnt = 10, guess = 11; 11 // inc then print 11 0 // inc then check 12 13 * * *
25
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"; * *
26
Area of Circle -- 1 Output: The area of a circle of radius 5 is 78.54
27
ºF - ºC Conversion -- 1 double celsius, faren; faren = 98.6;
cout << faren <<" degrees Fahrenheit equals " << celsius <<" degrees celsius.\n";
28
ºF - ºC Conversion -- 1 Output:
98.6 degrees Fahrenheit equals 37 degrees celsius.
29
Increment/Decrement a) cout << j++ b) cout << ++j
int j = 5; Increment/Decrement a) 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 a) 5 b) 6 c) 19 d) 0 e) 50 f) -1 g) 10 h) 6 * * * *
30
Common 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
31
More Common Programming Errors
forgetting << and ; not initializing variables before use applying ++ or -- to an expression forgetting >> to separate variables in cin
32
“Except in C++ class” Joseph DeLibero
dummy box for extra sound “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” Joseph DeLibero
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.