Nested Parentheses

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:

Expression 8 + 2 / 2 + 3 (8 + 2) / (2 + 3) (8 + 2) / 2 + 3 8 + 2 / (2 + 3)
Value? 12 2 8 8

The last answer is correct. It is done like this:

8 + 2 / (2 + 3 )
        --------
8 + 2 / 5
    -----
8 + 0
-----
8

2/5 is 0 with integer division.

Nested Parentheses

Sometimes in a complicated expression one set of parentheses is not enough. In that case use several nested sets to show what you want. The rule is:

The innermost set of parentheses is evaluated first.

Start evaluating at the most deeply nested set of parentheses, and then work outward until there are no parentheses left. If there are several sets of parentheses at the same level, evaluate them left to right. For example:

( ( ( 32 - 16 ) / ( 2 * 2 ) ) -  ( 4 - 8 ) ) + 7
    -----------

( (     16      / ( 2 * 2 ) ) -  ( 4 - 8 ) ) + 7
                  ---------

( (     16      /     4     ) -  ( 4 - 8 )  ) + 7
  ---------------------------

(               4             -  ( 4 - 8 )  ) + 7
                                  -------

(               4             -      -4     ) + 7
---------------------------------------------
                    
                     8                        + 7
                     ----------------------------

                                  15

Ordinarily you would not do this in such detail.

QUESTION 28:

What is the value of this expression:

(12 / 4 ) + ( 12 / 3)