320 likes | 441 Views
Chapter 1 The Way of the Program. Dr. Paige H. Meeker. Definition. What is computer science all about?. A computer scientist is…. A little bit of: Mathematician Engineer Natural Scientist Teacher Educator But most of all – A PROBLEM SOLVER. Problem Solving / Programming.
E N D
Chapter 1The Way of the Program Dr. Paige H. Meeker
Definition What is computer science all about?
A computer scientist is… A little bit of: • Mathematician • Engineer • Natural Scientist • Teacher • Educator But most of all – A PROBLEM SOLVER
Problem Solving / Programming • Computer scientist give computers lists of instructions (aka “programs”) in order to solve problems. • This semester, we will use the Python programming language to give instructions to our own programmable robot. • But first…
Programming Languages • Low level • High level
High Level Languages Advantages: • Easier to program • Faster to create • Shorter and easier to read • More likely to be correct • Portable
The Translation Process • Compiler • Interpreter • Some use both – Python uses both, but because of the way users interact with it, is considered more of an interpreted language
Python Interpreter • How do you use it? • Shell mode • Script mode
Shell Mode • Python commands typed into the shell are immediately interpreted and executed. • Instructions are typed at the Python prompt, which looks like: >>> • Once the shell session is terminated, all instructions given during that session are lost.
Script Mode • A program in a file • Name ends with “ .py” (perhaps we call it “firstprogram.py” • The file (or script) can then be run inside the interpreter’s shell with the command: >>>python firstprogram.py • Once the interpreter’s shell session is terminated, all instructions given during that session are lost, but all instructions held within a file are saved for use at a later time.
So… Now What? • We can give the computer (and/or the robot) a set of instructions which tell it what we wish it to do. This is called “Programming”
Computer Programming • What is computer programming? • Process of writing, testing, debugging, and maintaining the source code of computer programs.
Computer Programming • Why do we do it? • To create a program that performs a desired behavior. • There is some debate as to whether programming is an art, a craft, or an engineering discipline.
Computer Programming • Programmers work to create code that is efficient (works without draining current resources) and evolvable (can become something bigger in the future) • Programmers are generally given a list of requirements and then work to translate those requirements into something the computer can understand.
Computer Programming • Programming is often compared to following a recipe. • Programming is about giving the computer step by step instructions to go about completing the task. • The instructions generally take the form of: • a direct statement • a conditional statement • a link to a group of statements • a loop around statements meant to be repeated for some amount of time.
Pizza Dough Recipe (Requirements) 1. Gather Ingredients 2. Combine sugar (1tbs), salt (1tbs), olive oil (1tbs), flour (1c) in mixing bowl 3. Turn on mixer 4. Add 1/4 cup of flour 5. If dough comes off the sides go to step 6, otherwise go back to step 4 6. Knead 15 minutes 7. Let rest for at least 45 minutes in warm area • 1 dough ball
N-Pizza Dough Program • Gather Ingredients • Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N c) in mixing bowl • Turn on mixer • Add N/4 cup of flour • If dough comes off the sides go to step 6, otherwise go back to step 4 • Knead 15 minutes • Let rest for at least 45 minutes in warm area • N dough balls
N-Pizza Dough Program • Gather Ingredients • Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N c) in mixing bowl • Turn on mixer • Add N/4 cup of flour • If dough comes off the sides go to step 6, otherwise go back to step 4 • Knead 15 minutes • Let rest for at least 45 minutes in warm area Sequence of Statements • N dough balls
N-Pizza Dough Program Variable • Gather Ingredients • Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N c) in mixing bowl • Turn on mixer • Add N/4 cup of flour • If dough comes off the sides go to step 6, otherwise go back to step 4 • Knead 15 minutes • Let rest for at least 45 minutes in warm area • N dough balls
N-Pizza Dough - Program • Gather Ingredients • Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N c) in mixing bowl • Turn on mixer • Add N/4 cup of flour • If dough comes off the sides go to step 6, otherwise go back to step 4 • Knead 15 minutes • Let rest for at least 45 minutes in warm area Conditional
N-Pizza Dough - Program • Gather Ingredients • Combine sugar (N tbs), salt (N tbs), olive oil (N tbs), flour (N c) in mixing bowl • Turn on mixer • Add N/4 cup of flour • If dough comes off the sides go to step 6, otherwise go back to step 4 • Knead 15 minutes • Let rest for at least 45 minutes in warm area Subroutines Mini-programs • N dough balls
Programming Languages • There are hundreds of different programming languages. • A few basic types of instructions appear in all of them • Input (get input from the user) • Output (display data to the user on a monitor or printout) • Math (perform basic mathematical operations) • Conditional Execution (check for conditions and execute the appropriate sequence of statements) • Repetition (perform some sequences of statements repeatedly) • Not too bad, right?
Programming Benefits • What do students learn through programming? • Mathematical and computational ideas • Coordinates, Variables, Random Numbers • Problem solving skills • Process of Design • Idea->Prototype->Experiments->Debugging->Redesign->New Ideas • Fluency with Digital Technology • To be fluent in language, you learn to read AND write – how to express yourself in the language. The same is true of programming – using the computer not just to interact but to create.
Debugging • Nobody’s perfect – especially not the first time! • Debugging is the process of finding errors in the program and correcting them. • The term was first used by Grace Hopper, when she found a “bug” in the computer in the 1950’s (see photo) • Debugging is a vital programming skill 1st use of “computer bug”
Types of Bugs • Syntax • Runtime • Semantic
Syntax Errors • Programs must have correct syntax in order to run. • Syntax refers to the structure of a program and the rules about that structure. • Just one syntax error caused your program to quit. • Be prepared for this to happen quite frequently at first!
Runtime Errors • An error that doesn’t appear until a program is run. • Aka “Exceptions”
Semantic Errors • Really difficult to find • Program runs to completion, but produces an incorrect result • Must find them by looking at what the code is doing, not what you think its doing.
Languages • Formal • Natural
Similarities between Natural and Formal Languages • Both have rules of syntax • Both can be parsed
Differences between Formal and Natural Languages • Ambiguity • Redundancy • Literalness
The First Program • “Hello World” • >>>print "Hello, World!" • This is an example of a print statement, which doesn’t actually print anything on paper. It displays a value on the screen. • The quotation marks in the program mark the beginning and end of the value; they don’t appear in the result.