1 / 20

Computer Program

Computer Program. A sequence of step-by-step instructions for the computer to follow. Why bother? Demo: Human vs. Computer following instructions. Computer Instructions. 0. COMMENTS: Notes to self or other coders—what code should do

Download Presentation

Computer Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Name stuff Do this 3. Do that 4. Jump to instruction 8 Do the other thing All done, sleep Finally, do this important thing If switch closed, do that thing you do 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, yposition 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. Draw Stairs: What’s the code? 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 Initialize counter (once)Check condition. If true, do instructions then increment counter Effect: Repeats instructions for desired number of times for (i=0;i<n;i++) {instructions} Example What is j at the end? Ans: j = 3 Ans: j = 6 j=0 for (i=1;i<4;i++) { j=j+1; } j=0 for (i=1;i<4;i++) { j=j+i; }

  10. Draw Stairs: What’s the code?

  11. Draw Stairs: What’s the code? 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 thesize No marks on the graph Super-neat writing

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

  14. More Complexity: Conditionals and Functions if (expr) { Evaluate. If true, do instructionsinstructions} 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 Comments //drawbox function; //prints box of size z drawbox(intz) { PEN_DOWN if(z>10){ z = 10; } MOVE(Up , z) MOVE(Right, z) MOVE(Down , z) MOVE(Left , z) }

  16. Next slide has the answer

  17. 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(intz) { PEN_DOWN if(z>10){ z = 10; } MOVE(Up , z) MOVE(Right, z) MOVE(Down , z) MOVE(Left , z)PEN_UP }

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

More Related