Arithametic Expressions

An  arithmetic  expression  is  a  combination  of  variables, constants  and  operators  arranged  as  per  the syntax  of  the  language.

Java  can  handle  any  complex  mathematical  expressions.

Eg : 3x2+2x+1

      =3*x*x+2*x+1;

Evaluation  of  Expressions :-

Expressions  are  valuated   using an  assignment   statement of  the  form:

Variable  =Expression;

Variable  is any  valid java  variable name.  when the statement  is  encounted  the expression  is  evaluated   first  and  the  result then  replaces the previous  value of  the  variable  on the  left hand  side.

All variables  used  in the   expression  must be assigned  values  before  evaluations is  attempted.

Eg:  Z=a-b  / c+d;

The   variables  a,b,c & d  must  be  defined  before  they  are  used  in  the  expression.

Precedence    of  arithmetic   operators :-

An   arithmetic expression  with out  any  parenthesis   will be evaluated  from left  to  right  using  the Rules  of precedence  of  operators.

There  are  two  district   priority  levels  of  arithmetic  operators  in  java

High priority   :-  *  /  %

Low  :-  +  -

The  basic evaluation   procedure   includes  two  left  to  right  passes  through  the expression.

During  the  first  pass  the  high  priority  operators are   applied  as  they are  encountered.

During  the  second  pass  the  low  priority operators  are  applied as   they  are encountered.  

Consider the  following  evaluation  statement.  

Ex:– x = a–b/3+c*2–1

When a = 9, b =12 &c = 3, the statement becomes

x=9–12/3+3*2–1

and is evaluated as follows:

First pass:

Step1: x = 9–4+3*2–1

Step2:x = 9–4+6–1

Second pass:

Step3: x = 5+6–1

Step4: x = 11–1

Step5:x = 10    

Related

Java 1854810291842531900

Post a Comment

emo-but-icon

item