Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Program A sequence of step-by-step instructions for the computer to follow Why bother? Demo: Human vs. Computer following instructions.

Similar presentations


Presentation on theme: "Computer Program A sequence of step-by-step instructions for the computer to follow Why bother? Demo: Human vs. Computer following instructions."— Presentation transcript:

1 Computer Program A sequence of step-by-step instructions for the computer to follow Why bother? Demo: Human vs. Computer following instructions

2 Computer Instructions 0. COMMENTS: Notes to self or other coders— what code should do 1. OPERATION:Declare variable, evaluate expression, set output, read input,... 2. JUMP:Jump immediately, and out of sequence to another instruction 3. BRANCH:Evaluate a condition and jump if condition true 4. LOOP:Repeat specified section a number of times

3 A Program 1.Name stuff 2.Do this 3. Do that 4.Jump to instruction 8 5.Do the other thing 6.All done, sleep 7.Finally, do this important thing 8.If switch closed, do that thing you do 9.Jump to instruction 4 (and only AFTER you’re done 2)(does this ever happen?)What if the switch Is open?

4 Pen Computer PEN_UP Lift pen off paper PEN_DOWN Lower pen onto paper MOVE(dir, k) Move in direction ‘dir’ (either up, down, left or right) a distance of ‘k’ units (1, 2, 3, 4, …10) INIT_POS Start out with the pen over position x, y position 0,0 A square shape: MOVE(Right, 1) MOVE(Down, 1) MOVE(Left, 1) MOVE(Up, 1)

5 MOVE(Right, 4) MOVE(Up, 4) MOVE(Left, 4) MOVE(Down, 4)

6 INIT_POS PEN_UP MOVE(Right, 8) MOVE(Up, 8) PEN_DOWN MOVE(Right, 4) MOVE(Up, 4) MOVE(Left, 4) MOVE(Down, 4) PEN_UP

7 Draw Stairs: What’s the code?

8 INIT_POS PEN_DOWN MOVE(Right, 1) MOVE(Up, 1) MOVE(Right, 1) MOVE(Up, 1) MOVE(Right, 1) MOVE(Left, 1) MOVE(Right, 1) MOVE(Up, 1) MOVE(Right, 1) MOVE(Up, 1)...BLEH!!!

9 Some Complexity: For Loops for (i=0;i<n;i++) { instructions } j=0 for (i=1;i<4;i++) { j=j+i; } Example What is j at the end? Initialize counter (once) Check condition. If true, do instructions then increment counter Effect: Repeats instructions for desired number of times j=0 for (i=1;i<4;i++) { j=j+1; } Ans: j = 6Ans: j = 3

10 Draw Stairs: What’s the code?

11 INIT_POS PEN_DOWN for(i=0;i<12,i++){ MOVE(Right, 1) MOVE(Up, 1) }

12 Write Code to draw a spiral, you choose the size No marks on the graph Super-neat writing

13 Declaring Variables, Initializing, and Assigning Values int i; Declare an integer variable named i int j= 0; Declare an integer named j, initially set to 0 j = 10; Set value of j to 10 i = j + 1; Evaluate j+1 (which is 11) and save it in i Not Equations

14 More Complexity: Conditionals and Functions if (expr) { Evaluate. If true, do instructions instructions } myfunction( ) Jump to myfunction(). When done, return to next instruction. May have arguments to pass values: myfunction( i )

15 Run This Program // Draw a face main() { INIT_POS drawbox(6) // face outline MOVE(Right, 1) MOVE(Up, 4) drawbox(1) // left eye MOVE(Right, 3) drawbox(1) // right eye MOVE(Left, 2) MOVE(Down, 3) drawbox(2) // mouth } //end of main, stop here //drawbox function; //prints box of size z drawbox(int z) { PEN_DOWN if(z>10){ z = 10; } MOVE(Up, z) MOVE(Right, z) MOVE(Down, z) MOVE(Left, z) } Comments

16 Next slide has the answer

17

18

19 Run This Program // Draw a face main() { INIT_POS drawbox(6) // face outline MOVE(Right, 1) MOVE(Up, 4) drawbox(1) // left eye MOVE(Right, 3) drawbox(1) // right eye MOVE(Left, 2) MOVE(Down, 3) drawbox(2) // mouth } //end of main, stop here //drawbox function; //will print z-sized boxes, //with edges of max length 10 drawbox(int z) { PEN_DOWN if(z>10){ z = 10; } MOVE(Up, z) MOVE(Right, z) MOVE(Down, z) MOVE(Left, z) PEN_UP }

20 Programming Summary 1.Computer programs are just a set of VERY EXPLICIT instructions 2.Instructions executed one after the other 3.Only does what you tell it to do, NOT what you intend 4.When combined with control structures (branching, looping, conditionals) can be very powerful: fast, complex, repetitive tasks 5.Crucial skill for engineering


Download ppt "Computer Program A sequence of step-by-step instructions for the computer to follow Why bother? Demo: Human vs. Computer following instructions."

Similar presentations


Ads by Google