CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

60
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Transcript of CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Page 1: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

CMP 131Introduction to Computer

Programming

Violetta Cavalli-Sforza

Week 6, Lecture 1 (Monday)

Page 2: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

TODAY

• Flow of control and flow charts

• Conditional statements

• Boolean expressions

• Go over Homework #2 programming part

Page 3: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

TOMORROW (Lab)

• Exercises on the computer with I/O and simple conditional statements.

• Review of topics for Midterm Exam

Page 4: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Syntax Graphs, Flowcharts, & Structure Charts

• Syntax graph– To define the language syntax

• Structure chart– To show the relationship between program modules and the data

transfer between the modules (Example)

CONST =Identifier ;value

Compute mean of 3 numbers

Read 3 numbers

Compute mean of inputs

Display mean to screen

Page 5: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

• Flowchart– A form of visual control-flow specification

employing arrows and balloons of various shapes.

– Shows flow of actions (algorithm) in a program

Page 6: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

How the locus (location) of execution changes as the program executes.

Example. Let Height = 3 and Width = 5

FOR I := 1 to HeightBEGIN

FOR J := 1 TO Width DO write(‘*’); writeln;

END

What is Flow of Control?

Page 7: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = ?

J = ?

Page 8: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = ?

Page 9: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = ?

Page 10: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 1

Page 11: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 1

Page 12: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 1

Page 13: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 2

Page 14: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 2

Page 15: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 2

Page 16: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

A few steps later …

Page 17: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 6

Page 18: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 6

Page 19: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 1

J = 6

Page 20: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 2

J = 6

Page 21: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 2

J = 6

Page 22: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 2

J = 6

Page 23: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 2

J = 1

Page 24: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 2

J = 1

Page 25: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

A few steps later …

Page 26: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 3

J = 6

Page 27: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 4

J = 6

Page 28: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 4

J = 6

Page 29: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

007 …008 I := 1009 IF I > 3 GOTO 018010 J := 1 011 IF J > 5 GOTO 015012 write(‘*’)013 J := J + 1014 GOTO 011015 writeln016 I := I + 1017 GOTO 009018 …

DATA MEMORY

I = 4

J = 6

Page 30: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Flow of Control

• You can show it by animation for the simplest programs or

• You can show it much more succinctly with Flow Charts

• A graphical formalism for showing how control flows through a program

• Standard Flowchart Symbols (later) fromhttp://www.wiley.com/legacy/college/busin/icmis/oakman/outline/chap05/slides/symbols.htm

Page 31: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Flowchart Symbols

Page 32: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Rectangle Flow Chart

I := 1

write(‘*’)

J := 1

J := J+1

I := I+1

I > 3

J > 5 writeln

no

yes

no

yes

BEGIN

END

Page 33: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Yes/No Choice

• The logic:IF some condition or test is TRUETHEN perform some action[ ELSE do nothing ]

• Examples: – If the car is running out of gas, put in gasoline.– If you are failing your class and you cannot drop it,

study more.– If someone says hello and you don’t want to seem

impolite, say hello back.

Page 34: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Yes/No Choice: The IF-THEN Statement

• Syntax: IF <condition> THEN <statement>

• Syntax graph:

• Flowchart:

IF condition THEN statement

statement 1

conditionyes

no

statement 3

statement 2

Page 35: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Either/Or Choice

• The logic:IF some condition or test is TRUETHEN perform some actionELSE perform a different action

• Examples: – If the weather is hot wear light clothes, else wear

warm clothes.– If a number is odd increment OddCount else

increment EvenCount– If a noun starts with a capital letter categorize it as a

proper noun (name) else as a common noun.

Page 36: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Either/Or Choice : The IF-THEN-ELSE Statement

• Syntax: IF <condition> THEN <statement> ELSE <statement>

• Syntax graph:

• Flowchart:

IF condition THEN statement ELSE statement

statement 1

conditionyes

statement 4

statement 2no

statement 3

Page 37: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

What is <statement> ?

• A single statement:– An input or output statement– An assignment– Another IF … THEN … ELSE statement– A looping statement (FOR, WHILE, REPEAT)

• A compound statement:BEGIN

<statement> ; <statement> ; ….<statement>

END

Page 38: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

What is <condition> ?

• A boolean expression

• Named after the mathematician George Boole who invented logic.

• Boolean expressions – Evaluate to TRUE or FALSE– Include

• different types of operands• different types of operators

Page 39: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Boolean Expressions

• Boolean constants– TRUE– FALSE

• Boolean variables– Declared as type “boolean”

CONSTDebug = TRUE; { or FALSE }

VARSaveDebug : BOOLEAN;

Page 40: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

• Relational expressions: Arithmetic, character, or strings acted upon by relational operators: – Relational operators are:

• <= less than or equal to• < less than• >= greater than or equal to• > greater than• = equal• <> not equal

– Relational operators compare two compatible values• Two characters• Two strings• Real and integer• Two Booleans

– Relational operators return a Boolean value

Page 41: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

• Examples: new < old

‘B’ < ‘A’

value <> -99999

3.0 < 4

MiddleInitial = ‘L’

1.0 = 1.0/ 3.0 * 3.0 { Implementation dependent}

Page 42: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

• Logical expressions: Boolean values acted upon by logical operators– Logical operators are:

• AND• OR• NOT

– Logical operators take Boolean operands and return Boolean values.

– Examples:• IF NeedMoney AND

(NOT HaveMoneyInHand) AND HAVE MoneyInBankTHEN {Go get money at the bank}ELSE {Ask parents for money}

• IF Value < MinValue OR Value > MaxValue THEN writeln (‘Value ‘, Value, ‘ is out of range.’)

Page 43: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Truth Tables for Boolean Operators

Op1 Op2 Op1 AND Op2 Op1 OR Op2 NOT Op1 true true true true false

true false false true false

false true false true true

false false false false true

binary operators unary operator

Page 44: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Operator Precedence: Arithmetic, Relational and Logical

Operator Precedence( ) parentheses Highest (evaluated

first)

- + NOT (unary operators)

* / DIV MOD AND

+ - OR

< <= = <> >= > Lowest (evaluated last)

Page 45: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

(4.2 >= 5.0) AND (8 = (3 + 5)) (4.2 >= 5.0) OR (8 = (3 + 5))

writeln('Value of (-2 < 0) AND (18 >= 10) is : ', (-2 < 0) AND (18 >= 10) ); writeln('Value of (-2 < 0) OR (18 >= 10) is : ', (-2 < 0) OR (18 >= 10) ); writeln('Value of (3 > 5) AND (14.1 = 0.0) is : ', (3 > 5) AND (14.1 = 0.0) ); writeln('Value of (3 > 5) OR (14.1 = 0.0) is : ', (3 > 5) OR (14.1 = 0.0) ); writeln('Value of NOT (18 = 10 + 8) is : ', NOT (18 = 10 + 8) ); writeln('Value of NOT (-4 > 0) is : ', NOT (-4 > 0) ); writeln('Value of ''a'' < ''b'' is : ', 'a' < 'b' ); writeln('Value of ''a'' < ''A'' is : ', 'a' < 'A' );

Page 46: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Summary of Operations on Boolean Values

• Logical operations (AND, OR, NOT)– Relational operators act on non-Boolean

values but return Boolean values• Assignment

VAR Debug, SaveDebug : BOOLEAN;Debug := TRUE;SaveDebug := Debug; {Save the value of Debug}

• Output (in Turbo Pascal)writeln (‘The value of Debug is ‘, Debug);

• (Not Input – can’t read/readln a Boolean value)

Page 47: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Boolean expressions

• Appear primarily in control structures• Used to determine the sequence in which Pascal

statement are executed.• Boolean variables are often called Boolean ‘flags’ :

varDebug, SaveDebug : Boolean;…

BEGIN…SaveDebug := Debug;Debug := TRUE;IF Debug THEN {print out the value of several variables}Debug := SaveDebug;...

END

Page 48: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Example Expressions

• See program BOOLEXPR.PAS

Page 49: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Go Over Homework #2, Programming Part

Page 50: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Exercises 1.5, #13

• Write a complete program that produces the following table:

WIDTH LENGTH AREA

4 2 8

21 5 105

Page 51: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (1)

• Write table header: WIDTH LENGTH AREA

• Write table contents: 4 2 8

21 5 105

• Simplest codewriteln(‘WIDTH LENGTH AREA’);

writeln(‘ 4 2 8’);

writeln(‘ 21 5 105’);

Page 52: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Using formatting

• Assume table header starts in first column 1:• Write header as

– WIDTH, 8 spaces, LENGTH, 8 spaces, AREA• Write columns of numbers as

– First number fits in field of width 3– Second number fits in field 2+8+4 = 14– Third column fits in field of 2+8+4 = 14

• Code will bewriteln(‘WIDTH’,‘ ’:8,‘LENGTH’,‘ ’:8,‘AREA’);writeln(4:3,2:14,8:14);writeln(21:3,5:14,105:14);

Page 53: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Exercises 1.5, #8

• Assume the hourly wages of five students are: 3.65, 4.10, 2.89, 5.00, 4.50. Write a program that produces the following output, where the “E” in Employees is in column 20.

Page 54: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Desired Output

12345678901234567890123456789012345678901234

-------------------------

Employees Hourly Wage

-------------------------

1 $ 3.65

2 $ 4.10

3 $ 2.89

4 $ 5.00

5 $ 4.50

-------------------------

Page 55: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (1)

• Write table header:

------------------------- Employees Hourly Wage -------------------------

• Write table contents:

1 $ 3.65

….• Write table footer: -------------------------

Page 56: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (2)• Write table header:

– Write line starting at column 20– Write column headings starting at column 20– Write line starting at column 20

------------------------- Employees Hourly Wage -------------------------

• Write table contents:– Write 5 lines each with:

• Employee number starting at column 23• Employee wage starting at column 36

1 $ 3.65

….• Write table footer:

– Write line starting at column 20 -------------------------

Page 57: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (2)• Write table header:

– Write line starting at column 20– Write column headings starting at column 20– Write line starting at column 20

------------------------- Employees Hourly Wage -------------------------

• Write table contents:– Write 5 lines each with:

• Employee number starting at column 23• Employee wage starting at column 36

1 $ 3.65

….• Write table footer:

– Write line starting at column 20 -------------------------

Page 58: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (3)

• Write table header:– Write line starting at column 20– Write column headings starting at column 20– Write line starting at column 20

------------------------- Employees Hourly Wage -------------------------

• Pascal code:– CONST Line = ‘-------------------------’– BEGIN

writeln(‘ ’:19,Line); writeln(‘ ’:19,‘Employee’,‘ ’:5,‘Hourly Wage’);

writeln(‘ ’:19,Line); … END.

Page 59: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (4)• Write table contents:

– Write 5 lines each with:• Employee number starting at column 23• Employee wage starting at column 36

1 $ 3.65

….• Pascal code:

– BEGIN…

writeln(1:23,‘$’:13,3.65:5:2); writeln(2:23,‘$’:13,4.10:5:2);

writeln(3:23,‘$’:13,2.89:5:2); writeln(4:23,‘$’:13,5.00:5:2); writeln(5:23,‘$’:13,4.50:5:2);

… END.

Page 60: CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)

Task Decomposition (5)

• Write table footer:– Write line starting at column 20

-------------------------

• Pascal code:– CONST Line = ‘-------------------------’– BEGIN

...

writeln(‘ ’:19,Line);

END.