learncpponline

Logical Operators in C++

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators in C Plus Plus (C++) language. They are: Operator Meaning && Logical AND || Logical OR ! Logical NOT An expression involving && or || is sometimes called compound expressions, since the expression involves two other expressions, that is, each of these operators (&& and ||) takes two expressions, one to the left and another to the right. Example of && operator: Consider the following expression: a > b && x == 10 The expression on the left is a > b and that on the right is x == 10. The above stated whole expression evaluates to true (1) only if both the expressions are true (i.e., if a is greater than b and the value of x is equal to 10). Example of || operator: Consider the following expression: a...

Relational Operators in C++

There are four relational operators in C Plus Plus (C++). They are: Operator Meaning < less than > greater than <= less than or equal to >= greater than or equal to These operators all fall within the same precedence group, which is lower than the arithmetic and unary operators. The associativity of these operators is left to right. Closely associated with the above mentioned relational operators are the following two equality operators: Operator Meaning == equal to != not equal to The equality operators fall into a separate precedence group beneath the relational operators. These operators also have a left-to-right associativity. These six operators are used to form logical expressions, which represent conditions that are either true or false. The resulting expressions will be of type integer, since true is represented in C++ by the integer value 1 and false by the value 0. Example: Suppose that a, b...

Increment and Decrement Operators in C++

These operators also fall under the broad category of unary operators but are quite distinct than unary minus. The increment and decrement operators are very useful in C++ language. They are extensively used in for and while loops. The syntax of these operators is given below: ++<variable name> <variable name>++ –<variable name> <variable name>– The operator ++ adds 1 to the operand and — subtracts 1 from the operand. These operators manifest in two forms: prefix and postfix. For example, the ++ operator can be used in two ways: ++m and m++ These are two different expressions. The first expression immediately increments the value of m by 1. For example, the statements: int t, m = 1; t = ++m; will cause the value of m to incremented first, and then this value is assigned to t, resulting in both having the same value. On the other hand, the statements:...

Unary Operators in C++

C++ includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators. Unary operators usually precede their single operands, through some unary operators are written after their operands. Perhaps the most common unary operation is unary minus, where a numerical constant, variable or expression is preceded by a minus sign. Note that the unary operation is distinctly different from the arithmetic operator which denotes subtraction (-) as the subtraction operator requires two operands. Example: -743 -0x7fff -0.2 -5E-8 -root1 -(x + y) -3 * (x + y)

Arithmetic Operators in C++

There are five arithmetic operators in C++. They are: Operator Purpose + addition – subtraction * multiplication / division % remainder after integer division The % operator is also sometimes referred to as the modulus operator. Example of the use of Arithmetic Operators: Suppose that the x and y are integer variables whose values are 10 and 3 respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values: Expression Values x + y 13 x – y 7 x * y 30 x / y 3 x % y 1 In the above mentioned example, notice the truncated quotient resulting from the division operation (x / y), since both operands represent integer quantities. Also, notice the integer remainder resulting from the use of the modulus operator in the last expression (x % y). Precedence of Arithmetic Operators: Among the arithmetic operators, *, / and %...

Operators and Expressions in C++

An operator, in general, is a symbol that operates on a certain data-type. For example, the operator + is the addition operator. It can operate on integer, character and real (float and double) numbers. On the other hand, an expression is a combination of variables, constants and operators written according to the syntax of the language. Types of Operators: Arithmetic operators Unary operators Increment and Decrement operators Relational operators Logical operators Assignment operators Conditional operators