Tomas asks for one new number on Plant Status: the average of the morning and evening temperature readings.
Comet writes a line in seconds. "Morning is 20, evening is 30. Beacon says the average is 35!"
Wren raises an eyebrow. "What do you notice? Can an average be bigger than both readings?"
Comet stares at the page. "Okay. That is not possible."
Nova circles the line, her lights blinking slowly. "Would you like a hint?" she asks.
"Look at which part of the expression the computer works out first."
An expression can be a value, a variable, an operator, or a procedure call that returns a value.
An expression is evaluated to produce a single value.
Evaluation follows a set order of operations defined by the programming language.
In this course, the order matches math class: parentheses first, then multiply and divide, then add and subtract.
morningTemp ← 20 eveningTemp ← 30 cometAverage ← morningTemp + eveningTemp / 2 wrenAverage ← (morningTemp + eveningTemp) / 2
Algorithms can be written in different ways and still accomplish the same task.
Algorithms that look similar can still give different results, like Comet's line and Wren's line.
Clarity and readability matter. A line that is easy to read is easier to check.
| Version | Pseudocode | Value |
|---|---|---|
| A | average ← (morningTemp + eveningTemp) / 2 | 25 |
| B | average ← morningTemp / 2 + eveningTemp / 2 | 25 |
| C | average ← morningTemp + eveningTemp / 2 | 35 |
Inputs are data sent to a program. Outputs are data a program sends to a device.
In Beacon's pseudocode, INPUT() brings in the next reading from a sensor.
DISPLAY shows a value or a message on Beacon's screen. That is an output.
waterLevel ← INPUT()
DISPLAY("Row 4 water level:")
DISPLAY(waterLevel) | Statement | True or false? |
|---|---|
| An expression is evaluated to produce a single value. | ? |
| Parentheses change which part of an expression is worked out first. | ? |
| Two algorithms that look similar always give the same result. | ? |
| DISPLAY is an input. | ? |
Excellent work. Tomorrow is Impact Friday: what to do when code comes from someone else.