• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Arithmetic Operators

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class testing{
public static void main(String args[]){
int i = 3/3*6/2*6-1;
System.out.println(i);
}
}

In the above class , How should I evaluate this expression

int i = 3/3*6/2*6-1;

Tahnks for the reply
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 3/3*6/2*6-1;

* and / - have same priority

3/3 = 1
than
(3/3)*6 = 6
than
((3/3)*6)/2 = 3
than
(((3/3)*6)/2)*6 = 18
than
((((3/3)*6)/2)*6) - 1 = 17
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply. So it starts evaluate from left to right ? then priority between + - and * / ? thanks
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* and / have higher priority over + -. Within the group they are evaluated from left to right.

In the expression, - has lowest priority and separates the group into two:

[({(3/3)*6}/2)*6] minus 1

everything else has some priority and is evaluated from left to right.
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. I understand now.
 
Something must be done about this. Let's start by reading this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic