0% found this document useful (0 votes)
27 views

Lecture - 3 - Arithmetic - Operators - in - C++

The document discusses arithmetic operators in C++, including addition, subtraction, multiplication, division, and modulus operators. It covers operator precedence and order of operations. The document also covers assignment statements, increment and decrement operators, syntax errors, and includes an example program to convert between feet/inches and centimeters.

Uploaded by

Attah Francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture - 3 - Arithmetic - Operators - in - C++

The document discusses arithmetic operators in C++, including addition, subtraction, multiplication, division, and modulus operators. It covers operator precedence and order of operations. The document also covers assignment statements, increment and decrement operators, syntax errors, and includes an example program to convert between feet/inches and centimeters.

Uploaded by

Attah Francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Language (630203)

Fall 2010/2011 – Lecture Notes # 3

Arithmetic operators in C++

Objectives of the Lecture


 Arithmetic operators and Operator Precedence.
 Assignment statement.
 Increment and decrement operators.
 Syntax Errors in C++ program.
 Documentation in C++ program
 Programming Example: Convert Length

Arithmetic Operators and Operator Precedence

C++ arithmetic operators:


o + addition
o - subtraction
o * multiplication
o / division
o % modulus operator
+, -, *, and / can be used with integral and floating-point data types.
% can be used only with integral data types.
Operators can be unary or binary.
Order of Precedence
o All operations inside of () are evaluated first
o *, /, and % are at the same level of precedence and are evaluated next
o + and – have the same level of precedence and are evaluated last
o When operators are on the same level: performed from left to right (associativity)
3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
Expressions
o If all operands are integers, expression is called an integral expression and yields an
integral result, for example: 2 + 3 * 5
o If all operands are floating-point, expression is called a floating-point expression and
yields a floating-point result, for example: 12.8 * 17.5 - 34.50
o If the expression has operands of different data types (integers and floating-point),
expression is called mixed expression, examples of mixed expressions are :
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Assignment Statement
The assignment statement takes the form:

Expression is evaluated and its value is assigned to the variable on the left side
In C++, = is called the assignment operator.

C++ has special assignment statements called compound assignments


+=, -=, *=, /=, and %=
Example:
x *= y;

Increment and Decrement Operators


Increment operator: increment variable by 1
o Pre-increment: ++variable
o Post-increment: variable++
Decrement operator: decrement variable by 1
o Pre-decrement: --variable
o Post-decrement: variable--
What is the difference between the following?
x = 5;
y = ++x;
and
x = 5;
y = x++;
Syntax Errors in C++ program

Errors in syntax are found in compilation


int x; //Line 1 OK
int y //Line 2: error
double z; //Line 3 OK
y = w + x; //Line 4: error

Documentation in C++ program

A well-documented program is easier to understand and modify


You use comments to document programs
Example:
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in

Programming Example: Convert Length

Write a program that takes as input a given length expressed in feet and inches and convert and outputs
the length in centimeters

Problem analysis:
Input: length in feet and inches
o Lengths are given in feet and inches.
o Convert the length in feet and inches to all inches:
 Multiply the number of feet by 12
 Add given inches
o One inch is equal to 2.54 centimeters
Output: equivalent length in centimeters
o Program computes the equivalent length in centimeters
Needed variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in centimeters
Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
Programming Example: Body of the Function
• The body of the function main has the following form:
int main ()
{
declare variables
statements
return 0;
}

You might also like