70 likes | 83 Views
Learn about programming primitives through a fun artwork exercise. Replicate a picture with a partner and discuss observations. Understand high-level instructions and the process of designing a program.
E N D
Artwork: An introduction to programming primitives • Draw a picture [1 minute] • It can be concrete, abstract, whatever you want. • Don’t show it to anyone yet. You will show it later. • Find a partner far away from you & sit together [1 minute] • Each A and B (partners), do this: • A: Without showing B your picture, tell B how to replicate the picture exactly. • B: Following A’s instructions, draw the picture. • After 2 to 3 minutes, compare the original and the copy. • Repeat, with A and B reversed.
Artwork – Observations • Class discussion: • What are your observations from this exercise? • Your observations may include: • Giving adequate instructions is hard! • Good instructions must be precise. • The instructions can be executed more than once. • The 2nd execution may yield the same picture as the 1st. • Or perhaps not, if the instructions interact with the environment: • perhaps you use a different pencil • perhaps you had too much coffee and your hand starts shaking. • These are all characteristics of computer programs, too!
What is a Program? • A program is a set of instructions that a computer can follow, one by one. • If the computer knows how to do the instruction,the computer simply carries out the instruction. • If there is an instruction that the computerdoes not know how to do,the instruction needs to be rewritten in more detail. • These are called high-level instructions. • Exercise: What are some high-level instructions from the copy-my-picture exercise? • See the next slide for some answers.
High-Level Instructions • Suppose the picture is a picture of a person. • Draw the face is a high-level instruction.How can it be rewritten? • Draw the face might become: • Draw the outline of the face. Draw two eyes. Draw eyebrows. Draw the nose. Draw the mouth. • These are high-level instructions too!How might draw two eyes be rewritten? • Draw two eyes might become: • Draw one eye near the top left of the outline. Draw the other eye near the top right of the outline. • How might draw one eye be rewritten?
Designing a Program, Part 1 • These high-level instructions show that you can use top-down design to build programs like the copy-my-picture program. • Step 1: • Ask yourself “What do I do next?” • until you have a sequence of instructions that accomplishes the desired result. • Step 2: • Ask yourself “How do I do each of these things?” • until each instruction is something the computer knows how to do.
Programming Primitives • We call the things that the computer knows how to do programming primitives. • What general-purpose programming primitives did you use in the draw-my-face exercise? • Most computer languages provide: • Combining by sequencing: do this, then that, then ... • Combining by grouping and giving the group a name (high-level instructions). • This is called procedural abstraction(akaencapsulating in methods) • Loops • E.g. (for drawing a circle): Repeat 360 times: draw a short line and change direction by 1 degree. • Conditionals • E.g.: If you have finished the circle, stop; else ...
Summary, so far • A programmer answers the questions: • What do I do next? • How do I do each of these things? • Programming primitives include: • Sequencing, procedural abstraction, conditionals, loops. • This is only one part of the programming problem. The second part is: Coordinating the activitiesof many interdependent participantsin a computational community