Programming Chap 2
Programming Chap 2
•Designing a Program
•Output, Input, and Variables
•Variable, Assignment and Calculation
•Variable Declarations and Data Types
•Named Constants
•Hand Tracing a Program
•Documenting a Program
•Designing Your First Program
Designing a Program
For example, suppose your little sister asks you how to boil
water. Assuming she is old enough to be trusted around the
stove, you might break down that task into a series of steps as
follows:
Pour the desired amount of water into a pot.
Put the pot on a stove burner.
Turn the burner to high.
Watch the water until you see large bubbles rapidly rising.
When this happens, the water is boiling.
This is an example of an algorithm, which is a set of well-
defined logical steps that must be taken to perform a task.
Example
Suppose you have been asked to write a program to
calculate and display the gross pay for an hourly paid
employee. Here are the steps that you would take:
•Get the number of hours worked.
•Get the hourly pay rate.
•Multiply the number of hours worked by the hourly pay
rate.
•Display the result of the calculation that was performed in
Step 3.
NOTE:
This algorithm isn’t ready to be executed on the computer.
The steps in this list have to be translated into code.
Programmers commonly use two tools to help them
accomplish this: pseudocode and flowcharts.
Pseudocode
The word pseudo means fake, so pseudocode is fake code.
It is an informal language that has no syntax rules, and is
not meant to be compiled or executed. Instead,
programmers use pseudocode to create models, or “mock-
ups” of programs.
Example of how you might write pseudocode for the pay
calculating program that we discussed in slide 5:
Example 3:
Write a pseudocode statement that declares the variable
total so it can hold integers. Initialize the variable with the
value 0.
Notice that the string literal "Here is the value that you
entered: " ends with a space. That is because in the
program output, we want a space to appear after the
colon, as shown here:
Variable Assignment and Calculations
Variable Assignment
In the previous section, you saw how the Input statement
gets a value typed on the keyboard and stores it in a
variable. You can also write statements that store specific
values in variables. The following is an example, in
pseudocode:
Set price = 20
This is called an assignment statement. An assignment
statement sets a variable to a specified value. In this case,
the variable price is set to the value 20. When we write an
assignment statement in pseudocode, we will write the
word Set, followed by the name of the variable, followed
by an equal sign (=), followed by the value we want to
store in the variable.
Variable Assignment (Continue)
Example 1:
Set dollars = 2.75
Display "I have ", dollars, " in my account.“
Computer Algorithm
1.Get the number of excess minutes as input.
2.Calculate the overall fee by multiplying the number of
excess minutes by 0.35.
3.Display the overage fee.
Example 2 (Continue)
Pseudocode
Set average=(test1+test2+test3)/3
The Order of Operations (Continue)
Notice that the flowchart uses a new symbol: