Evaluate Equal Precedence from Left to Right

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

24 / 2  - 8
------
  12    - 8
  ---------
       4

Evaluate Equal Precedence from Left to Right

When there are two (or more) operators of equal precedence, the expression is evaluated from left to right. Examples:


2 * 7 * 3 
-----
  14  * 3
  -------
     42

4 - 2 + 5
-----
  2   + 5
  -------
      7

Usually it doesn't matter if evaluation is done left to right or in any other order. In algebra it makes no difference; But with floating point math it sometimes makes an important difference.

QUESTION 24:

What is the value of the following expression?

2 + 4/2 + 1