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

Slides:



Advertisements
Similar presentations
Desk Checking. Desk checking is a manual (non computerised) technique for checking the logic of an algorithm. The person performing the desk check effectively.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Repeating Actions While and For Loops
ITC 240: Web Application Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Program Design and Development
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Advanced Decisions and Loops Chapter Some Simple Schoolroom Statistics.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Mini Project II – Drawing Machine
Fundamentals of C and C++ Programming Control Structures and Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 12: How Long Can This Go On?
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
By the end of this session you should be able to...
Algorithms and Algorithm Analysis The “fun” stuff.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
The George Washington University Department of ECE ECE Intro: Electrical & Computer Engineering –Intro to the Robotics –Introducing the IC –Discuss.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Controlling Program Flow with Decision Structures.
Computer program A sequence of step-by-step instructions for the computer to follow.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 4 – C Program Control
The George Washington University Department of ECE ECE Intro: Electrical & Computer Engineering Intro to the Robotics Introducing the IC Discuss.
Chapter 4 - Program Control
ALGORITHMS & FLOWCHARTING II
Computer Programming Fundamentals
CS1100 Computational Engineering
Additional Control Structures
A First Book of ANSI C Fourth Edition
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Computer Science Core Concepts
Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.
Objectives You should be able to describe: The while Statement
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Chapter 4 - Program Control
Desk Checking.
Assignment Operators Topics Increment and Decrement Operators
Lec 6 Loop Statements Introduction to Computer Programming
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

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 1. OPERATION:Declare variable, evaluate expression, set output, read input, 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.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?

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)

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

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

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!!!

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

Draw Stairs: What’s the code?

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

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

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

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 )

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

Next slide has the answer

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 }

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