Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class.

Similar presentations


Presentation on theme: "CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class."— Presentation transcript:

1 CSC 110 - Intro. to Computing Lecture 12: PALGO

2 Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class on Thursday  Covers same topics as the homework  Useful to review material before midterm

3 Programming Languages Solving problems using a computer, we usually need a program  Program could be written in machine code Series of 1s and 0s that computer understands  Difficult to read & write in machine language  Also very time consuming Programs can contain billions of instructions

4 Low-Level Languages Close to machine code, but easier to understand  Assembly language replaces 1s & 0s with mnemonics: add, sub, ldi  “C/C++” uses code that is closer to English  Still takes a lot of practice to use them But, also give programmers lots of power  Important when efficiency is critical  But introduces many potential errors

5

6 High-level Languages Examples include SQL, and spreadsheet commands  Easier to read & write than low level languages  Less powerful, but also less prone to errors Most programs written in high-level languages  Environment that is most often used for programming: Microsoft Excel

7 Palgo Programming language we will use  Developed and implemented by Dr. Meyer  Link to PALGO on class webpage  Can be run off of web or download and run on individual machine

8 Programming Experience Good news! You already have programming experience  Worked with algorithms since beginning of class  Spreadsheets and SQL queries are examples of writing computer programs  This provides you a good base for this discussion

9 Starting Palgo Web page gets filled with a grid Palgo editing window also pops up

10 Writing Your First Program Traditional first program is “Hello World”  Prints “Hello World” onto screen  Another example of wacky computer programmers In Palgo, we enter into editing window: print “Hello World” … and then hit the button labeled “Run”

11 Hello World

12 Is That All There Is? Programs can be longer than 1 command  Otherwise, Palgo would be really boring… When there are multiple lines*  Palgo 1 st executes first line of the program…  … then executes program’s 2 nd line  … and then executes the 3 rd 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

13 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”

14 Hello Friends

15 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

16 Functions You have already used a number of functions  e.g., … within truth tables  e.g., Min, Max, Sum… in spreadsheets  Write 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

17 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 message  Returns the user’s reply as the result

18 Hello Friend Text specified in input command Space for the user to enter input

19 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

20 Variables Every variable in Palgo must have a name  Names 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

21 Variables Variable is created the first time program assigns it a value  Assign variable a value using the equals sign  For example, following line creates “user”: user = input(“Tell me your name”)  Variable can have only 1 value at a time  So variable’s will store only the newest value if it is assigned a new value later in the program

22 Variables Palgo uses variable’s value whenever it executes a line using the variable  Similar to spreadsheet using cell’s value whenever it sees a cell name Palgo does NOT reassign variables’ values like a spreadsheet  Palgo ONLY changes variable’s value during assignments  Palgo executes programs line-by-line* and will not automatically propagate changes

23 Hello Friend Can use print to say “Hello” to the user  So our final program is: name = input(“Tell me your name”) print “Hello ” + name

24 Hello Friend “Prof. Hertz” is here since that is value of user

25 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

26 Other Palgo Data Types “ One ”, “ 1 ”, 1, and 1.0 are not equal  1.0 is a real  reals 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

27 Palgo Operations Palgo does variety of mathematical functions  +, -, *, / will add, subtract, multiply, or divide two numbers  Does not matter whether or not the numbers are stored in a variable

28 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

29 Program Trace 1 x = 4 + 2 2 y = 8 * 1 3 z = y – 3 4 x = x + 1 5 z = y - x 6 y = y / 2 7 z = “x” 8 y = z + “b” Line#xyz

30 Program Trace 1 x = 4 + 2 2 y = 8 * 1 3 z = y – 3 4 x = x + 1 5 z = y - x 6 y = y / 2 7 z = “x” 8 y = z + “b” Line#xyz 16 xyz 2 xyz 28 3 xyz 28 35 4 xyz 7 5 xyz 1 6 xyz 64 7 xyz x 8 xyz xb

31 Hello Friends What if one or two people are using the computer?  Want to say hello to either the single user or both users  To do this we first need to ask how many people are at the computer num = input_number("Are there 1 or 2?")  input_number is like input, but returns a number

32 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)

33 Palgo Operators Some operators compute boolean values  == compares if two elements are equal  != compares if two elements are NOT equal  >, =, & <= do the obvious comparisons  a && b returns true if a AND b are true  a || b returns true if either a OR b are true

34 if---then--else num = input_number("Are there 1 or 2?") if num == 1 then user = input("What is your name?") print "Hello " + user else user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + user + " & " + user2 end Palgo checks if value num variable is equal to 1

35 if---then--else num = input_number("Are there 1 or 2?") if num == 1 then user = input("What is your name?") print "Hello " + name else user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2 end If in this execution the value of num is 1, Palgo will run these statements

36 if---then--else num = input_number("Are there 1 or 2?") if num == 1 then user = input("What is your name?") print "Hello " + name else user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2 end If the value of num is not 1, Palgo runs these statements

37 if---then--else num = input_number("Are there 1 or 2?") if num == 1 then user = input("What is your name?") print "Hello " + name else user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2 end The end keyword marks where Palgo should again execute the program as normal

38 Hello Friends

39

40 What happened?

41 For Next Lecture Finish reading about Palgo Study for Quiz #3 Play around with Palgo  Run the demo programs  Try writing something on your own  Paint a picture for someone special


Download ppt "CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class."

Similar presentations


Ads by Google