120 likes | 196 Views
Computer Program. A sequence of step-by-step instructions for the computer to follow. Computer Instructions. 1. OPERATION: Evaluate expression, set output, read input, ... 2. JUMP: Jump immediately, and out of sequence to another instruction
E N D
Computer Program A sequence of step-by-step instructions for the computer to follow
Computer Instructions 1. OPERATION: 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
A Program 1. Do this 2. Do that 3. Jump to instruction 6 4. Do the other thing 5. All done, sleep 6. If switch closed, do that thing you do 7. Jump to instruction 4
Pen Computer PEN_UP Lift pen off paper PEN_DOWN Lower pen onto paper MOVX dMove pen a distance d in x direction MOVY dMove pen a distance d in y direction PEN_UP MOVX 8 MOVY 8 PEN_DOWN MOVX 4 MOVY 4 MOVX -4 MOVY -4 PENUP
PEN_UP MOVX 8 MOVY 8 PEN_DOWN MOVX 4 MOVY 4 MOVX -4 MOVY -4 PENUP
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 }
Program 1 pen_down movx 20 movy 16 movx -20 movy -16 pen_up for (i=1;i<4;i++) { movx 4*i movy 4*i pen_down movx 4 movy 4 movx -4 movy -4 pen_up movx -4*i movy -4*i } (go to top of next column)
More Complexity: If and Functions if (expr) { Evaluate. If true, do instructionsinstructions} myfunction() Jump to myfunction(). When done, return to next instruction.
Program 2 main() { pen_up movx 10 movy 8 pen_down for (i=0;i<5;i++) { movy 1 } movepen() pen_up movx -2 movy -2 pen_down for (i=0;i<4;i++) { movx 1 } pen_up } //end of main, stop here //movepen function movepen() { pen_up movx 4 pen_down movy 4 pen_up movx -4 movy -4 }