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.
Scope of the while loop?
The WHILE statement defines the start of the loop. The ENDWHILE statement declares the end of the loop. By combining the start and the end, the scope of the statement is identified.
When does a while loop stop?
The iteration continues until the condition test result is FALSE, at which point the loop ends and the program executes the next line of code in sequence after the loop.
Is it possible for the code in a WHILE loop to never be executed.
Because the condition is tested at the start of the loop, it is possible for the code within it to never actually be executed. Consider this program:
password ← USERINPUT WHILE password <> “B1t3s1z£” OUTPUT “Password incorrect. Try again.” password ← USERINPUT ENDWHILE
Are while loops useful for validation?
Yh
Consider this program:
password ← USERINPUT WHILE password <> “B1t3s1z£” OUTPUT “Password incorrect. Try again.” password ← USERINPUT ENDWHILE
The first time the condition is tested, the result may be FALSE if the value of password matches. Because of this, none of the code within the loop will be executed and the program will move onto the next line of code in sequence after the loop.
WHILE loops are particularly useful for validation of user inputs as the code can insist that they retry entering data until it is correct.
Difference between a WHILE and REPEAT UNTIL loop
REPEAT UNTIL loops function in the same way as WHILE loops, with one major difference - the condition is tested at the end of the loop
Print a string ten times using repeat until loop
count ← 0 REPEAT OUTPUT “Coding is cool” count ← count + 1 UNTIL count = 10
Is it possible for the code in a REPEAT UNTIL loop to never be executed.
No.
The REPEAT statement defines the start of the loop. The UNTIL statement tests the condition and declares the end of the statement scope. Because the condition is tested at the end, the code within the loop is always executed at least once, even if the result of the test is FALSE.
Key difference between WHILE and REPEAT UNTIL loop in terms of function
With a WHILE loop, the code within the iteration may never be executed. With a REPEAT UNTIL loop, the code is always executed at least once.
Another type indefinite (condition controlled) iteration with the condition at the end - according to spec (not repeat until)
DO
… Instructions here …
WHILE NotSolved
Use nested iteration to print out the times table for all numbers from one to ten
Iteration can also be nested. This program uses two definite FOR loops, one within another, to print out the times table for all numbers from one to ten:
FOR x ← 1 TO 10 FOR y ← 1 TO 10 result ← y * x OUTPUT y + " * " + x + " = " + result ENDFOR ENDFOR
What can be in nested iteration?
Nested iteration isn’t limited to FOR loops. A WHILE loop can be nested in a FOR loop and a FOR loop can be nested in a WHILE loop.
An example of nested iteration would be:
Using while and for
An example of nested iteration would be:
WHILE NotSolved ... Instructions here ... FOR i ← 1 TO 5 ... Instructions here ... ENDFOR ... Instructions here ... ENDWHILE
An example of nested selection would be:
An example of nested selection would be:
IF GameWon THEN ... Instructions here ... IF Score > HighScore THEN ... Instructions here ... ENDIF ... Instructions here ... ENDIF
Why nested selection can be good?
Nested selection can be built up over and over to include many possibilities and paths for a program to follow. It allows for complex decisions to be made.
(Strange answer)
The grade of a student depends on the marks gained in an exam. The grade bands are
Grade C if the marks are less than or equal to 20%
Grade B if marks are more than 20% and less than or equal to 60%
Grade A if marks are more than 60%
Write pseudo-code using nested something
The pseudocode below makes use of nested IFs to work out the grade
Mark USERINPUT(“Enter your exam result as a percentage”)
IF Mark <= 20 THEN
PRINT ‘You obtained a C grade’
ELSE
IF Mark > 20 AND Mark <= 60 THEN
PRINT ‘You obtained a B grade’
ENDIF
ELSE
PRINT ‘You obtained an A grade’
ENDIF
Does nested ifs have to be used?
No.
IF-THEN-ELSE IF ENDIF (removes the need for multiple indentation levels)
- see aqa pseudo-code syntax pdf - could not copy it here properly (but two programs are same, but one makes use of nested, while other does not)
A variable is
A variable is a named piece of memory that holds a value. The value held in a variable can - and usually does - change as the program is running.
A variable’s name is known as
Identifier
Identifier naming conventions (5)
It can contain letters and numbers but must start with a letter.
It must contain at least one letter - at the start of the name.
It must not contain special characters such as !@£$%&* or punctuation characters. However, an underscore can be used.
Spaces are not allowed.
It should contain lowercase letters. However, uppercase letters can be used if a variable name comprises more than one word joined together.
The name should be meaningful - it should represent the value it is holding.
Use meaningful identifier names and know why it is important to use them.
What should the name do?
It is good practice in programming to give variables a sensible name that will help explain its purpose.
Link between variables and memory locations (not important?)
Variables make it easy for a programmer to use memory locations. The computer keeps track of which memory location the variable refers to. All the programmer has to do is remember the identifier the variable was given.
Variable Declaration
Most programming languages require a variable to be identified before a value is assigned to it. This is known as declaring a variable.
Some programming languages, such as Python, do not require variables to be explicitly declared before use.
Giving a variable a value is known as __
Assignment
A constant is?
A constant is a named piece of memory where the value cannot be changed while a program runs.
Why constants are useful
Constants are useful because they are declared and assigned once, but can be referred to over and over again throughout the program. This means that if the programmer needs to change the value throughout the program code, they only need to make one change. This can help make a program easier to maintain.
Constant naming convention
Constants follow the same naming conventions as variables, except that they are often written in uppercase. Some programming languages, such as Python, do not support constants.
Constants are used for values that are unlikely to change, for example:
CONSTANT PI ← 3.141
(AQA pseudo-code ‘names of constants will always be written in capitals’)
Global vs local variables?
A global variable is one that can be accessed and changed throughout the whole program. Local variables only exist within a particular subroutine.
Advantages of local variables (2) - guessing there will be another question like this, with better answers later
Using local variables rather than global variables makes it easier to debug a program as the value of that variable can only be read or changed within one subroutine.
Another advantage of local variables is memory efficiency. Once a subroutine has finished running, the memory used for all local variables is removed.
27
Data type and purpose?
Integer Whole numbers
27.5
Data type and purpose
Real Decimal numbers
A
Data type and purpose
Character A single alphanumeric character
ABC
Data type and purpose
String One or more alphanumeric characters
TRUE
Data type and purpose
Boolean TRUE/FALSE
Limitations of different data types (2)
integers and real numbers cannot be concatenated, ie joined together.
numbers held in strings cannot be subject to mathematical operations.