You are on page 1of 35

COMP 1500

High Level Language (HLL) Programming


COMP 1500 Winter 2024 High Level Language Programming 2

What is Computer Programming?


• Computer Programming is simply:
• the job of writing instructions for a computer,
• using a given instruction set,
• to get a job done.
• Computer Science is NOT just Computer
programming
• It is a basic skill of Computer Scientists
• Computer Science itself is way bigger
• doing cool things
• knowing the theory behind programming
• finding clever new ways to approach problems, and
• inventing whole new ways for using computers
COMP 1500 Winter 2024 High Level Language Programming 3

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

High Level Programming


• Programming in Assembly language is tedious
• You had to micromanage every little thing
• move this memory to the ALU registers,
• do an operation
• move the result back to memory
• Even people who love Computer Science quickly find this
frustrating and annoying.
• Computer Scientists have developed abstract toolkits to
help us program better and faster
• These toolkits are called High Level Languages (HLL)
COMP 1500 Winter 2024 High Level Language Programming 7

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

High Level Language (HLL) Programming


• High level languages let you write code that looks more
like how a human sees a solution
• For example: adding a list of numbers
• Assembly language involves lots of SAVEs, GETs, STOREs, ADDs
• A high-level language just lets you add the numbers:
• 1+2+3+4+5+6
• Simple, right?
• But how do we get from this to something that will run on our CPU?
• Compiling!
COMP 1500 Winter 2024 High Level Language Programming 9

The HLL abstraction


• HLLs use the following abstractions:
• Named variables
• Natural arithmetic expressions
• Structured programming:
• Decisions (if-else)
• Counted loops
• Conditional loops
• Modular design
• Functions
COMP 1500 Winter 2024 High Level Language Programming 10

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

User Input in HLL (python)


• Using a function named input()

• Default data type is String

• Apply casting if you want to convert to any other data


type : int() / float()

• Python User Input


COMP 1500 Winter 2024 High Level Language Programming 14

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

If-else structured programming


• C1500 • HLL
C1 STORE 1,1 number = input()
C2 GETINPUT 0 if number = -1
C3 GETA 0 output 1
C4 GETB 1 else
C5 ADD output 0
C6 JUMPIFZERO C9
C7 STORE 0,2 • The intent of the
C8 JUMP C10 structured version is
C9 STORE 1,2 clear
C10 OUTPUT 2
• Python if-else structured programming
COMP 1500 Winter 2024 High Level Language Programming 17

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

The Snap Window

From the Snap Reference Manual http://snap.berkeley.edu/SnapManual.pdf


Licensed under the GNU Affero GPL https://www.gnu.org/licenses/agpl
COMP 1500 Winter 2024 High Level Language Programming 22

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

Storing input in a variable


• We can get input from the
user, and store it into
variables
• We use the ask, set and
answer blocks
• type your question into the ask
block
• drag-and-drop the answer block
onto the square in the set block
• Choose the variable from the
drop-down list in the set block
• Simple, no?
COMP 1500 Winter 2024 High Level Language Programming 26

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

The Operators palette


• What about our 'natural'
arithmetic abstraction?
• This is provided by the
Operators palette
• The first 8 blocks (the ones with
round ends) are arithmetic blocks
• they perform arithmetic on numbers
• The next 7 blocks (the ones with
pointed ends) are logical blocks
• Remember logic? (True/False)
• The last 2 blocks are text blocks
• They perform operations on text
instead of numbers
• Plus others not shown here
COMP 1500 Winter 2024 High Level Language Programming 28

Showing output

• How do we display output?


• like the C1500 OUTPUT instruction
• The say block puts text into a cartoon bubble
• In this example, we use a join block to make a string out
of text and the contents of variables
COMP 1500 Winter 2024 High Level Language Programming 29

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

Using our new block

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

You might also like