/    /  Expressions

Expressions

 

An expression is a combination of variables, constants, and operators.

 

Examples:

 

SI=P*T*R/100 (a+b) c/d; 
a*b+c (d+x);
5*a+b+b*c; 
(a+b)*(x+y);

 

Evaluation of expressions:

 

An expression can be evaluated based on the precedence of operators, after the evaluation of an expression, the value of the expression is assigned to the variable.

 

Precedence of operators:

 

Operator precedence determines which operator will be performed first in a group of operators with different precedence.

 

Expressions

 

  • An arithmetic expression without parenthesis will be executed from left to right using the rules of precedence of operators

Highest Presidency :(,),*, /, %

Lowest Presidency: +, –

For instance, 5 + 3 * 2 is calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.

  • During the first pass the highest priority operator (if any) are applied as they are encountered.
  • Whenever parenthesis is used in the expressions, within parenthesis assumes the highest priority.
  • If two or more sets of parenthesis appear one after the another as 9- 12/(3+3)*(2-1). The expression contained in the left-most set is evaluated first and the rightmost set in the last.

 

Example:

 

9-12/ (3+3)*(2-1)
9-12/6*1
9-2*1
9-2
7

 

  • If the parenthesis is nested and in such cases evaluation of the expression will process outwards from the innermost set of the parenthesis.

 

Example:

 

9-(12/ ((3+3)*2)-1
9-(12/6*2)-1
9-(2*2)-1
9-4-1
5-1
4