0% found this document useful (0 votes)
45 views13 pages

DR M. Arifur Rahman: PHY-206 Cpna

This document discusses operators in C++ programming. It introduces various types of operators used for calculations and selections. Examples are provided to demonstrate the use of arithmetic, increment/decrement, relational, logical and assignment operators. Precedence rules for operators are also covered, noting that operators like ++ have higher precedence than /, and && has higher precedence than ||. The key differences between prefix and postfix notation for ++ and -- are explained.

Uploaded by

Muheb Bullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views13 pages

DR M. Arifur Rahman: PHY-206 Cpna

This document discusses operators in C++ programming. It introduces various types of operators used for calculations and selections. Examples are provided to demonstrate the use of arithmetic, increment/decrement, relational, logical and assignment operators. Precedence rules for operators are also covered, noting that operators like ++ have higher precedence than /, and && has higher precedence than ||. The key differences between prefix and postfix notation for ++ and -- are explained.

Uploaded by

Muheb Bullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

PHY-206

CPNA

Dr M. Arifur Rahman
Associate Professor
Content

 In this chapter, operators needed for calculations


and selections are introduced.

 Overloading and other operators, such as those


needed for bit manipulations, are introduced in later
chapters.

11/17/20 A Complete Guide to Programming in C++ 2


Operands and operators

11/17/20 A Complete Guide to Programming in C++ 3


Example of Operators

#include <iostream>

using namespace std;

int main()

{
double x, y;
cout << "\nEnter two floating-point values: ";

cin >> x >> y;


cout << "The average of the two numbers is: "
<< (x + y)/2.0 << endl;
return 0;
}

11/17/20 A Complete Guide to Programming in C++ 4


Expressions

 In its simplest form, an expression consists of only one constant, one variable, or one
function call. Expressions can be used as the operands of operators to form more
complex expressions. An expression will generally tend to be a combination of operators
and operands.

 Each expression that is not a void type returns a value. In the case of arithmetic
expressions, the operands define the type of the expression.

 Examples:
 int a(4);
 double x(7.9);
 a * 512 // Type int
 1.0 + sin(x) // Type double
 x – 3 // Type double, since one

 An expression can be used as an operand in another expression.


 Example: 2 + 7 * 3 // Adds 2 and 21

11/17/20 A Complete Guide to Programming in C++ 5


Unary operators and Precedence

11/17/20 A Complete Guide to Programming in C++ 6


Increment / Decrement Operators

 The increment operator ++ modifies the operand by adding 1.

 Given that i is a variable, both i++ (postfix notation) and ++i (prefix notation) raise the
value of i by 1. In both cases the operation i = i + 1 is performed.

 However, prefix ++ and postfix ++ are two different operators. The difference becomes
apparent when you look at the value of the expression; ++i means that the value of i has
already been incremented by 1, whereas the expression i++ retains the original value of
i. This is an important difference if ++i or i++ forms part of a more complex expression:

 ++i i is incremented first and the new value of i is then applied,


 i++ the original value of i is applied before i is incremented.

 The decrement operator -- modifies the operand by reducing the value of the operand
by 1. As the sample program opposite shows, prefix or postfix notation can be used with
--

11/17/20 A Complete Guide to Programming in C++ 7


Effects of prefix and postfix notation

#include <iostream>

using namespace std;

int main()

{
int i(2), j(8);
cout << i++ << endl; // Output: 2
cout << i << endl; // Output: 3
cout << j-- << endl; // Output: 8
cout << --j << endl; // Output: 6
return 0;
}

11/17/20 A Complete Guide to Programming in C++ 8


Precedence

 Operator precedence determines the order of


evaluation, i.e. how operators and operands are
grouped. As you can see from the table opposite, ++
has the highest precedence and / has a higher
precedence than -. The example is evaluated as
follows:

 (val++) – (7.0/2.0). The result is 1.5, as val is


incremented later.

11/17/20 A Complete Guide to Programming in C++ 9


Relational operators

11/17/20 A Complete Guide to Programming in C++ 10


Note

You cannot use the assignment operator = to compare


two expressions.

The compiler will not generate an error message if the


value on the left is a variable. This mistake has caused
headaches for lots of beginners when troubleshooting
their programs.

11/17/20 A Complete Guide to Programming in C++ 11


Logical operators

11/17/20 A Complete Guide to Programming in C++ 12


Precedence of Boolean Operators

 The && (AND) operator has higher precedence


than || (OR). The precedence of both these
operators is higher than the precedence of an
assignment operator, but lower than the precedence
of all previously used operators. This is why it was
permissible to omit the parentheses in the examples
earlier on in this chapter.

11/17/20 A Complete Guide to Programming in C++ 13

You might also like