ACSE 2006 8 th Conference The Iconic Programmer

  • Slides: 30
Download presentation
ACSE 2006 8 th Conference The Iconic Programmer Stephen Chen

ACSE 2006 8 th Conference The Iconic Programmer Stephen Chen

Overview l l l Actual slides from first three weeks Want students to focus

Overview l l l Actual slides from first three weeks Want students to focus on programming concepts rather than (JAVA) syntax Want students to learn to think in structures (i. e. problem solve) ACSE -- The Iconic Programmer 2

Structured Programming l l Focus on processes – focus on actions 3 structures of

Structured Programming l l Focus on processes – focus on actions 3 structures of structured programming § Sequence ØPerform an action § Branch ØSelect an action (or no action) § Loop ØRepeat an action ACSE -- The Iconic Programmer 3

Iconic Programmer l Each structure has an icon l Program by assembling icons ACSE

Iconic Programmer l Each structure has an icon l Program by assembling icons ACSE -- The Iconic Programmer 4

Sequence l l Standard operation of a computer Actions are performed in sequence §

Sequence l l Standard operation of a computer Actions are performed in sequence § First action § Second action §… § Last action l Program runs same way each time ACSE -- The Iconic Programmer 5

Actions l Action § Manipulate data l Iconic Programmer § Declare § Assign §

Actions l Action § Manipulate data l Iconic Programmer § Declare § Assign § Output ACSE -- The Iconic Programmer 6

Declarations l A computer needs to allocate storage space for all data that it

Declarations l A computer needs to allocate storage space for all data that it manipulates § Declaration gives a meaningful name to the data element/storage space l Iconic Programmer § Only integer data elements § Give name in text box ACSE -- The Iconic Programmer 7

Assignments l l Once the computer has a storage space, it can store/change data

Assignments l l Once the computer has a storage space, it can store/change data in that space Iconic Programmer § Random value § Result of mathematical expression § User input ACSE -- The Iconic Programmer 8

Mathematical Expressions l value = value +1 (math) § The value is equal to

Mathematical Expressions l value = value +1 (math) § The value is equal to the value plus one § Impossible mathematically l value = value + 1 (computers) § The storage space for value will become the previous contents plus one l Actions: perform math, perform storage ACSE -- The Iconic Programmer 9

Output l l When computer is done with program (or during debugging), we may

Output l l When computer is done with program (or during debugging), we may want to see the result – what is in a storage space Iconic Programmer § Value in storage space § Text information (nominally stored) ACSE -- The Iconic Programmer 10

Sample Program l Output the double and triple of an input § Declare a

Sample Program l Output the double and triple of an input § Declare a storage space § Assign an input value to the storage space § Declare a second storage space § Assign double the input to this space § Output the value in the second space § Assign triple the input to this space § Output the value in the second space ACSE -- The Iconic Programmer 11

Branching l Branching allows a program to make decisions § Diamonds represent conditions §

Branching l Branching allows a program to make decisions § Diamonds represent conditions § Two outgoing paths from condition § Paths (with sequences) can be skipped ACSE -- The Iconic Programmer 12

Example of Branching l Program specification § Make withdrawal if funds are sufficient l

Example of Branching l Program specification § Make withdrawal if funds are sufficient l Program actions § Check account balance and withdraw amount § Make withdrawal l Need to make withdrawal optional ACSE -- The Iconic Programmer 13

Decisions l l l Branching selects from two paths Two paths two states §

Decisions l l l Branching selects from two paths Two paths two states § true § false (yes) (no) Diamond contains a condition § A condition is a true-false question ACSE -- The Iconic Programmer 14

Relational Operators l How to turn integers into true/false? § Greater than § Less

Relational Operators l How to turn integers into true/false? § Greater than § Less than § Equal to § Not equal to § Greater than or equal to § Less than or equal to ACSE -- The Iconic Programmer 15

Compound Conditions l Allow us to put two (or more) subconditions into a condition

Compound Conditions l Allow us to put two (or more) subconditions into a condition § AND § OR ACSE -- The Iconic Programmer 16

AND l The expression is TRUE if and only if both input variables are

AND l The expression is TRUE if and only if both input variables are TRUE FALSE 1 0 TRUE FALSE 1 1 0 FALSE 0 0 0 ACSE -- The Iconic Programmer 17

OR l The expression is TRUE if either input variable is TRUE 1 FALSE

OR l The expression is TRUE if either input variable is TRUE 1 FALSE 0 ACSE -- The Iconic Programmer TRUE 1 FALSE 0 18

Inclusive and Exclusive OR l Computers use inclusive OR § Stop the bus if

Inclusive and Exclusive OR l Computers use inclusive OR § Stop the bus if passenger. A OR passenger. B wants to get off l Exclusive OR is different § You can get $1000 cash back or 0% financing ACSE -- The Iconic Programmer 19

Two Branches l Did you roll a doublet? § die 1 == die 2

Two Branches l Did you roll a doublet? § die 1 == die 2 double move § Normal move ACSE -- The Iconic Programmer 20

Multiple Branches l What is your grade classification? § >= 80 honours § >=

Multiple Branches l What is your grade classification? § >= 80 honours § >= 60 pass § Not pass ACSE -- The Iconic Programmer 21

Looping l l l In Sequence and Branching, each action is performed at most

Looping l l l In Sequence and Branching, each action is performed at most once Looping allows certain actions to be repeated Keeps programs from getting large and unmanageable ACSE -- The Iconic Programmer 22

Example of Looping l Program specification l Program § Take user inputs until they

Example of Looping l Program specification l Program § Take user inputs until they guess correctly § Declare and initialize number § Declare guess § Input and compare guess §… ACSE -- The Iconic Programmer 23

Looping Icon l Loops have same components as branches § Condition selects one of

Looping Icon l Loops have same components as branches § Condition selects one of two paths § Optional path “loops back” to condition ACSE -- The Iconic Programmer 24

Java syntax – branches l Basic shell if (/*boolean expression*/) { // conditional statements

Java syntax – branches l Basic shell if (/*boolean expression*/) { // conditional statements } ACSE -- The Iconic Programmer 25

Java syntax – exclusive branches if (/*boolean expression*/) { // conditional statements } else

Java syntax – exclusive branches if (/*boolean expression*/) { // conditional statements } else { // default statements } ACSE -- The Iconic Programmer 26

Java syntax – multiple branches if (/*boolean expression*/) { // conditional statements } else

Java syntax – multiple branches if (/*boolean expression*/) { // conditional statements } else if (/*boolean expression*/) { // more conditional statements } ACSE -- The Iconic Programmer 27

Java syntax – loops l Basic shell while (/*boolean expression*/) { // repeatable statements

Java syntax – loops l Basic shell while (/*boolean expression*/) { // repeatable statements } ACSE -- The Iconic Programmer 28

Common Error l l Decision to loop does not require a branch Decisions are

Common Error l l Decision to loop does not require a branch Decisions are not structures Branches - selected actions Loops - repeated actions ACSE -- The Iconic Programmer 29

Sample Program – Telephone Banking l Write a program that implements an ATM interface

Sample Program – Telephone Banking l Write a program that implements an ATM interface § 1 – get balance § 2 – deposit § 3 – withdraw § 9 – end session l What are the key structures? ACSE -- The Iconic Programmer 30