← Back to course
Intro to CS 9-10 / Week 02 / Thursday
4/6
Week 02 · Algorithms in Pseudocode

Thursday

Expressions, inputs and outputs
// Exact steps, variables and expressions
⏱ about 20 min

Thursday: Expressions, Inputs and Outputs

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."

Expressions

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
EVALUATE THE EXPRESSIONS
  • Read the question.
  • Tap your answer.
What value does cometAverage get?
What value does wrenAverage get?
Which line gives the true average Tomas asked for?
lightLevel gets 10 plus 4 times 5. What value is that?

Same task, different algorithms

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.

VersionPseudocodeValue
Aaverage ← (morningTemp + eveningTemp) / 225
Baverage ← morningTemp / 2 + eveningTemp / 225
Caverage ← morningTemp + eveningTemp / 235
COMPARE THE VERSIONS
  • Read the question.
  • Tap your answer.
Versions A and B are written differently. Do they accomplish the same task?
Versions A and C look almost the same. Do they give the same result?

Inputs and outputs in pseudocode

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)
ONE READING, FROM SENSOR TO SCREEN
  • Tap a card.
  • Then tap its spot.
1First
2Second
3Third
4Last
EVALUATE (MORNINGTEMP + EVENINGTEMP) / 2, STEP BY STEP
  • ?Work out the parentheses: 20 plus 30 is 50.
  • ?Store 25 in the variable on the left of the arrow.
  • ?Look up morningTemp: 20.
  • ?Divide: 50 divided by 2 is 25.
  • ?Look up eveningTemp: 30.
WHY THIS EXERCISEEvaluating step by step, in the set order, is how a computer turns an expression into one value.
StatementTrue 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.?
WHY THIS EXERCISEOrder of operations and clear inputs and outputs are where many small bugs hide.
A value, variable or operator that evaluates to one value is an ____.
In Beacon's pseudocode, the word that shows a value on the screen is ____.
On paper, draw an input arrow going into a box labeled Beacon and an output arrow coming out. Write one example on each arrow.

Excellent work. Tomorrow is Impact Friday: what to do when code comes from someone else.

← Wednesday