Upload
meredith-washington
View
213
Download
0
Embed Size (px)
Citation preview
CSC 110 -Intro. to Computing
Lecture 12:
PALGO
Announcements
Homework #3 solutions availableDownload from Blackboard/web
Quiz #3 will be in class on ThursdayCovers same topics as the homeworkUseful to review material before midterm
Programming Languages
Solving problems using a computer, we usually need a programProgram could be written in machine code
Series of 1s and 0s that computer understands
Difficult to read & write in machine languageAlso very time consuming
Programs can contain billions of instructions
Low-Level Languages
Close to machine code, but easier to understandAssembly language replaces 1s & 0s with
mnemonics: add, sub, ldi“C/C++” uses code that is closer to EnglishStill takes a lot of practice to use them
But, also give programmers lots of power Important when efficiency is criticalBut introduces many potential errors
High-level Languages
Examples include SQL, and spreadsheet commandsEasier to read & write than low level languagesLess powerful, but also less prone to errors
Most programs written in high-level languagesEnvironment that is most often used for
programming: Microsoft Excel
Palgo
Programming language we will useDeveloped and implemented by Dr. MeyerLink to PALGO on class webpageCan be run off of web or download and run on
individual machine
Programming Experience
Good news! You already have programming experienceWorked with algorithms since beginning of
classSpreadsheets and SQL queries are examples
of writing computer programsThis provides you a good base for this
discussion
Starting Palgo Web page gets filled with a grid Palgo editing window also pops up
Writing Your First Program
Traditional first program is “Hello World”Prints “Hello World” onto screenAnother example of wacky computer
programmers In Palgo, we enter into editing window:
print “Hello World”
… and then hit the button labeled “Run”
Hello World
Is That All There Is?
Programs can be longer than 1 command Otherwise, Palgo would be really boring…
When there are multiple lines* Palgo 1st executes first line of the program… … then executes program’s 2nd line … and then executes the 3rd line … and continues to execute line-by-line until it
reaches the end of the program
* While true for simple programs, later we will discuss how to make Palgo do more
Hello Friends
Suppose you only want to say hello to specific people (You really should get out more and be more
friendly, but…) In Palgo, we enter into editing window:
print “Hello Ishkibble”print “Hello Nevada”
… and again hit “Run”
Hello Friends
Hello Friend
Decide to get out more and say hello to the user(Now need to meet a nice girl, settle down,
and… sorry, channeling my mother) First, need to get user’s name
To do this we will need to use the input function defined by Palgo
Functions
You have already used a number of functions e.g., … within truth tablese.g., Min, Max, Sum… in spreadsheetsWrite function name, a left parenthesis the
function inputs, and a right parenthesis:Min(A2, A3, A4) or Average(A4:A9)
When evaluated, we replace them with their actual value
( ),( )a b c d
input()
Trying to say hello to the user First need to get their name Do this by using Palgo’s input function:
input(“Tell me your name”)
When Palgo executes function:Prints the messageReturns the user’s reply as the result
Hello Friend
Text specified in input commandSpace for the user to enter input
Variables
Can use input to get user’s name, but how can we use this? We need to save this value for later use To do this we will use a variable
Getting user’s name and storing it in a variable: user = input(“Tell me your name”)
After executing this line, whatever the user types will be stored in a variable named user
Variables
Every variable in Palgo must have a nameNames start with a letter and then have any
letters, numbers, or underscores (“_”) Example variable names:
user, name, Foo, Bar, foo_bar, jamesBond007, peer2peer, r, thisIsALongNameContaining34Letters
Variables
Variable is created the first time program assigns it a valueAssign variable a value using the equals signFor example, following line creates “user”:
user = input(“Tell me your name”)Variable can have only 1 value at a timeSo variable’s will store only the newest value if
it is assigned a new value later in the program
Variables Palgo uses variable’s value whenever it executes a
line using the variableSimilar to spreadsheet using cell’s value whenever it sees
a cell name Palgo does NOT reassign variables’ values like a
spreadsheetPalgo ONLY changes variable’s value during assignmentsPalgo executes programs line-by-line* and will not
automatically propagate changes
Hello Friend
Can use print to say “Hello” to the userSo our final program is:name = input(“Tell me your name”)print “Hello ” + name
Hello Friend
“Prof. Hertz” is here since that is value of user
Data Type
All data objects define the type of data “One”, “1”, 1, and 1.0 are not equal
“One”, “1” are Strings Strings contain anything inside quotes
“Bob is my #1 uncle” “Prof. Hertz is the greatest teacher EVER!”
1 is an int ints are positive and negative whole numbers
0, -1, 1, 64, -483291, 234897432 are all ints
Other Palgo Data Types
“One”, “1”, 1, and 1.0 are not equal1.0 is a realreals include numbers with a decimal
1.2, 456.787, -19321732.131321, 0.00000000001
Whenever we reassign a variable, we will also reassign its data type
It is not always easy to convert between the different data types
Palgo Operations
Palgo does variety of mathematical functions+, -, *, / will add, subtract, multiply, or
divide two numbersDoes not matter whether or not the
numbers are stored in a variable
Program Trace
Trace programs to see what it will do or why it did something Important for understanding new code, fixing
non-working code, and getting good exam grades
Shows how Palgo executes program step-by-step
Shows the line being executed, the value assigned to any variable, and other important information
Program Trace
1 x = 4 + 22 y = 8 * 13 z = y – 34 x = x + 15 z = y - x6 y = y / 27 z = “x”8 y = z + “b”
Line# x y z
Program Trace
1 x = 4 + 22 y = 8 * 13 z = y – 34 x = x + 15 z = y - x6 y = y / 27 z = “x”8 y = z + “b”
Line# x y z
1 6
Line# x y z
2
Line# x y z
2 8
3
Line# x y z
2 8
3 5
4
Line# x y z
7
5
Line# x y z
1
6
Line# x y z
6 4
7
Line# x y z
x
8
Line# x y z
xb
Hello Friends
What if one or two people are using the computer?Want to say hello to either the single user or both
usersTo do this we first need to ask how many people
are at the computernum = input_number("Are there 1 or 2?")
input_number is like input, but returns a number
if---then---else---end
num = input_number("Are there 1 or 2?")
Program differs depending on num. users We use if—then—else—end to do this
if—then tests boolean (true or false) expression If true, Palgo executes code between then and else
Otherwise, Palgo executes code between else and end
Palgo will then continue execution after end (Part of how Palgo does not always execute line-
by-line)
Palgo Operators
Some operators compute boolean values== compares if two elements are equal != compares if two elements are NOT equal>, <, >=, & <= do the obvious comparisonsa && b returns true if a AND b are truea || b returns true if either a OR b are true
if---then--else
num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + userelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + user + " & " + user2end
Palgo checks if value num variable is equal to 1
if---then--else
num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end
If in this execution the value of num is 1, Palgo will run these statements
if---then--else
num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end
If the value of num is not 1, Palgo runs these statements
if---then--else
num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end The end keyword marks where
Palgo should again execute the program as normal
Hello Friends
Hello Friends
Hello Friends
What happened?
For Next Lecture
Finish reading about Palgo Study for Quiz #3 Play around with Palgo
Run the demo programsTry writing something on your ownPaint a picture for someone special