Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
How to Read Code
Benfeard Williams
June 16th, 2016
1
Concepts You Will Learn
• Programming
Skill, science, engineering, art, creativity
• Problem-solving
How to solve problems using computer programming
• Impact of computer science
Scale and automation
• Foundation for future work
2
DETAILS TO CONSIDER
3
Interpreted vs Compiled Code
• Interpreted
Read and executed by another program on the
target machine
Easy to implement
• Compiled
Expressed specifically for the target machine
Faster performance
4
Computers Do What You Tell Them
• They are fast but not smart
• You need to plan exactly what the computer
needs to do in order to solve a problem
5
Declarative Knowledge
• Statements of fact
• “A good health care plan improves the quality
of medical care while saving money”
• “y is the square root of x if and only if y*y = x”
6
Imperative Knowledge
• How to accomplish something (recipe)
1) Start with a guess, g
2) If g*g is close enough to x, then g is a good
approximation of the square root of x
3) Otherwise, create a new guess by averaging g
and x/g.
4) Using this new guess, go back to step 2
7
WHAT WILL YOU SEE IN CODE?
8
Building Blocks
• Variables
Name of a storage location
Assign a value to a variable
• Data types
Various classifications of data
• Operators
– Arithmetic: + - * / % **
• Functions (subroutines)
Sequence of instructions to perform a specific task
9
Variables
• Names are unique and can be abstract
What is http://205.251.242.103
What is http://www.amazon.com
• Declare a variable and assign a value
Name and type
Examples: int age = 18;
age = 18
10
Data Types
• Int (integer)
4, 13, -7
• Float (floating-point number)
3.33337
• Work just like normal numbers
ans = 2 * (7 + 4) – 1
ans = 21
11
Data Types
• Char (characters)
‘H’, ‘3’, ‘!’
• Str (string)
“HELLO”, “Echo 123”
• Arithmetic manipulation
phrase = “My name is ” + “Benfeard”
phrase = “My name is Benfeard”
12
Data Types
• Arrays/List
Collection of elements
[ 1, 2, 3, 4, 5]
[ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ ]
• Manipulation
myArray = [1, 2, 3]
myArray = myArray * 2
myArray = [1, 2, 3, 1, 2, 3]
• Indexing elements
13
Functions
• Allows you to easily recall a procedure or subroutine
def sum(a, b):
return a+b
• Parameters
values in the call, that you pass to the function to use as input
• Output
Return value
Call Function
answer = sum(7,3)
answer = 10
14
Functions
• Example function that returns a value
def greet(name):
print "Hello " + name
• Advantages
Repeat code, call multiple times
Flexible, call with different arguments
• Functions
greet("Sue")
15
Booleans
• True or False
• Useful for comparisons
Greater than, less than
Is equal to, is not equal to
• Supports algebraic operations
and, or, not
16
If Statements and Loops
• If statement
If this statement is true, do something
• For loops
For all values in an array, do something repeatedly
• While loops
While a statement is true, do something
repeatedly
17
UNDERSTANDING THE CODE
18
Computers do what you tell them
• They are fast but not smart
• You need to plan exactly what the computer
needs to do in order to solve a problem
• Outline the solution to your problem
Pseudocoding (step by step walkthrough of code)
19
Algorithms
• Describe in words how to solve a problem
• Like a recipe, must be detailed and precise
How to make a Peanut Butter & Jelly Sandwich
Ingredients:
Two slices of bread
Peanut Butter
Jelly
Does this provide enough
information for anyone to
make the sandwich?
1. Spread peanut butter on one slice
2. Spread jelly on the other slice
3. Combine slices together
20
How To Dissect Code
Important information about
function and usage
Opening a romeo.txt file
Counting something
Looping through lines
Splitting lines into words?
Words of length 4?
Increase counter
Print count at the end
21
Computers Read In Order
22
Important Resources
• Sakai webpage
• howtolearntocode.web.unc.edu
• HtLtC teachers
• Stackoverflow.com
• ITS Research Computing
23
QUESTIONS?
24