PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
CPE 035
Programming Logic and Design
Laboratory Manual
Activity No. 3
Performing Arithmetic Operations
Objectives: 1. Perform the indicated arithmetic operations;
2. Explain what is an integer data-type;
3. Use relational and logical operators;
4. Identify the importance of comma and sizeof in programming
Materials: Computer & C/C++ programming language software tool
Assignment operator ( = )
The assignment operator assigns a value to a variable.
x = 5;
This statement assigns the integer value 5 to the variable x. The assignment operation always
takes place from right to left, and never the other way around:
x = y;
This statement assigns to variable x the value contained in variable y. The value of x at the
moment this statement is executed is lost and replaced by the value of y.
Consider also that we are only assigning the value of y to x at the moment of the assignment operation.
Therefore, if y changes at a later moment, it will not affect the new value taken by x.
Procedure:
1. Let's have a look at the following code below - I have included the evolution of
the content stored in the variables as comments:
2. Run the program. Place the output on the space provided
output here
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
3. Write your observations on the output or how and why you arrive that output.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
Assignment operations are expressions that can be evaluated. That means that
the assignment itself has a value, and -for fundamental types- this value is the one
assigned in the operation. For example:
y = 2 + (x = 5);
In this expression, y is assigned the result of adding 2 and the value of another
assignment expression (which has itself a value of 5). It is roughly equivalent to:
1 x = 5;
2 y = 2 + x;
With the final result of assigning 7 to y.
The following expression is also valid in C++:
x = y = z = 5;
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
It assigns 5 to the all three variables: x, y and z; always from right-to-left.
Arithmetic operators ( +, - , *, /, % )
The five arithmetical operations supported by C++ are:
operator description
+ addition
- subtraction
* multiplication
/ division
% modulo
Operations of addition, subtraction, multiplication and division correspond literally
to their respective mathematical operators. The last one, modulo operator,
represented by a percentage sign (%), gives the remainder of a division of two
values. For example:
x = 11 % 3;
results in variable x containing the value 2, since dividing 11 by 3 results in 3,
with a remainder of 2.
Compound assignment ( +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= )
Compound assignment operators modify the current value of a variable by
performing an operation on it. They are equivalent to assigning the result of an
operation to the first operand:
expression equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
4. Let’s try another example and place your output on the space provided.
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
5. Write your observations on the output or how and why you arrive that output.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
Increment and decrement ( ++, --)
Some expression can be shortened even more: the increase operator (++) and the
decrease operator (--) increase or reduce by one the value stored in a variable.
They are equivalent to +=1 and to -=1, respectively. Thus:
1 ++x;
2 x+=1;
3 x=x+1;
are all equivalent in its functionality; the three of them increase by one the value
of x.
In case that it is used as a suffix (x++), the value is also increased, but the
expression evaluates to the value that x had before being increased. Notice the
difference:
Example 1 Example 2
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3
In Example 1, the value assigned to y is the value of x after being
increased. While in Example 2, it is the value x had before been increased.
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
6. Create a program in C++ to print the sum, difference, product, quotient and
module of two numbers. Write your answer on the space provided.
Sample Output:
Print the sum of two numbers :
-----------------------------------
The sum of 5 and 2 is : 7
The difference of 5 and 2 is : 3
The product of 5 and 2 is : 10
The quotient of 5 and 2 is : 2.5
The of modulo 5 and 2 is : 1
Relational and comparison operators ( ==, !=, >, <, >=, <= )
Two expressions can be compared using relational and equality operators. For
example, to know if two values are equal or if one is greater than the other.
The result of such an operation is either true or false (i.e., a Boolean value).
The relational operators in C++ are:
operator description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
Here are some examples:
1 (7 == 5) // evaluates to false
2 (5 > 4) // evaluates to true
3 (3 != 2) // evaluates to true
4 (6 >= 6) // evaluates to true
5 (5 < 5) // evaluates to false
Of course, it's not just numeric constants that can be compared, but just any
value, including, of course, variables. Suppose that a=2, b=3 and c=6, then:
1 (a == 5) // evaluates to false, since a is not equal to 5
2 (a*b >= c) // evaluates to true, since (2*3 >= 6) is true
3 (b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
4 ((b=2) == a) // evaluates to true
Logical operators ( !, &&, || )
The logical operators && and || are used when evaluating two expressions to
obtain a single relational result. The operator && corresponds to the Boolean
logical operation AND, which yields true if both its operands are true,
and false otherwise. The operator || corresponds to the Boolean logical operation
OR, which yields true if either of its operands is true, thus being false only when
both operands are false.
For example:
1 ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false )
2 ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )
operator short-circuit
if the left-hand side expression is false, the combined result is false (the
&&
right-hand side expression is never evaluated).
if the left-hand side expression is true, the combined result is true (the right-
||
hand side expression is never evaluated).
Conditional ternary operator ( ? )
The conditional operator evaluates an expression, returning one value if that
expression evaluates to true, and a different one if the expression evaluates
as false. Its syntax is:
condition ? result1 : result2
If condition is true, the entire expression evaluates to result1, and otherwise
to result2.
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
1 7==5 ? 4 : 3 // evaluates to 3, since 7 is not equal to 5.
2
3 7==5+2 ? 4 : 3 // evaluates to 4, since 7 is equal to 5+2.
4
5>3 ? a : b // evaluates to the value of a, since 5 is greater than 3.
a>b ? a : b // evaluates to whichever is greater, a or b.
7. Run this program and place the output on the space provided
8. Write your observations on the output or how and why you arrive that output.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
Comma Operator ( , )
The comma operator (,) is used to separate two or more expressions that are
included where only one expression is expected. When the set of expressions has
to be evaluated for a value, only the right-most expression is considered.
For example, the following code:
a = (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to variable a. So, at the
end, variable a would contain the value 5 while variable b would contain value 3.
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
sizeof
This operator accepts one parameter, which can be either a type or a variable,
and returns the size in bytes of that type or object:
x = sizeof (char);
Here, x is assigned the value 1, because char is a type with a size of one byte.
The value returned by sizeof is a compile-time constant, so it is always
determined before program execution.
9. Create a program in C++ in which you are to find the sum of three numbers. If
the result is greater than 20, the output would be “undefined” thus, show the
result. Place your answer on the space provided
10. Create a program in C++ to find the comparison between a product of three
numbers and a sum of five numbers. Only show the highest number. Place your
answer on the space provided
Procedure 9 Procedure 10
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
Conclusion
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Rubric
1 2 3 4
Student spent too Student spent too Student spent an Student spent an
much time and/or much time and/or adequate amount of adequate amount of
Adequate Time too little time on too little time on time on computer time on computer
Spent on Activity entire computer parts of computer lab activity to ensure lab activity to ensure
lab activity. lab activity. good results. the best results.
Student put little to Student put little Student put a good Student put a great
no effort towards effort towards amount of effort deal of effort
Effort computer lab computer lab towards computer towards computer
activity activity. lab activity. lab activity.
Student Student completed Student completed Student completed
completed less about 1/2 of the about 80% of the all of the computer
Completion of than 1/2 of the computer lab computer lab activity lab activity by the
Task computer lab activity by the due by the due date. due date.
activity by the due date.
date.
Responses and Responses and Responses and Responses and
information given information given information given information given
Reasonable are entirely are unreasonable are reasonable are very reasonable
Response and unreasonable in some areas of throughout most of throughout all of the
Information throughout the the activity. the activity. activity.
activity.
Neatness, Responses and Responses and Responses and Responses and
Readability, and information given information given information given information given
Legibility are entirely are unreadable are neat, readable, are very neat,
unreadable and and illegible and legible readable, and
illegible throughout most of throughout most of legible throughout all
throughout the the activity. the activity. of the activity.
activity.
Instructor: Percentage Total
Name: Subject
Code