Chapter 2 (Part 3) - Week 3
Chapter 2 (Part 3) - Week 3
Chapter 2 (Part 3) - Week 3
Contents
Arithmetic Operators
Arithmetic Assignment Operators
Increment and Decrement Operators
Expressions
Precedence and associativity
Type Conversion
Library Function Introduction
Objectives
To understand the arithmetic operators and their precedencies
+ addition ans = 7 + 3
- subtraction ans = 7 - 3
* multiplication ans = 7 * 3
/ division ans = 7 / 3
% modulus ans = 7 % 3
}
Arithmetic Operators
(Example 2)
//display the use of arithmetic operators (addition and multiplication)
#include<iostream>
int main()
{
//calculate addition and multiplication of two numbers
int num1, num2;
cout << "Enter two numbers to be operated with arithmetic operators: ";
cin >> num1 >> num2;
cout << "Num1 + Num2 = " << num1 + num2 << endl;
cout << "Num1 * Num2 = " << num1 * num2 << endl;
}
Output
Enter two numbers to be operated
with arithmetic operators:
12 9
Num1 + Num2 = 21
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
Arithmetic Assignment
Operator(Example)
//assign a single value to three variables and display the sum
#include<iostream>
using namespace std;
int main()
{
int num1, num2, num3;
int result;
num1 = num2 = num3 = 5;//assign the value 5 to variables num1, num2 and num3
#include <iostream>
using namespace std;
int main() { var1 var2 Output
int var1 = 5, var2 = 5; 5 5 5
cout << var1++ << endl; 6 7
6
cout << ++var1 << endl; 6
7 5
cout << ++var2 << endl; 6
4
cout << var2 <<endl; 5
cout<< --var2 <<endl; 5
cout<< var2-- <<endl;
return 0;
}
Expression
is a combination of variables, constants and operators that
represents a computation.
consists of one or more operands, zero or more operators to
compute a value and produces some value.
Example of C++ expression:
(x/y) – z
alpha +12
(alpha – 37) * beta / 2
Precedencies
Operator precedence specifies the order of operations in expressions that contain
more than one operator.
Table 3: Arithmetic operator precedence
Precedence Operator Description Example Associativity
1 () Grouping (a + b) / 4 Left to right
operator
2 ++ Increment ++a Right to left
-- Decrement - -a
3 * Multiplication int i = 2 * 4
/ Division float f = 10 / 3 Left to right
% Modulus int rem = 4 % 3
4 + Addition int i = 2 + 3 Left to right
- Subtraction int i = 5 - 1
5 = assignment int i = 5 Right to left
z = 108
return 0;
}
Type Conversion
Example,
clrscr();
getch()
Example
Write a program that input a number and then display square root value of that
input number.
Procedure
Input – a number
Process - square root value
Output - output the square root result
The library function sqrt() can be used to calculate the square root of a number.
Example
int main()
{
double number, answer;
Place the cursor under the library function and press Ctrl+F1.
The new window shows the header file name on top right corner and Type the
preprocessor directive #include with the header file.
Summary
Arithmetic Operators
Arithmetic Assignment Operators
Increment and Decrement Operators
Expressions
Precedence and associativity
Type Conversion
Library Function Introduction
Reading Assignments
5. C Programming for Engineering & Computer Science by H.H. Tan and T.B. D’Orazio
Exercise
3. Write a program that generates the following output:
10
20
19
Use an integer constant for the 10, an arithmetic assignment operator to
generate the 20, and a decrement operator to generate the 19.
Exercise
Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the
decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler warning.
You can use statements like
float decpounds; // input from user (new-style pounds)
int pounds; // old-style (integer) pounds
float decfrac; // decimal fraction (smaller than 1.0)