Arithmetic Expressions Note 2019
Arithmetic Expressions Note 2019
A literal is a value (number, letter, character, string, etc.) typed directly into a program.
Question
What two operations are taking place in each of the above statements? _declaration__ and _assignment_.
Operators
An operator is a special symbol for performing a function that combines values and/or expressions.
An operand is one of the things that gets combined (i.e. the values or expressions being operated on).
5 * 4 Operator(s): _________ Operand(s): _________
0.7 / 4.5 Operator(s): _________ Operand(s): _________
var a = 27 / 5 * 3 – 45 / 2; var b = 90 + 45 % 4 – 15 / 2;
What value gets assigned to a? _____ What value gets assigned to b? _____
Integer and floating-point operands can be mixed, resulting in a floating-point (decimal) value.
Practice – Evaluating Arithmetic Expressions (try them yourself, check answers in a program)
Expression Result (Value) Type (integer or floating point)
4.5 * 2 – 5 = 9 - 5 4 integer
4.5 * (2 – 5)
5 / 4 + 9.0
0 * 15 / 2
10 % 4 + 4
10 % 1.5 + 4
ICS2O Page 2
Concatenation (String Addition)
The + operator has dual meaning. Depending on how and where it is used, it can be used to perform numeric
(mathematical) addition or string addition (i.e. concatenation). You can “add” strings with numbers (integers or
floating-point) using the + operator. This will concatenate them (add them together as strings). If at least one of
the operands is a string, the result will be a string as well. Be careful when performing concatenation, as some
unexpected results can occur.
Example Can you explain what is happening here and why?
text(“23 + 7 = ” + 23 + 7, 10, 10); vs. text(23 + 7 + “ = 23 + 7”, 10, 10);
Output: 23 + 7 = 237 30 = 23 + 7
More Examples (“_” means a space): use b (a ‘b’ with a forward slash through it) to represent spaces
random(low, high): generates a random number between low (inclusive) and high (exclusive)
random(1, 100) = a random number between 1 and 99.9999...
floor(random(1, 7)) = a random integer between 1 and 6 (like a die roll)
pow(base, exponent): calculates a power, the base raised to the exponent (baseexponent)
pow(2, 3) = _____ pow(7, 2) = _____
sqrt(num): calculates the positive square root of any non-negative number (to 3 decimal places)
sqrt(25) = _______ sqrt(35) = _______ sqrt(-1) = _____
min(num1, num2) and max(num1, num2): determines the minimum or maximum of two values
THINK: How can we find the max. or min. of more than 2 values?
There are also functions for the three primary trigonometric functions:
sin(value) cos(value) tan(value)
There are many other useful functions built into the Processing.JS language:
Get the current date with: day(), month(), year()
day() for the day of the month, month() for the number of the current month (1 to 12), year() for the year
Get the current time with: hour(), minute(), second()
Get the time (in milliseconds) that has elapsed since the program started with: millis()
Variables in Expressions
Anywhere you can put a literal, you can also put a variable.
var x = d2 * i + i;
What value is stored in variable x after these statements? x = ______
x = x + 1;
→ Refer to file/handout “Analysis & Design of a Program.doc” for more detailed info and instructions.
2. Rectangle Calculator: Write a program that asks the user for a length and width in pixels, then draws the
rectangle, and outputs the perimeter (in pixels) and area (in pixels squared). Name your program:
<LastName>RectangleCalculator
<clear screen>
Your rectangle has a perimeter of 180 pixels and an area of 1904 pixels squared.
3. Hollow Square Calculator: Write a program that asks the user for a radius in pixels,
then uses the given radius to draw an image with a square and a circle like the one on the right, r
and then calculates and outputs the area of the shaded portion rounded to the nearest square
pixel. Name your program: <LastName>HollowSquareCalc
2r
Hollow Square Calculator!!! Example
output…
Enter the radius in pixels: 200
<clear screen>