The goal of this lab is to investigate order of operations in different programming languages. We have four languages available to us at the moment, so we can experiment with all four. You may also be learning a fifth language at this moment. If you are advanced enough in it to try a similar experiment, you are encouraged to do so.
To get started, download and unzip the lab-exorder.zip starter files. You will find files written in Python, OCaml, C, and Java.
Execution Order
Begin with the exOrder program files. You may start with any language, and continue with another until you have gone through all four.
Study each program until you understand what it is doing. They all have a similar structure. A single-argument function takes a mutable variable as its parameter, adds one to it, and returns the value. That function is then called in two different contexts: first, as arguments to a different function. Second, as pieces of an arithmetic expression. Try to predict what the program output will be. Is it hard to predict? Why or why not?
When you feel that you understand the program, run it and look at the results. Do the results match your prediction? In what order were the single-argument function calls made? Do you find anything surprising? The languages may differ in how they treat these similar computations. If you have anyone in your group who is working in a different OS than you are, you may even find that their result differ from yours even within the same language!
When you have run all four experiments, you have a choice of actions: either repeat the experiment for your exploration language (if you can), or add a new test to each of the four existing language files that combines arithmetic and function calls in one expression. Any surprises?
Operator Precedence
The four languages we have studied don't differ greatly in their rules for operator precedence. (You can find summaries of those here.) However, there is one significant difference in ordering between comparison operators like == and bitwise boolean operators like & and |. (If you are unfamiliar with bitwise operators, they perform boolean logic operations on corresponding bits of the binary representation of two integers.)
Take a look at precedence.c and precedence.py, noting the identical expressions in each. Then run the expressions and not the result. Can you explain why this occurs? To test yourself, add a second printout to each program that uses parentheses to achieve the same result as in the other language. Experiment if need be until you get it.
As a final exercise, see if you can come up with a similar example using | (bitwise or).