Lecture - 3 - Precedence and Control Statements-1

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

PRECENDENCE

In an expression which involves several operators, the order in which the operators
are evaluated depends on the priority given to them. The priority given to the
operators is called precedence. The operators such as *, /, % have higher
precedence than the operators +,-. The operators *, / and % have same precedence
and operators + and - have same precedence. For example, in the statement

a+b*c; b*c is evaluated before and the result of b*c is added to a. It is same as

(a+(b*c));

ASSOCIATIVITY

If the expression contains operators of same precedence then the order in which
they are evaluated can be either left associative or right associative. Left associative
means that left most operators are evaluated first then the operators on the right.
Right associative means that right most operators are evaluated first than the
operators on the left. If the expression a+b+c is evaluated in the left associative
manner, then result of a+b is added to c. Similarly in the right associative, result of
b+c is added to a.

PARENTHESES

If the expression contains parentheses, then the natural order of execution of the
operators is overridden by the parentheses. The parentheses are given more priority
than the any of the operators. Parentheses force the operations to have higher
precedence. If the expression contains nested parentheses, then the innermost
parentheses are evaluated first. For example in the expression a*(b+c), b+c is
evaluated before the multiplication operation.

Here is the program which illustrates the meaning of precedence, associativity and
parentheses.

1
In the given program a=20, b=30 and c=40. In the statement

a+a*b-c;

a*b is evaluated first then the operators + and – are evaluated. The operation a*b
gives 600 and then a is added and c is subtracted which gives the final result 580.

In the statement a*b/c, the operators * and / have same precedence and it is left
associative. The multiplication operator is evaluated before the division operator.
The operation a*b gives the result 600 which is then divided by 40 and giving the
final result as 15.

In the last statement parentheses are used, the innermost expression (b/c) is
evaluated first giving the result 0 where b=20 and c=40, which is then multiplied
with variable a and the final result is 0.

INCREMENT, DECREMENT OPERATOR

The operator ++ and -- are called an increment operator and decrement operator
respectively. They are unary operators and are applied to integers. They perform the
same function as x=x+1 in the case of increment operator and performs the

2
function x=x-1 in the case of decrement operator. Increment operator increments
the value of variable x by 1 whereas decrement operator decreases the value of
variable x by 1. For example ++x and x=x+1 have same effect. These operators
are more precise and directly modify the values. If the operator is written in front of
the variable then it is called prefix form whereas if the operator is written after the
variable then it is called postfix form. Prefix form and postfix form have different
meanings.

Prefix form - ++x. It means that value of variable x is incremented before the value
is used in context. For example, z=++x; when x=6 gives z=7 and x=7. Same is the
case with decrement operator for example, the expression z=--x when x=6 gives
z=5 and x=5.

Postfix form - x++. It means that value of variable x is incremented after the value
is used in context. For example z=x++, when x=6 gives z=6 and x=7. Same is the
case with decrement operator for example, the expression z=x-- when x=6 gives
z=6 and x-5.

The op= ASSIGNMENT OPERATORS

These operators consist of an operator sign and equality operator. They modify the
current value of the variable by performing an operation. Consider the following case

x +=2; is same as x=x+2;

x *=2 ; performs the operation x = x * 2 and gives the value 6 when the original
value of x was 3.

Basically these operators are short hand notations for the normal operators where
the meaning is that the operation specified in the statement has to be performed on
the variable left to the operator with the variable right to the equality operator and
the value is stored in the variable left to the operator.

Control Structures

If Else
3
The if else statement is a type of control structure. A control structure is a
instruction, statement or group of statements which determines the sequence of
execution of other statements. The basic operation of if else statement is that a
statement or group of statements is executed under if, if the value of expression is
true and if the expression is false, statements under else are evaluated. In C++
language, zero is false everything else is true. Statement associated either with if or
else are executed not both group of statements are executed. The else clause is
optional. The general form of if else statement is:-

if (expression)

statement;

else

statement;

Or

if (expression)

statement;

The curly braces mark the beginning and the end statements under if and else.

Here is a program which illustrates the functioning of if else statement.

4
The statement

if (c>=10)

checks whether the value of variable c is less than or equal to 10. The expression
evaluates to be true and as a result statement

cout << “Number is less then equal to 10”

is executed. The statement

cout << “Number is greater then 10”

under else is not executed as value of the variable c is not greater than 10.

The position of semicolon is after the first statement following if and the expression.
There is no semicolon after the expression in parentheses as if and expression are
bounded to the statement or group of statements following the expression. Here is
the program which illustrates the working of statements under else.

5
The statement

if (c>=10)

now evaluates to be false because the value of variable c is 15. The statement
under if is not executed. The statement

cout << “Number is greater then 10”

under else is executed as the expression evaluates to be false.

Nested ifs

The statement that is executed either after if or else can itself be an if statement.
This arrangement is called nested if. In the nested if, the else statement is always
associated with the nearest if statement within the same block. Here is a program
which illustrates the working of nested ifs.

6
The value of the variable c is 6.

The statement

if (c<=10)

evaluates to be true. Then the statement

if (c<=5)

evaluates to be false and hence the statement

cout << “Number is greater than 5 and less than equal to 10”

under else in executed. It shows that else statement is associated with inner if
statement. Here is a program which makes things more clear.

7
Now the value of the variable c is 12. The statement

if (c<=10)

evaluates to be false and hence the statement under else is executed. The
statement

cout << “Number is greater than 10”

is executed as value of c is greater than 10. Now the else statement is associated
with the outer if as else statement is outside the inner block.

You might also like