Nate Brunelle Today: Pseudo-Code CS1110 Nate Brunelle Today: Pseudo-Code
Questions?
Ambiguity Put the pot in the dishwasher When we talk to computer, we give it some instruction How to wash your hair Lather Rinse repeat
Hair washing ambiguity Don’t know when to stop What am I rinsing? Where am I lather What should I repeat? What is lathering anyway?
Repeating Repeat 3 times: lather rinse Lather Repeat: Rinse
Repeat 3 times: lather rinse How to lather: wet your hair put shampoo in your hand repeat until your hair is foamy: run your hand through your hair
How to wash you hair
Pseudo-Code Code: write things in a way a computer can understand Pseudo- Not quite Pseudo-Code: Not quit write so that a computer can understand Good for expressing things unambiguously But still using English to gloss over the details
Conditionals What if I’m out of shampoo? How to wash your hair: if you are out of shampoo: get more shampoo repeat: lather rinse
Rules of Pseudo-Code Sequence Repetition = repeat something We start with the instruction written at the top We go in order, one instruction at a time Each line is “one” thing to do Repetition = repeat something Repeat a fixed number of times (repeat 3 times) Repeat Until something happens (repeat until hair is foamy) Conditions/Decisions = maybe do something Check something first If there is no shampoo, then get some more Named actions “how to lather” Definition = describe how to perform that action Use = no go do that thing
Do this Repeat 2 times: stand clap sit
Brush your teeth Write pseudo-code instructions for brushing your teeth Share with your neighbor, who will try to find a problem with it Remember: Sequence Repetition Conditionals Named Actions
3x+1 Let x be your age in years Let “the count” be 0 Repeat as long as x is not 1: add 1 to “the count” if x is even: divide x by 2 otherwise: multiply x by 3 and add 1 Clap “the count” many times
X = 18 X = 9 X = 28 X=14 X = 7 X = 22 X = 11 X = 34 X = 17 X = 52 X = 26 X = 13 X = 40 X = 20 X = 10 X = 5 X = 16 X = 8 X = 4 x = 2 X = 1
Variables In Math: In Statistics: In Programming: A name that refers to a particular value In Statistics: A name for a thing that has one value, but we don’t know what it is In Programming: A named value that can vary over the course of doing something x is a variable in the previous slide
Problem Programs cannot “reflect” on what they did Let x be your age in years Let “count” = 0 Repeat as long as x is not 1: add 1 to “count” if x is even: divide x by 2 otherwise: multiply x by 3 and add 1 Clap “count” many times
Bugs Syntax Semantic Logical Understandability
“Homework”