Professional Documents
Culture Documents
Code.org
• Code.org is an organization dedicated to encouraging
students to learn computer science
• It sponsors an annual event called Hour of Code
• https://www.youtube.com/watch?v=FC5FbmsH4fw
• Participants write short programs in a HLL called Blockly
• It is a visual programming language
• You write programs by linking together blocks
• Minecraft is an Hour of Code programming project
• Watch the tutorial on using Blockly
• https://studio.code.org/s/mc/reset
• Some more practice with Angry Bird and Evil Pig:
• https://studio.code.org/s/20-hour
COMP 1500 Winter 2024 High Level Language Programming 4
The Artist
• These problems are more free-form and less
constrained than the maze problems.
• Also, note how each puzzle has a maximum number of
blocks listed on the purple bar at the top.
• Completing the puzzle efficiently can be challenging!
• https://studio.code.org/s/20-hour/stage/5/puzzle/1
• https://studio.code.org/s/20-hour/stage/7/puzzle/1
COMP 1500 Winter 2024 High Level Language Programming 5
More abstraction
• We have started at the very lowest level
• bits, bytes, Boolean operations, transistors and circuits
• As we move to higher levels, we hide the details
• using abstractions
• gates, ALU, CPU, Control Unit, Assembly language
• Computer programming also gets complex
• Programs can contain millions of lines of code!
• Can we apply abstraction to programming?
• Of course!
COMP 1500 Winter 2024 High Level Language Programming 6
Compilation
• Remember the • For example, the HLL
assembler? program: output 5+2
• Compiles to:
• It translates Assembly
language into machine C1 STORE 5, 0
code C2 STORE 2, 1
• Now we need an app C3 GETA 0
to translate our high- C4 GETB 1
level program into C5 ADD
Assembly language C6 SAVE 2
• This app is called a C7 OUTPUT 2
compiler
COMP 1500 Winter 2024 High Level Language Programming 8
Named variables
• Referring to memory by address is tedious
• We must remember which memory address is being used for what
purpose
• We usually make a 'table of contents' so we can remember what
each address is used for
• A HLL lets us refer to memory by a name instead
• e.g. in C1500 …
STORE 0,0 // set the total to zero
• and in a HLL …
total = 0
• Python Variable
COMP 1500 Winter 2024 High Level Language Programming 11
Data types
Reference
COMP 1500 Winter 2024 High Level Language Programming 12
Natural arithmetic
• Arithmetic in assembly language involves lots of steps
• GETA, GETB, ADD, SAVE for one addition
• HLLs let us write arithmetic that is pretty close to what we
use in algebra
• e.g. a quadratic equation: y = ax2 + bx + c
y = a*x*x + b*x + c
• In many HLLs, multiplication is denoted by *
• How many instructions would be needed in C1500?
• Python arithmetic expression
COMP 1500 Winter 2024 High Level Language Programming 13
Structured programming
• What is structured programming?
Structured programming is a programming paradigm
aimed at improving the clarity, quality, and
development time of a computer program by
making extensive use of subroutines, block
structures, for and while loops—in contrast to using
simple tests and jumps such as the go to [JUMP]
statement, which could lead to "spaghetti code" that
is difficult to follow and maintain.
• https://en.wikipedia.org/wiki/Structured_programming
COMP 1500 Winter 2024 High Level Language Programming 15
Spaghetti code
• What does that mean?
• It is instructions that have a complex logical flow because it
jumps from place to place
• Much like what we have seen in C1500 programs
• It makes for programs that are hard to understand and even harder
to modify
• Structured programming makes the logic of the program
clear
COMP 1500 Winter 2024 High Level Language Programming 16
Loops
• Repetition is common in programs
• For example, processing a list of items …
• or reading input from the user
• Loops allow instructions to be repeated
without having to copy them
• We use the same instructions to process a list of one million items
• We can also perform a loop until some logical condition is
met
• Get input until a value of -1 is entered
• Python For Loops
• Python While Loops
COMP 1500 Winter 2024 High Level Language Programming 18
Loops
total = 0 total = 0
count = 0 count = 0
for each item in repeat
list
item = input()
add item to total
add item to total
add 1 to count
end for add 1 to count
until item = 0
average = total /
count average = total /
count
output average
output average
COMP 1500 Winter 2024 High Level Language Programming 19
Functions
• Functions allow programmers to divide a large program
into small pieces
• It permits modular design
• Really another type of abstraction
• It allows a programmer to focus on what a function does, rather
than the details of how it does it
• For example, the previous slides used input()
• We know that it gets input from the user, but not how
• It may be from the keyboard …
• or a spoken command (i.e. Siri/Alexa/Cortana) …
• or something else …
• Python Functions
COMP 1500 Winter 2024 High Level Language Programming 20
Snap
• Snap is a visual, drag-and-drop programming language
• http://snap.berkeley.edu/index.html
• It runs in a web browser, so no software necessary
• It has all the HLL abstractions we just discussed
• It has lots of support for animation
• Sprites can interact on a stage
• But we can do arithmetic too
COMP 1500 Winter 2024 High Level Language Programming 21
Scripts
• A Snap program consists of
scripts
• A script consists of blocks
chosen from the Palette
• the blocks 'snap' together
• Scripts are written for
sprites
• In the example here:
• The top block is called a Hat
block
• These control when a script is
performed
• It will cause the sprite to draw
a square
COMP 1500 Winter 2024 High Level Language Programming 23
Variables
• The first HLL abstraction
• The Variables palette lets you
create variables
• for all sprites, or individual ones
• In this example, two variables
have been created
• If the checkbox to the left of the
variable is checked, the variable
name and its value will appear in
the stage
• Use the set block to set a
variable's value
• Use change to change the
value
COMP 1500 Winter 2024 High Level Language Programming 24
Getting input
• Can we get input from
the user?
• like C1500 GETINPUT
• The Sensing palette has
blocks for getting input
• The ask block asks for
some keyboard input
• When the user types
something and hits
Return, the input is
stored in the answer
block
COMP 1500 Winter 2024 High Level Language Programming 25
Hat blocks
• On the Control palette
• Sprites may have many
scripts
• Each one will be
executed whenever
some event occurs
• clicking the flag icon
• on a keystroke or mouse
action
• some logical condition
• receiving a message
COMP 1500 Winter 2024 High Level Language Programming 27
Showing output
Choices
• There are 2 blocks
under the Control
palette for making
choices
• If block
• If-Else block
• These blocks require a
logical block
• If the logical block is
True, do the if part
COMP 1500 Winter 2024 High Level Language Programming 30
Loops
• Snap has 3 types of loop
blocks
• Forever
• Repeat
• Repeat until
• Forever loops … forever
• Repeat loops a specific
number of times
• Repeat until requires a
logical block
• The loop keeps repeating
until the logical block is True
COMP 1500 Winter 2024 High Level Language Programming 31
Functions
• In Snap, you create a
function by making a block
• A block contains a script
• The blocks that make up the
script are encapsulated in the
new block
• Click the Make a block
button in the palette
• A Command block does not
return any result
• A Reporter block returns a
result
• A Predicate block returns a
Boolean result
COMP 1500 Winter 2024 High Level Language Programming 32
A Reporter block
• Here is our get-input
function in Snap
• It gets input from the
user and reports
(returns) it.
• The block itself requires
an input
• prompt: a message to
the user
• It is displayed to prompt
the user to enter some
appropriate response
COMP 1500 Winter 2024 High Level Language Programming 33
• We can now use our new block in place of the two blocks we
used before
• Good use of functions makes a program simpler and easy to
understand
COMP 1500 Winter 2024 High Level Language Programming 34
A command block
• Here is a block called
square
• It accepts an input
called size
• It draws a square
using size for the
length of a side
COMP 1500 Winter 2024 High Level Language Programming 35
Summary
• We did our very first computer programming.
• We were given a computer language and used it to solve
problems.
• The language provides tools that free us from the details of
assembly language.
• This challenge, of turning a set of basic instructions into a
desired output, is the essence of computer programming.
• Hopefully, by the end of the unit your brain power was
no longer being used to understand the language.
• Instead, you focus on the problem solving.