3.2 Programming Flashcards
The three combining principles that are basic to all high-level imperative programming languages.
sequence, iteration/repetition and selection/choice
The three programming constructs:
Sequence, selection, iteration
Two types of iterations (2 names each), also known as
definite iteration (also known as count-controlled iteration) indefinite iteration (also known as condition-controlled iteration)
Sequence is
Sequence is the order in which programming statements are executed
Why is the sequence of a program important?
The sequence of a program is extremely important as once these are translated, carrying out instructions in the wrong order leads to a program performing incorrectly.
In this pseudo-code program, designed to find the average of two whole numbers, there is an error. What kind?
total ← 0 average ← number1/number2 OUTPUT “Enter the first number” number1 ← USERINPUT OUTPUT “Enter the second number” number2 ← USERINPUT OUTPUT "The average is " + average
Running this program would result in a logic error, because it tries to calculate the average before it knows the values of the numbers.
This version has the instructions in the correct sequence:
total ← 0 OUTPUT “Enter the first number” number1 ← USERINPUT OUTPUT “Enter the second number” number2 ← USERINPUT average ← number1/number2 OUTPUT "The average is " + average
Selection is?
- it is the process of?
Selection is a programming construct where a section of code is run only if a condition is met.
- Selection is the process of making a decision.
- The result of the decision determines which path the program will take next.
How does selection work?
Selection works by testing a condition. The test gives a Boolean result - TRUE or FALSE.
In programming, how is selection implemented? - so how would it be written in pseudo-code
In programming, selection is implemented using IF THEN ELSE statements:
OUTPUT “How old are you?”
age ← USERINPUT
IF age > 16 THEN
OUTPUT “You are old enough to drive a car”
ELSE
OUTPUT “Come back when you are older!”
ENDIF
Iteration is
The repetition of a block of statements within a computer program.
Iteration is often referred to as _ (2)
Iteration is often referred to as looping, since the program ‘loops’ back to an earlier line of code. Iteration is also known as repetition.
Why iteration is helpful?
Iteration allows programmers to simplify a program and make it more efficient . Instead of writing out the same lines of code again and again, a programmer can write a section of code once, and ask the program to execute the same line repeatedly until no longer needed.
When a program needs to iterate a set number of times, this is known as __ iteration and makes use of a __ loop.
A () loop uses an extra variable called a __ __ that keeps track of the number of times the loop has been run.
Definite iteration
FOR loop
A FOR loop uses an extra variable called a loop counter that keeps track of the number of times the loop has been run.
This program would print a message out six times:
OUTPUT “Coding is cool” OUTPUT “Coding is cool” OUTPUT “Coding is cool” OUTPUT “Coding is cool” OUTPUT “Coding is cool” OUTPUT “Coding is cool”
Improve efficiency using iteration.
Definite iteration (also known as count-controlled iteration):
FOR count ← 1 TO 6
OUTPUT “Coding is cool”
ENDFOR
FOR count ← 1 TO 6
OUTPUT “Coding is cool”
ENDFOR
What kind of variable is ‘count’ ? (not important)
Stepper variable
Use iteration to print out the ten times table, from 1 to 10.
The stepper variable used to initialise a FOR loop can be used within the loop itself. This program uses a loop’s condition variable to print the ten times table:
FOR count ← 1 TO 10
OUTPUT count * 10
ENDFOR
How does iteration simplify a program? (2)
As can be seen above, by using iteration a program is simplified, less error prone and more flexible. This is because:
There are fewer lines of code, meaning fewer opportunities for typing errors to creep in
To increase or decrease the number of iterations, all the programmer has to do is change the loop’s end value
What is STEP?
It is also possible to add the keyword STEP to the first line of a FOR loop to determine how much the stepper variable increases or decreases by with each iteration.
Code to output 1, 3, 5, 7, 9 as the value of count increases by two for each iteration
FOR count ← 1 TO 10 STEP 2
OUTPUT count
ENDFOR
Code to display 5, 4, 3, 2, 1
The code below would display 5, 4, 3, 2, 1 as the value of count decreases by one for each iteration:
FOR count ← 5 TO 1 STEP -1
OUTPUT count
ENDFOR
What does indefinite iteration do
Indefinite iteration repeatedly executes a section of code until a condition is met - or no longer met.
There are two types of indefinite iteration:
There are two types of indefinite iteration:
WHILE loops - uses the statements WHILE and ENDWHILE
REPEAT UNTIL loops - uses the statements REPEAT and UNTIL
Print a string six times, using a while loop
count ← 0 WHILE count < 6 OUTPUT “Coding is cool” count ← count + 1 ENDWHILE
When do WHILE loops test the condition is met
At the beginning of the loop.
If the condition is met, the code within the loop is executed before the program loops back to test the condition again.