**Arithmetic operators**: They are

* (Multiplication), / (division), – % (modulus/remainder)

+ (addition), – (subtraction)

Each operator has precedence or priority.

The operators in the first line above have higher priority than the operators given in the second line.

Among the same priority, the operations are performed from left to right.

**Built-in Algebraic Functions in C++**

abs(x) returns the absolute value of x, argument x must be an int type

absf(x) x must be float or double type

sqrt(x) returns the square root of x (x must not be –ve).

exp(x) returns exponent of x (ex)

log(x) returns logarithmic (natural) value of x

log10(x) returns log (10 base) value of x

pow(x,n) returns value of x to the power n, both are double/float type

**Built-in Trigonometric Functions in C++**

sin(x), cos(x), tan(x), returns sine, cosine and tangent values of the argument x where, x must be in radian (180 deg=pi=3.14159 radian)

The algebraic and trigonometric functions are available in **math.h** file which you must include in your program to use them.

Header files (.h) are available with C++ compiler where built-in functions and overloaded operators are defined.

__Arithmetic expression__

Expression is the collection of operand (s) and operator (s).

For example, 2 + 3 – a

is an expression where, + and – are two operators and 2,3, and a are operands.

Arithmetic expression always yields or produces a numeric value.

The operands may be numeric constants (literal or symbolic), variable and function. Any function that returns a value is treated as a value. These functions are also called value function.

Example 1. 12*3/4 + 2 – 3

Here, multiplication and division operators present, both have same priority. Since multiplication operator comes first, it will operate first (left to right)

The expression becomes

36/4 + 2 -3-> 9 + 2 – 3 -> 11 – 3 -> 8

The above expression can yield different result if you override their priority (which you can) and in this case, you use parenthesis.

Note that, parenthesis is not an operator (in math, we use it as product, say (2)3 or (2)(3) yields 6).

The above expression can be written as

36/(4+2)-3 which will give 3.

You can also write the expression like (36/(4+2)-3) which will give the same result. Note that the inner-most parenthesis will be removed first.

Example 2.

a=2;

y= a + 3 – sin(30 * 3*3.14159/180) / cos(0.126) + exp(2.0/3.12) + pow(2.5,-1.32)+ log(a/2+0.125);

Always, try to avoid long expression which is difficult to debug and take long time to execute.

The above can be done as:

a=2.0;

b=sin(30 *3 * 3.14159/180.0);

c= log(1/2.0 + 0.125)

y= a + 3 – b / cos(0.126) + exp(2.0/3.12) + pow(2.5,-1.32) + c;

In the above, we have calculated two intermediate values b and c to get the desired value of y

**Relational Operators: **They are

< (less than)

<= (less than or equal to)

> (greater than),

>= (greater than or equal to ),

!= (not equal to ),

== (equal to or equality).

They all have they same priority but their priority is less than that of all arithmetic operators.

**Logical expression:**

Expressions developed using operands and relational operators.

They can yield only two values – true (any non-zero value, 1 in C/c++) or false (zero value). These two values are also called Boolean constants (An English Mathematician, Dr. Boole has developed these constants during his PhD work)).

Ex1. 2>3 yields false

2==2 yields true

2!=2 yields false

In Ex1. only numeric constants are used in the expression. However, you can use named constant, variable and functions in the logical expression.

Ex2**. **Say, a=2, b=3.5

- a) a>2 yields false – look, variable and numeric constant in the expression
- b) a+2 < b-1 yields false

Note, arithmetic operators have higher priority than that of relational operators

Here, arithmetic expressions are evaluated first and then logical expressions

The above becomes

4<2.5 and that yields false

- c) a > sin(b) + 3 . Here arithmetic expressions will be evaluated first and then logical expression

The above three expressions are called **simple logical expression**

**Logical Operators**: they are

! (not)

&& (and)

|| (or)

Their priorities are top to bottom.

Remember, ! Is an unary operator, hence it has priority 1.

Among all types of operators, logical operators have least priority.

**Complex/Composite Logical Expressions:**

– are made by combining simple logical expressions using the logical operators.

Look at the following table the effect of logical operators

In case of && operator, when all the operands are T, the output is T, otherwise the output is F

In case of || operator, when at least one input is T, the output is T, otherwise the output is F

In case of ! operator, it inverts (make opposite) the operand.

Ex1. a) 2>3 && 3==3

F && T

F

- b) 2>3 || 3==3

F || T

F

- c) !2 yields F
- d) !(2+3) yields F
- e) !(2==2) yields F
- f) 2>3 && 3==3 || !(2>1)

F && T || !F (! Have higher priority)

F && T || T

F || T {among && and ||, && have higher priority)

T

Ex2. Given a=6, b=2, c=3

!(a-2 > c -1) && ((b+2) < (c+1)) || (!(b-1)

!(4>2) && (4 < 4) || (!1)

!F && F || F

T && F || F (! Operates first as top priority)

F || F (&& operates first)

F (result)

**Other classification of operators**:

- a) Unary operator: works only on one operand, their Priority is 1.

The unary operators are:

+, -, ++ (increment op), — (decrement op) , & (address op), * (indirection op) and

! (not op.)

Ex. +2 is an expression with unary + op.

-3 is an expression with unary op –

! 2 is a logical expression with logical op !

Increment(++)/decrement (–) operators with priority 1.

They come in two notations: post-fix an pre-fix notation.

2++ ; the result will be 3

a++; //a=a+1

The above two are in post-fix notation

++2; ++a;//both are in pre-fix notation.)

The decrement operator also may be in post-fix and pre-fix notation.

When the increment/decrement operators affect only its operand, there is no difference between post-fix and pre-fix.

Example:

int a=3;

int x=++a; //a=3, x=3

x=a++; //x=3. a=4

a++; //a=5

++a; //a=6

x=–a; //a=5, x=5

x=a–; //x=5, a=4

Complex assignment operators:

+=

-=

*=

/=

%=

Ex. a+=2 means a=a+2

Advantage, we don’t need to refer a twice and this reduce the computing time. Also program size reduces and hence memory requirement is less.

- b) Binary Operator (works on 2 operands) Priority 2

The binary operators are: +, -, *, /, %, &&, ||

Ex. a+2 – here + works on two operands – a and 2

- c) Ternary Operator(!:) -works on three operands.

(also called conditional op)

Its priority is 3

max= 2> 3 ? 2 : 3

Here, the logical expression (first operand) will be evaluated first. If its value is true, it returns 2 (2nd operand) to max, otherwise, returns 3 (third operand).

- b) abs=a>0 ? a : -b
- c) y=a<b ? a : ((c>d)? c : d)

Bitwise Expression

x << 3 //shift 3 bit position to left

y>>1 //shift one bit position to right

The vacant bit position are filled with 0s

Chained Assignment

X=(y=10) or x=y=10;

First 10 is assigned to y and then to x

A chained statement can not be used to initialize variables at the time of declaration.

Ex. float a=b=12.34; //illegal

However,

Float a=12.34, b=12.34; ‘’correct

Embedded Assignment

X=(y=50) * 10;

(Y=50) is an assignment expression/statement known as embedded assignment.

Here, value 50 is assigned to y first and then the result 50*10 is assigned to x.

Operator Overloading

Overloading means assigning different meanings to an operation.

Thus, we can use the same operator for different operations.

Two important overloaded operators are:

<< (insertion or put to ) operator

>> (extraction or get from ) op.

Ex. cout<<55; //displays 55 to screen

cin>>a; //the value supplied by the user from keyboard is assigned to var a.

**Escape sequence characters **

There are some characters in C/C++ that you can use in programming where they escapes from their normal way of interpretation. These are:

\n New line character

\r return character

\b backspace character

\’ apostrophe

\” quotation

\% percentage

We will see their use in programming.

**Command: **

instruction that executes immediately.

Ex. Selection f a menu item in MS Word

**Statement:**

Instruction in a program that do not execute immediately.

Statement in a program executes following a sequence.

__Program__

Collection of statements (instructions) to perform a specific task

A statement may be simple (one line) or complex (multiple lines).

Each statement must terminate with a semicolon (;)

In C/C++, the program structure is as follows:

– pre-processor directive

– [ declarative statement (s) ] //optional

– user-defined function (s)

A program may or may not consist of many user-defined functions, but there must be a function named __ main__ from where program execution begins.

In the pre-processor directive part, we include header file (s) to get predefined functions and overloaded operators.

In the declarative part, we declare variable, constants and functions.

The other type of statement that we will study is: i/o statement, control statement

**Comments**

A special type of statement called comment, which is non-executable.

The comments enhance the readability of a program.

If there is enough comment in a program, a non-programmer can also understand the program.

To terminate any comment, there is no semicolon at end.

Many ways to write a comment:

**single line comment**

// This program finds …. And written by ….

**Multiple comments**

/* This problem

was updated last on Jan…

New version enhances the program efficiency

*/

**White space**

The blank spaces in a program are called white space. In any statement, white spaces are ignored by the compiler.

## Leave a Reply