PL1 Lecture 1.3 Basic Concepts
PL1 Lecture 1.3 Basic Concepts
List of topics,
Pseudocode
Variables
Loops
Decision
Function
debugging
3
What is pseudocode?
End
6
Read birthYear
1. Start
2. Get year born Age= currentYear - birthYear
3. Calculate age
4. Print age Print Age
End
7
Elements of a Program
• All programming languages have certain features in common.
For example:
Variables
Commands/Syntax (the way commands are structured)
Loops
Decisions
Functions
Variables
• Variables are part of almost every program.
Variables (cntd.)
• Here are some examples of data types:
Format
Data Type Size (bytes) Range
Specifier
Variables (cntd.)
• Variables may be classified as global or local.
Loops
Decisions
• A program often needs to decide whether something is true or false to see which way to
continue.
• Programs often use IF (or IF THEN or IF THEN ELSE) statements to show a decision.
• An IF statement always has a condition to check, often a comparison between a variable and a
number.
• These instructions (for “true”) may come after the word THEN, or they may simply be listed.
• In an IF THEN statement, when the condition is false, the program simply ignores the THEN
commands and continues to the next line.
• In an IF THEN ELSE statement, commands are given for both the true and false conditions.
14
Functions
• In most programming languages, small sub-programs are used to perform
some of the tasks.
• These may be called functions, subroutines, handlers, or other such
terms.
• Functions often have names (e.g., getName or CALCTAX).
Debugging
• Virtually no program works the first time you run it. There are just too
many places to make errors.
• When you are debugging a program, look for spelling and punctuation
errors.
Self-Check 1
A computer program is…
A series of instructions to accomplish something
A TV show
Written in Egyptian hieroglyphics
Can be written any way you want to
17
Self-Check 2
To “compile” a program means to…
Translate it into English
Translate it into binary code
Pile up the punch cards used for the program
Run the program as it was written
18
Self-Check 3
Pseudocode is…
The program as it is written in a programming
language
The results of a program that makes secret codes
The logic of a program written in English
The logic of a program shown in a chart
19
Self-Check 4
The flowchart symbol to perform a calculation is…
20
Self-Check 5
The flowchart symbol to show a decision is…
21
Self-Check 6
Look at the flowchart section below. If the
variable X is 5, what will print (K or 1st)?
N Y
Print “K” X > 5? Print “1st”
K will be printed. The answer to the question “Is X greater than 5?”
is NO, since X is equal to (not greater than) 5.
22
Self-Check 7
A function in a program is…
Something from trigonometry, like COSINE
A sub-program, usually performing one task
A way to check the accuracy of a program (a “function
check”)
23
Self-Check 8
A variable in a program is…
A letter or word that represents a place to store data
A decision made within a program
A small sub-program used to find errors
24
Class Task
Try to write pseudo code and create a flowchart for a program that:
• The word GOOD should be printed only if the average is more than 80.