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

Operators

The document discusses logical operators and logical expressions in C++. It covers: 1. Logical expressions evaluate to true or false and use operators like >, <, ==, !=. 2. Relational operators like >, <, >=, <= allow comparisons between values. 3. Logical operators !, &&, || take logical operands and yield logical results. 4. Short-circuit evaluation stops evaluating an expression as soon as its final value is known.

Uploaded by

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

Operators

The document discusses logical operators and logical expressions in C++. It covers: 1. Logical expressions evaluate to true or false and use operators like >, <, ==, !=. 2. Relational operators like >, <, >=, <= allow comparisons between values. 3. Logical operators !, &&, || take logical operands and yield logical results. 4. Short-circuit evaluation stops evaluating an expression as soon as its final value is known.

Uploaded by

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

C++ Programming

(Operators)

Dr. Pooja Raj Verma


[email protected]

Shaheed Bhagat Singh College


(University of Delhi)
Logical operators and logical expressions

• In C++, the conditions are represented by a logical (boolean) expression.


• An expression that has a value of either true or false is called logical expression.
• true or false are logical (boolean) values.
• Example : suppose i and j are integers. Now consider expression
i>j
If this is a logical expression, it will have the value true if the value of i is greater
than the value of j; otherwise, it will have the value false.
• The symbol > is called a relational operator.
Relational Operators
• The relational operators allow you to make the comparison and to state condition in
the program.
• C++ provides six relational operators, which are as follows:

Operator Description
== equal to
!= not equal to
< less than
<= less than and equal to
> greater than
>= greater than and equal to

• Each of the relational operators is a binary operator because it requires two operands.
Relational Operators and Simple Data Type
• The relational operators are used with all three simple data types.
• Examples :-

Expression Meaning Value


8 < 15 8 is less than 15 true
6! = 6 6 is not equal to 6 false
5.22 < 5.28 5.22 is less than 5.28 true
7.553 >= 7.500 7.553 is greater than or equal true
to 7.500
Relational Operators and floating-point data type
• To check whether two floating numbers are equal is to check whether the
absolute value of their difference is less than a certain tolerance.
• Suppose x and y are two floating-point number and the tolerance is 0.00000001.
Then the x and y are equal only if the absolute value of the difference (x-y) is less
than 0.00000001.
• To find the absolute value of any function, we will use the function fabs.
• The expression fabs(x-y) gives the absolute value of the function (x-y).
• To use pre-defined mathematical functions, the program file must include header
file cmath.
#include <cmath>
Relational Operators and Characters
• For char values, whether an expression using relational operators evaluates to true or false depends
on a machine’s collating sequence.
Expression Values of Expression Explanation
• ‘ ’ < ‘a’ true The ASCII value of ‘ ’ is 32 and ASCII value of ‘a’ is
97. Because 32 < 97 is true, so ‘ ’ < ‘a’ is true
‘R’ > ‘T’ false The ASCII value of ‘R’ is 82 and ASCII value of ‘T’ is
84. Because 82 > 84 is false, so ‘ R’ > ‘T’ is false
‘+’ < ‘ * ’ false The ASCII value of ‘+’ is 43 and ASCII value of ‘*’ is
42. Because 43 < 42 is false, so ‘ +’ < ‘*’ is false
‘>’ <= ‘6’ false The ASCII value of ‘6’ is 54 and ASCII value of ‘>’ is
62. Because 62 < 54 is false, so ‘ >’ <= ‘6’ is false.

• Comparing values of different data types may produce unpredictable results.


• Example:- Suppose we are comparing an integer and a character:
8 < ‘5’
In this expression, 8 would be compared with the collating sequence of ‘5’ which is 53. That is, 8
is compared with 53, which evaluates to true.
Relational Operators and the string Type
• The variables of type string are compared character-by-character, starting with the
first character and using ASCII (American Standard Code for Information
Interchange) collating sequence.
• The character-by-character comparison continues until either a mismatch is
found or the last characters have been compared and are equal.

• Example:
string str1 = “Hello”;
string str2 = “Hi”;
string str3 = “Air”;
string str4 = “Bill”;
string str5 = “Big”;
Relational Operators and the string Type
• The variables of type string are compared character-by-character, starting with
the first character and using ASCII collating sequence.
• The character-by-character comparison continues until either a mismatch is
found or the last characters have been compared and are equal.
• Example:
string str1 = “Hello”;
string str2 = “Hi”;
string str3 = “Air”;
string str4 = “Bill”;
string str5 = “Big”;
ASCII Table
Relational Operators and the string Type
• If two strings of different lengths are compared and the character-by-character
comparison is equal until it reaches the last character of the shorter string, the
shorter string is evaluated as less than the larger string.
Logical operators and logical expressions
• C++ provides three logical operators.
Operator Description
• ! not
&& and
|| or

• Logical operators take only logical value as operand and yield only logical values (true or false) as
results.

The ! (Not) operator :


• The logical operator ! is unary, because it has only one operand.
• !(true) is false and !(false) is true. Putting ! in front of a logical expression reverse the value of that
logical expression
Expression !(Expression) Expression Value Explanation
true (nonzero) false (0) !(‘A’ > ‘B’) true since ‘A’ > ‘B’ is false, !(‘A’ > ‘B’) is true.
false (0) true (1) !(6 < = 7) false since 6 <= 7 is true, !(6 <= 7 ) is false.
Logical operators and logical expressions
The && (And) operator :
• The operator && is binary because this requires two operands.
• The (expression1 && expression2) is true if and only if both expression1 and expression2 are true.
• If any one of them is false then the (expression1 && expression2) is false.
Expression1 Expression2 Expression1 && Expression2
true (nonzero) true (nonzero) true (nonzero)
true (nonzero) false (0) false (0)
false (0) true (nonzero) false (0)
false (0) false (0) false (0)
• Example:
Expression Value Explanation
(14 > = 5) && (‘A’ < ‘B’) true Because (14 > = 5) is true and (‘A’ < ‘B’) is also true and
true && true is true. The expression evaluates to true.
(14 < = 5) && (‘A’ < ‘B’) false Because (14 < = 5) is false and (‘A’ < ‘B’) is also true and
false && true is false. The expression evaluates to false.
Logical operators and logical expressions
The || (Or) operator :
• The operator || is binary because this requires two operands.
• The (expression1 || expression2) is true if and only if at least one of the expression1 or expression2 is true.
• If both of them are false then the (expression1 || expression2) is false.
Expression1 Expression2 Expression1 || Expression2
true (nonzero) true (nonzero) true (1)
true (nonzero) false (0) true (1)
false (0) true (nonzero) true (1)
false (0) false (0) false (0)

• Example: Expression Value Explanation


(14 > = 5) || (‘A’ < ‘B’) true Because (14 > = 5) is true and (‘A’ < ‘B’) is also true and
true || true is true. The expression evaluates to true.
(14 < = 5) || (‘A’ < ‘B’) true Because (14 < = 5) is false and (‘A’ < ‘B’) is also true and
false || true is true. The expression evaluates to true.
(14 < = 5) || (‘A’ > ‘B’) false Because (14 < = 5) is false and (‘A’ > ‘B’) is also false and
false || false is false. The expression evaluates to false.
Short-Circuit Evaluation

• The short-circuit evaluation (of a logical expression) is a process in which the


computer evaluates a logical expression from left to right and stops as soon as
the value of the expression is known.
• Example: Let x and y are of int type variable.
(x > y) || (x == 5)
(x > y) && (x == 5)
In the first statement, if the operand (x > y) evaluates to true, then the whole
expression evaluates to true because true|| true is true and true || true is also true.
Therefore, the value of the operand (x==5) has no bearing on the final outcome.
Similarly, for second statement.
Order of Precedence
• Suppose: 11 > 5 || 6 < 15 && 7 >=8. This logical expression yields different results, depending on whether ||
or && is evaluated first. If || is evaluated first, the expression evaluates to false. If && is evaluated first, the
expression evaluates to true.
Operators Precedence
!, + , - (unary operator) First
*, /, % Second
+, - (binary operators) Third
<, < = , >=, > Fourth
==, != Fifth
&& Sixth
|| Seventh
= (assignment operator) Last
• Using the precedence rules in an expressions, relational and logical operators are evaluated from left to right.
• Because relational and logical operators are evaluated from left to right, the associativity of these operators is
said to be from left to right.
int Data Type and logical (Boolean) Expressions
• In C++, logical expressions can be manipulated or processed in either of two ways:
(i) By using int variables
(ii) By using bool variables
• Since the logical expressions evaluate to either 1 or 0, the value of a logical expression is stored in
a variable of the data type int.
• So, the int data type is used to manipulate logical expressions.
• A nonzero values is treated as true.
• Example:
int legalAge, age;
legalAge = 21;
bool Data Type and logical (Boolean) Expressions
• C++ contains a build-in data type, bool, that has the logical (boolean) values true and false.
• bool data type is used to manipulate the logical expression.
• In C++, bool, true and false are reserved words.
• The identifier true has the value 1 and the identifier false has the value 0.
• Sometimes logical expressions do not behave as you might expect.
• suppose that num is an int variable and we want to write a logical expression
that evaluates to true if the value of num is between 0 and 10, including 0 and 10
and otherwise, it evaluates to false. The expression is
0 < = num < = 10
• Suppose num = 5
Then 0 < = num < = 10 Suppose num = 20
= 0 <= 5 < = 10 Then 0 < = num < = 10
= 0 <= 20 < = 10
= (0<=5) < = 10 = (0<=20) < = 10
= 1 <=10 = 1 <=10
= 1 (true) = 1 (true)

The correct statement for this (0 <= num && num < = 10),

You might also like