Learn to create a simple sequential program and execute it.
The process of problem solving
The four stages of problem solving are listed below:
- Analyze: Understand the available resources and constraints of a problem.
- Plan: Write the steps that solve the problem in the form of a program. This is the plan of action that will solve the problem.
- Implement: Execute a computer program to obtain the results.
- Evaluate: Match the results of the program with the desired output to determine success.
Simple language
One of the rules for good writing is to not repeat words. To ensure the readers enjoy what we write, we may use words with similar meanings instead of repeating them. For example, we may use any of the following similar but distinct phrases that mean roughly the same thing:
- Display on the screen
- Show on the monitor
- Print on the display unit
- Display
- Show
- Output on the screen
- Send to the screen
- Write to the console
However, we don’t want this type of fancy writing with multiple variations in our programs. We should select one simple way of saying something and use it the same way everywhere, even if we use it ten times in a single program.
Let’s say we pick “print” and use it to mean that we want to show something on the screen. We call this an instruction in the programming language. Similarly, we use “input” to mean that we want input from the user. We only use lowercase for these commands. Using a standardized vocabulary will help others understand our programs.
We’ll create a few simple programs using the above-mentioned simple programming language. Keep in mind that all programs are sequential and follow the instructions one by one in a sequence. We’ll also demonstrate the implementation of the programs with the help of the execution sheets.
Example programs
Let’s look at some example programs, starting simply and adding complexity.
Print the product
First, let’s create a program to display the product of and . This program has two steps—finding the product using the multiplication operator *
and displaying the resultant answer.
prod = 5 * 7
print prod
The program above is expected to display 35
. In this program, prod
is the name of the variable that holds the result of 5 * 7
. When we use prod
with the print
command, the value will be displayed. Therefore, print prod
will display the value of the prod
.
Label the product
Next, let’s create a program to display the product of and along with the text label "product:"
written before the resulting prod
.
prod = 5 * 7
print "product:"
print prod
The program above is expected to display product: 35
. Notice that text written in double quotes will display as text in the output. Therefore, "product:"
will be displayed before the value of the variable prod
. This "product:"
text is called a label.
More variables
Next, let’s create a program that displays the product of two values stored as separate variables.
a = 5
b = 7
prod = a * b
print "product:"
print prod
The program above is expected to display the product of two numbers stored in separate variables. Here, a
and b
are variables with values 5
and 7
, respectively. In the third line, their product is stored in another variable, prod
. So, the output product: 35
is displayed.
Interaction with the user
Now, let’s create a program that displays the sum of two input values.
input a
input b
sum = a + b
print "sum:"
print sum
This program is expected to display the sum of two numbers input by the user. In the first line, input
is used to take input from the user and store it in the a
variable. In the second line, input
is used to take input from the user and store it in the b
variable. The output of this program will change according to the input values. If the user enters 5
and 7
, the output sum: 12
is displayed.
Prompt the user
Finally, let’s create a program that displays the difference of two values input by the user with prompt messages.
print "Please enter the first integer value:"
input a
print "Please enter the second integer value:"
input b
diff = a - b
print "difference:"
print diff
The program above is expected to display a prompt asking the user to enter an integer. After receiving the first input from the user, it shows another prompt to the user for the second input. Then, it shows the difference of the two numbers input by the user.
The text displayed before the input
command is the prompt for the input of the user. The output of this program will change according to the input values. If we use 5
and 7
, the output difference: -2
is displayed.
Execution sheet
A program is just a plan of action to solve a problem. The execution of the program follows its instructions step-by-step. It involves observing and updating the values of the
Structure of the execution sheet
The execution sheet takes the form of a table with rows and columns. The program instructions are listed by rows on the sheet. Each row contains one of the program’s instructions. The columns of the sheet change depending on the variables in the program.
-
Program sequence (PS#): This column shows the sequence number of each instruction in the program.
-
Instruction: This column contains the actual instructions.
-
Input/Output: This column shows the resulting output of the
print
instruction in the corresponding row. -
Variables: If there is only one variable in the program, we’ll have a column labeled with its name to show how its value changes across the program. If there are three variables in a program, there will be three columns. The portion shaded in gray indicates the nonexistence of the respective variable. The value of the variable remains unchanged unless it is modified by an instruction. The background color is used to quickly identify the instruction modifying the respective variable.
Sample problem
Let’s start with a sample problem to understand the execution sheet and the process of filling it. We want to display the sum and difference of two numbers input by the user.
Sample execution sheet
To solve this problem, here’s how we’ll fill the execution sheet:
- Write the line number of each instruction in the first column.
- Write instructions for the program in the second column, one instruction per row in order of execution.
- Provide one column for “input/output” and four columns for the variables
a
,b
,sum
, anddiff
.
Filling the execution sheet
- The first instruction,
input a
, asks the user to input the value ofa
. Let’s say we input50
. We also need to write it in the third column as “Input/Output” and in the fourth column as a new value of the variablea
. - The second instruction,
input b
, asks the user to input the value ofb
. Let’s say we input35
here. We also need to note it in the third column as “Input/Output” and in the fifth column as a new value of the variableb
. - The third instruction,
sum = a + b
, calculates the sum of50
and35
to store85
in the variablesum
. Write this down in the sixth column as a new value of the variablesum
. - The fourth instruction,
diff = a - b
, calculates the answer of50 - 35
to store the difference15
in the variablediff
. Write this in the last column as a new value of the variablediff
. - The fifth instruction,
print sum
, displays the latest value in the “sum” column, so we note85
in the third column as “Input/Output”. - The last instruction,
print diff
, displays the latest value in the “diff” column, so we note15
in the third column as “Input/Output”.
Let us practice the above execution sheet with various input pairs.
First, type two integers in the input boxes below and press the 'Show' button to print the execution sheet.
For example,
When you type:
50
35
Then the execution sheet will be displayed.
Execution sheet for example programs
Let’s start typing our example programs into the execution sheet.
Product with the label
Let’s create a program to display the product of 5
and 7
along with the label "product:"
written before the resulting value.
Use more variables
Let’s create another program to display the product of two values stored in separate variables.
Find the unit rate
Now, let’s create a program to find the unit rate from a total bill amount and quantity of units provided by the user.
In the execution sheet above:
-
The first instruction,
print "Please enter the bill amount:"
, shows a prompt for the user. -
The second instruction,
input amount
, asks the user to input the value to store in theamount
variable. Let’s say we input300
so we write it in the third column as “Input/Output” and in the fourth column as a new value of the variableamount
. -
The third instruction,
print "Please enter the quantity:"
, shows the second prompt to the user. -
The fourth instruction,
input quantity
, asks the user to input the value of the variablequantity
by showing the given prompt. Let’s say we input6
here so we write it in the third column as “Input/Output” and in the fifth column as a new value of the variablequantity
. -
The fifth instruction,
rate = amount / quantity
, calculates the quotient of300 / 6
and stores50
in the variablerate
. We note it in the sixth column as a new value of the variablerate
. -
The sixth instruction,
print "Unit rate is:"
, displays the label. -
The last instruction displays the latest value in the “rate” column. So, we write
50
in the third column as “Input/Output”.
Get hands-on with 1400+ tech skills courses.