SlideServe
Menu
Préférences
© SlideServe · v2026

Computer Program

gareth-giles avatar
20 diapositives · 355 Views · Nov 15, 2014

This guide delves into the fundamentals of computer programming, emphasizing the structured sequence of instructions a computer follows. It explains key concepts such as operation, jumps, branches, and loops, illustrating these through practical examples like drawing shapes and executing functions. The importance of comments, variable declarations, and proper coding practices is also highlighted. By mastering these essentials, programmers can create complex, efficient scripts, enhancing their problem-solving abilities and productivity in engineering tasks.

Computer Program - slide 1
01 / 20Computer Program
1 / 17

Télécharger la présentation

Computer Program
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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