Beruflich Dokumente
Kultur Dokumente
3
ArithmeticOperators,Expressions,and
Assignment
While solving mathematical problems, you might be used some mathematical formulas. For example, how to find area of a circle,
evaluate a function, and so on. In this chapter you will learn how to write mathematical formula using expressions, evaluate each
value using mathematical operations, and manipulate a value using assignment.
An expression is a statement that using one operator or more. Consider the following code.
01#include<iostream>
02usingnamespacestd;
03intmain()
04{
05inta,b,result;
06a=8;
07b=2;
08a=a+1;
09result=ab;
10cout<<result;
11return0;
12}
Statements at line 08 and 09 are expression, i.e. mathematical expressions with one operator (binary operator). If there are more
than one operator in an expression, we use the following rules to decide which operator will be performed first:
If there are more than one of the operators * in an expression, then the leftmost operator is performed first. Similarly if there are
more than one of the operators + and - in an expression, then they are performed from left to right.
For example:
3 * 2 * (13 - 3) + 2 = 3 * 2 * 10 + 2 (parenthesis first)
= 6 * 10 + 2 * next (left-most)
= 60 + 2 * next
= 62 + last
1
3 Arithmetic Operators, Expressions, and Assignment
Exercise #1:
Evaluate these expressions and place your answer in the box below:
a. 2 + 3 * 4 - 5
b. 14 + 8 * 2
c. 17 * (( 3 - 1) * 7)
d. 2.0 + 3.0 * (2.0 - 6.0 * 4.0)
(note : you can create your own C++ code to evaluate each expressions. See example.)
01#include<iostream>
02usingnamespacestd;
03intmain()
04{
05cout<<(2+3*45);
06return0;
07}
Assignment Statements
Now that we know how to set up memory locations, what type of data can be placed in memory locations, and what types of
arithmetic operations can be performed on each type of data, we need to know how to place data into a the memory location of
a variable. We will accomplish this by using an assignment statement or by using the C++ input statement.
A value may be placed into a memory location with the assignment statement which has the form:
variable_name=expression;
where the expression is always evaluated first, then the resulting value is assigned to the variable. Note that this value replaces
any old value that the variable had. For example, the assignment:
intage;
age=21;
gives the variable age the value 21. If the next statement is:
age=age*2;
then 21 is replaced with 42.
Note: The data types of the variable on the left and the expression on the right should agree. It would not be proper to use:
age=21.5;
2
3 Arithmetic Operators, Expressions, and Assignment
Exercise #2:
Write valid C++ assignment statements for the following mathematical expressions:
a. A = 0.5 w h
b. E = J(J + 1)h
c. Place the sum of the values in t1, t2, and t3 into the variable sum.
Create a program to calculate the amount of fuel consumption of a car, which input in kilometer start and end of the trip, start
and end of the tank contents (fuels) in liters, then print a distance covered, the amount of consumption, and
consumption of fuel per kilometer.
Output: distance_covered, ., .
Process:
1. Enter ., , ., . .
2. distance_covered = km_end km_start
3. = .... .
4. = / ..
5. Display distance_covered, ., .
#include<iostream>
usingnamespacestd;
intmain()
{
doublekm_start,km_end,liters_start,,distance_covered,,;
<<Enterkilometersstart:;
cin>>;
<<Enterkilometersend:;
cin>>;
<<Enterlitersstart:;
cin>>;
<<Enterlitersend:;
>>;
distance_covered=;
=;
=;
cout<<Distancecovered<<distance_covered<<km<<;//addnewline
cout<<Amountofconsumption <<<<liters;<<;//addnewline
cout<<Consumptionoffuelperkilometer<<<<liters;
return;
}
3
3 Arithmetic Operators, Expressions, and Assignment
Complete your script, compile, and run it. Try for several input, write your result in the box below.
Exercise #3:
1. Write the following formulas into C++ expressions
a2 1 1
b 2 4ac a.(b + c)
bc 1 x2
2. Determine the result of the following expressions:
17/317%31/21/2*(x+y)
3. Based on these declarations
floatx;
intk,i=5,j=2;
determine the value of k and x based on the following assignments:
k=i/j;
x=i/j;
k=i%j;
x=5.0/j;
Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
Syntax
value+=increase;
equals to value=value+increase;
Example
a+=5, equals to a=a+5
Syntax
value=decrease;
equals to value=valuedecrease;
Example
a=5, equals to a=a5
Syntax
value*=units;
equals to value=value*units;
Example
a*=5, equals to a=a*5
Exercise #4
Consider declaration at Exercise #3, point 3. Determine the result of the following expressions
k+=j;
i=j;
k*=i+j;
4
3 Arithmetic Operators, Expressions, and Assignment
01#include<iostream>
02usingnamespacestd;
03intmain()
04{
05inta,b=3;
06a=++b;
07cout<<Valueofa=<<a<<\nValueofb=<<b;
08return0;
09}
Save as prefix.cpp. Compile and run. Write your result in the box below. Explain with your own words.
Questions?