Unformatted text preview:

Chapter 3:ProgrammingCS105: Great Insights in Computer ScienceThe Vector• We can build (or at least imagine!) lots of circuits.• We can even think about state machines that use circuits to do various things over time.• We’re headed towards using these ideas to create a programmable computer.• Hillis has us take a detour to talk about programming first.Programming• Many different languages have been devised for programming.• Each provides a way of writing a kind of script along with rules for the computer to interpret the script as instructions.• They can all do pretty much the same things, but make it easier to say some things than others.• How many languages can you name?Scratch• Developed by the “Lifelong Kindergarten” group at MIT.• Allows users to make media-rich programs by clicking together blocks.• Share your creations a la YouTube.• My miniNim program is an example.Programming WindowThings• Performance area!stage (background)!sprites (objects)- scripts (behavior)- costumes (appearance)- sounds (available sounds)More Things• Run/Stop• Script inventory!Motion!Looks!Sound!Pen!Control!Sensing!Numbers!VariablesShapes• Trigger• Statement• Ending statement• Boolean value• Numeric valueMotion InventoryLooks InventorySound InventoryPen InventoryControl InventoryLogo in Scratch• Logo is a language invented to help people (kids?) learn to program. • Scratch is the language we’ve been using for examples and demos in this class. It’s a descendent of Logo, in many ways.• Hillis uses Logo for his examples in this chapter.• I’ll translate them into Scratch so it fits better with our other examples.Drawing CommandsNew CommandDo It!Use It!Command With VariableAfterwards, I renamed it “squareSize”Conditionals• Based on the random number, two possible outcomes.• Executes only one block of code, depending on the Boolean condition.If. If is good.(Infinite) LoopingNever never never stops!(bug in book)Kinds of Loops• Infinite loop• “For” loop: Repeat a set number of times.!Flexible.• “While” loop: Until a condition holds.!More Flexible.• Recursion: Repeat substructure.!Most Flexible.Just Four, Thanks• Sets a repeat count• Stops after that number of repetitionsUnroll The Loop• It’s as if the statements inside the loop are repeated four times.More Power• “For” (repeat) loops are great if you know how many times you will be repeating.• Sometimes need something more powerful.• “While” (repeat-until) loops keep going until a condition becomes true.• Can behave like for loops...Four Square CountdownUnroll, With ConditionsEven More Power• “While” (repeat-until) loops are great if repetitions are sequential.• Sometimes need something more powerful.• “Recursion” can allow control to proceed in multiple directions at once!• But, can also behave like for loops...Infinite Recursion• Each “pattern” message spawns another.• Ad infinitumProper Recursion• “Base case” says what to do when the counter runs down.• In this case, it stops when the counter reaches zero.Not A Simple Loop• Loop proceeds in two directions at once.• Can’t do that with for or while.(In fact, can’t do it at all in Scratch, but other it is commonplace in other programming languages.)I Think That I Shall Never See...• Each tree is made up of a trunk and two smaller trees.• Base case keeps the structure from growing infinitely.• Makes shapes known as “fractals”.• Other


View Full Document

Rutgers University CS 105 - Programming

Download Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?