You are on page 1of 19

CMP 131

Introduction to Computer
Programming
Violetta Cavalli-Sforza
Week 3, Lecture 2
Today’s Plan
• Questions
• Quiz (1/2 hour)
• Break (brief)
• Lecture: Important Programming Concepts
Program Structure
• Programs are made up of:
– Program headers
– Declaration statements:
• Name things
• Allocate memory
• Examples:
– Constant Definitions
– Type Definitions
– Variable Declarations
– Executable statements
• Statements that perform actions
Executable Statements
• Assignment statements
• Selection statements
• Looping statements
• ….
Important Structured Programming
Concepts
• Selection
• Repetition/Looping/Iteration
• Sequential execution (stacking)
• Nesting
• Blocks & Compound Statements
(Most high-level PLs provide constructs for
these important structured programming
concepts. Pascal was one of the first PLs
to provide them.)
Selection
• Conditional statements
– Condition controlled execution
– Single branch : IF <condition> THEN
– Two branches :
IF <condition>
THEN <statements>
ELSE <statements>
– Multiple branches:
CASE statement
Selection Examples
• Single branch:
– IF a condition is true THEN perform an action
– The condition is tested once
– The action is performed once
• Examples:
– IF it’s cold THEN wear a coat
– IF a homework is due THEN do it
–…
Selection Examples
• Two branches:
– IF a condition is true
THEN perform an action
ELSE perform a different action
– The condition is tested once
– One of the two actions is performed once
• Examples:
– To take the absolute value of a number:
IF N >= 0 THEN return N ELSE return -N
– IF the time is before noon
THEN say good morning
ELSE say good afternoon
–…
Selection Examples
• Multiple branches:
– The condition is tested once
– Several values of the condition are possible
– Several actions are possible
– One of the actions is performed once
• Examples:
– CASE Score OF
10, 9 : Grade is A
8, 7 : Grade is B
6, 5 : Grade is C
OTHERWISE Grade is F
–…
Repetition / Looping / Iteration
• Fixed / Definite
– Know before entering how many times will repeat loop
body
– While the “how many times” may be a variable:
• Its value is set before entry into the loop
• Its value may not (should not) be changed inside the loop
– FOR loop
• Variable Condition / Indefinite
– Don’t know before entering how many times will
repeat loop body
– Entry into / exit from loop is controlled by a variable
that can be changed inside the loop
– REPEAT…UNTIL : tests condition at end
– WHILE … DO : tests condition at start
Mean of 20 Numbers: Refinement
PROCESSING:
Initialize Sum to 0

Do the following 20 times:


Get a Number
Add it to Sum
Compute Mean as Sum / 20

This is an example of definite looping: You know how


many times you go around the loop when you start.
Mean of N Take 1: Refinement
PROCESSING:
Initialize Sum to 0
Prompt for value of Count
Read value of Count
Do the following Count times:
Prompt for Number
Read Number
Add Number to Sum
Compute Mean as Sum / Count
This is ALSO an example of definite looping :
You know how many times you go around the loop when you start.
Mean of N Numbers (Take 2):
Refinement
PROCESSING:
Initialize Sum to 0
Initialize Count to 0
repeat
Prompt for Number
Read Number
Add Number to Sum
Increment Count by 1
until no more numbers
Compute Mean as Sum / Count
Mean of N (Take 3)
PROCESSING:
Initialize Sum to 0 called a sentinel value
Initialize Count to 0 because it guards the loop

Prompt for Number


Read Number
while Number is not equal to -99999 do:
Add Number to Sum
Increment Count
Prompt for Number
Read Number
Compute Mean as Sum / Count

This is also an example of indefinite looping :


You DO NOT know how many times you go around the loop until you
Stacking
• Two meanings:
– sequential execution vs. nested execution
• one statement/block of code after the other, vs.
• one statement/block of code inside the other
– a way of storing and retrieving data:
• Last-In First-Out (LIFO) regime
• Used in a certain style of processor architecture
(stack machines)
• Used for memory management at runtime with
procedure and function calls.
• Used for certain types of applications (e.g. search)
Nesting
• A block of code is visually placed inside
another block of code AND
• The execution of the internal block of code
is controlled by the execution of the
external block of code.
• Ex.
Do the following 20 times:
Get a Number
Add it to Sum
PROGRAM Silly (output);

CONST Name = 'George';


Age = 26;

VAR J, Sum : integer;

BEGIN
Sum := 0;
FOR J := 1 TO 10 DO
Sum := Sum + J;
writeln ('My name is ':28, Name);
writeln ('My age is ':27, Age);
writeln;
writeln ('The sum is ':28, Sum)
END.
PROGRAM Silly (output);

CONST Name = 'George';


Age = 26;

VAR J, Sum : integer;

BEGIN
Sum := 0;
FOR J := 1 TO 10 DO
Sum := Sum + J;
writeln ('My name is ':28, Name);
writeln ('My age is ':27, Age);
writeln;
writeln ('The sum is ':28, Sum)
END.
Blocks of Code
• Most statements like FOR, IF, etc.. control only
one other statement:
– What do I mean by that?
• To make them control multiple statements, you
need to enclose those statements in a
bracketing mechanism: BEGIN … END.
• BEGIN … END
– essentially says: treat all my contents as if they were
a single complex statement
– creates a compound statement

You might also like