Sie sind auf Seite 1von 5

3 Arithmetic Operators, Expressions, and Assignment

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.

Simple Numeric Operators


Some of the operators that may be used on numeric data include:

Operator Integer Example Real Example


+ Add 2 + 3 is 5 3.56 + 4.01 is 7.57
- Subtract 4 - 1 is 3 8.45 -10.3 is -1.85
* Multiply 4 * 3 is 12 0.2 * 100.0 is 20.0
/ Division 4 /3 is 1 4 / 3 is 1.33333
% Modulo 5 % 3 is 2 Not operable

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:

All parenthesized expressions are evaluated first.


The operator * is performed next.
The operators + and - are performed last.

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;

Converting Mathematical Equations


Mathematical expressions and equations can be converted to C++ by using the operators and operator priorities discussed
above. Care must be taken to make sure that the operations with highest priority are done first. For example, to convert the
following mathematical expression to C++,

2A+B

The resulting C++ expression is:

2*A+B

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.

Next, consider the following problem.

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.

Analyze the input, process, and output:

Input: km_start, km_end, liters_start, .

Output: distance_covered, ., .

Process:

Algorithm (please complete)

1. Enter ., , ., . .
2. distance_covered = km_end km_start
3. = .... .
4. = / ..
5. Display distance_covered, ., .

Now, implement your algorithm to C++ code

#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

Increase and decrease


Increase operator (++) and decrease operator (--). They add or subtract a variable by 1. Examples

a++;isequalstoa+=1;ora=a+1;
a;isequalstoa=1;ora=a1;

One of characteristic for using these operators is that these operators can be used as prefix or suffix. That is can be written
weather before (++a) or after (a++) a variable. Basically, a++ or ++a has same meaning but different at the order of operations.
See example below

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.

Now, replace line 06 with



a=b++;

Save as suffix.cpp. Compile and run. Write your result in the box below. Explain with your own words.

Questions?

Das könnte Ihnen auch gefallen