Expressions in a Programming Language


Overview

Expressions in programming languages such as Cinnameg and Java are quite similar to expressions in mathematics. But there are a few important differences. For example, you write * to mean multiplication. This chapter explains what expressions look like. Everything in this chapter is the same for Cinnameg and Java.

To make it clear that certain things are written in a programming language, we show them in bold brown.

Occasionally, we show an something that is in programming language notation but that is not allowed for some reason. In that case, we show it in magenta.


Numbers

Write numbers in familiar notation, such as 100 and 15.3. But do not use commas in numbers. One million is written 1000000, without any commas. Also, do not put any spaces inside a number.

If you have a really large or small number, you can use scientific notation to write it, where you multiply by a power of 10. The notation is a little strange, though. You write the number followed by E followed by the exponent of 10, without any spaces. For example, 3.0E20 is the number that is written in scientific notation as 3.0×1020 and 5.1E-8 is the number 5.1×10-8.


Multiplication and division

You have probably already seen three different notations for indicating multiplication. When you first learned to multiply, you might have used 10 × 4 to indicate multiplication of 10 and 4. Later, you might have used notation 10·4 or (10)(4) for the same thing.

Most programming languages use symbol * to indicate multiplication. So expression 10*4 has value 40. It is important to remember the *. In a programming language, (10)(4) is not correct, and does not mean anything.

To square a number, just multiply it by itself. So x2 is written x*x.

Divide using operator /. Expression 12/4 has value 3. (Be sure to use /, not \. You will probably find / on the same key as a question mark.)

Use sqrt(x) for the square root of x, and abs(x) for the absolute value of x.


Negation

You can write a minus sign in front of a number or expression to negate it. For example, expression -(4+5) has value -9.


Precedence and associativity

For the most part, precedence and associativity rules for +, -, * and / are the usual ones; + and - have low precedence, with * and / having higher precedence. Negation has the highest precedence, even higher than multiplication. Operators of the same precedence are done from left to right.

But there is one important difference. In both Cinnameg and Java, operators * and / have the same precedence. Multiplications and divisions are done from left to right. So expression 12/2*3 is computed as follows.

  12/2*3
    = 6*3
    = 18
Notice that the division is done first, because, in those programming languages, division and multiplication have the same precedence. If you wanted the multiplication to be done first, you would write 12/(2*3).


Parentheses

Use parentheses to force an expression to be evaluated in a particular order. But only use the round kind of parentheses (...), not the square ones [...] or curly ones {...}. Square brackets and curly braces have a different meaning.


Substitution rule

Most programming languages (including Cinnameg and Java) follow the rule that you can build up a larger expression by replacing a variable by an expression. For example, 2*(x + 8) is a Cinnameg expression. So is 3*y+ 1. Replacing x by (3*y+ 1) in 2*(x + 8) yields a new Cinnameg expression 2*((3*y+ 1) + 8).


Testing expressions

You can test the value of an expression by clicking on the Test link at the top of each page. Select the language and select Expression. Type your expression into the box. Then push the Compute button to get the answer.

If you have made a mistake, what you will see instead of the answer is an indication of your mistake. It is not always clear what the mistake is. If you do not understand what is being said, try examining your expression for errors. If you cannot find the error, it is a good idea to ask for help. If someone is available, just ask.

If you are working by yourself, you might want to ask a question by email. If you do, always include with your question the exact expression that you typed. Do not type it again, since you might mistype it. Copy it and paste it. If you send an email that just says "I do not understand why my expression does not work" without showing what the expression is, there is not much that anyone can say to help.


Problems

  1. [solve] Write the number ten thousand and 1 in programming language notation.

  2. [solve] Convert mathematical expression 3(9+2) to programming language notation.

  3. [solve] Convert mathematical expression 82 to programming language notation.

  4. [solve] Convert mathematical expression (4+5)(6+(8-1)2) to programming language notation.

  5. [solve] Convert mathematical expression 100/52 to programming language notation.

  6. [solve] What is the value of expression 10-15?

  7. [solve] What is the value of expression -2 + 3?

  8. [solve] What is the value of expression 10.5/2.0?


Summary

Expressions in Cinnameg and Java look very similar to ordinary mathematical expressions. But remember a few differences.

  1. Use * to indicate multiplication.
  2. * and / have the same precedence.
  3. Never write a comma or space in a number
  4. Use parentheses to group things in an expression, but not square brackets or curly braces.

If you encounter mistakes that you cannot understand, ask questions.


Review

Evaluate expressions step-by-step from smaller to larger subexpressions.

Be sure to follow precedence and associativity rules.