2.2 Programming constructs + Arithmetic/logic operators Flashcards
State the three programming constructs
Programming constructs
Sequence
Selection
Iteration
What is sequence
Sequence is executing one instruction after another.
Write out an example of when sequence is used in a program
Just write out the first few lines of code of the program
Define selection
Selection is a program branching depending on a condition
State some examples of selection constructs
If statement
Switch/Select … Case statements
Explain what Switch/Select … Case statements are
a program can branch in more than one directions depending on the value of a variable - like an if statement
State what is happening in the following code
switch entry:
case “A”:
print (“You selected A”)
case “B”:
print (“You selected B”)
case “C”:
print (“You selected C”)
case “D”:
print (“You selected D”)
default:
print (“Unrecognised selection”)
endswitch
entry (entry is the variable, and it would have something stored in it)
With each case we would inspect the contents of the switch variable, so we would look inside entry and if entry contained a capital A, the line under that case statement would be executed and there could be a number of lines of code here.
If entry didn’t hold a capital A, it would go down to the next case statement which is capital B and say, “does entry contain a capital B”. If “entry” does then it would execute the code under that case statement.
You can have as many case statements as you like and typically after them, you can have what’s called a “default statement”
A default statement is a statement which could run – or any code here would run - if none of the previous cases have been matched.
And then we typically have some way of ending or terminating our switch statement. So here it is ‘end switch’ . ( a bit like end if)
What is iteration
Iteration, sometimes called looping, is repeating sections of code
State some examples of iteration constructions
For Loop
While Loop
Do … Until Loop
What type of loop is a For loop
FOR loops (known as counter controlled loops) are used when the number of iterations needed is known ahead of the iteration executing.
For example, in the code above it is saying:
for roles in range(roles_per_player):
If roles_per_player was 9 then it would be:
for roles in range(9):
So we know the number of times we want to execute or iterate or repeat this code.
What type of loops are while loops
WHILE loops (known as condition controlled loops) are used when the number of iterations is not known because the variable used to determine when the iteration ends is changing within the iteration itself.
What is a DO…UNTIL loop
DO…UNTIL loops are an alternative to WHILE loops where the code executes at least once before the condition is checked
What type of loop is a do…until loop
A DO…UNTIL loop is a condition controlled loop
What is happening in the following code
do
answer=input(“What is the password”
until answer == “computer”
A do until loop is basically
“What is password
while answer != “computer”
“What is password”
asks user what is password
the user cannot progress with the program unless the password is correct. The same question will continue to loop unless the right password is written down
However, with an DO…UNTIL loop we check the exit condition at the end - (until answer == “computer”).
So with a DO…UNTIL loop you can guarantee the code inside the loop will execute at least once. This is an important diference.
The program asks “What is the password”
If the user answerss “computer” the loop is broken however if the student answers something else e.g. “ict”
The program will ask again “What is the password”
The point of a DO..UNTIL loop is so that the line of code “What is the password” is guaranteed to be executed/displayed to the screen at least once, even if it is never going to be executed again.
State the types of arithmetic operators
Addition operator
Subtraction operator
Multiplication operator
Division operator
Exponentiation operator
Modulus operator
Integer division operator