Professional Documents
Culture Documents
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
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);
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