2.2 Programming constructs + Arithmetic/logic operators Flashcards

1
Q

State the three programming constructs

A

Programming constructs
Sequence
Selection
Iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is sequence

A

Sequence is executing one instruction after another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write out an example of when sequence is used in a program

A

Just write out the first few lines of code of the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define selection

A

Selection is a program branching depending on a condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

State some examples of selection constructs

A

If statement
Switch/Select … Case statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain what Switch/Select … Case statements are

A

a program can branch in more than one directions depending on the value of a variable - like an if statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is iteration

A

Iteration, sometimes called looping, is repeating sections of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

State some examples of iteration constructions

A

For Loop
While Loop
Do … Until Loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What type of loop is a For loop

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type of loops are while loops

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a DO…UNTIL loop

A

DO…UNTIL loops are an alternative to WHILE loops where the code executes at least once before the condition is checked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What type of loop is a do…until loop

A

A DO…UNTIL loop is a condition controlled loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is happening in the following code

do
answer=input(“What is the password”
until answer == “computer”

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

State the types of arithmetic operators

A

Addition operator
Subtraction operator
Multiplication operator
Division operator
Exponentiation operator
Modulus operator
Integer division operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

State the symbols of the arithmetic operators

A

Addition operator +
Subtraction operator -
Multiplication operator *
Division operator /
Exponentiation operator ^
Modulus operator MOD
Integer division operator DIV

17
Q

Show the addition operator in use

A

x = 7 + 2
x = 9

18
Q

Show the subtraction operator in use

A

x = 7 - 2
x = 5

19
Q

Show the multiplication operator in use

A

x = 7 * 2
x = 14

20
Q

Show the division operator in use

A

x = 7 / 2
x = 3.5

21
Q

Show the exponentiation operator in use

A

X= 7 ^ 2
X = 49

22
Q

Show the Modulus operator in use

A

x = 7 MOD 2
x = 1
WORKS OUT THE REMAINDER
7/2 = 3 REMAINDER 1
MOD - It means how many whole numbers are left over once you fit the number on the right into the number on the left as many times as you can
2 can fit into 7, 3 whole times – and there is 1 left over – modulus returns the left over value

23
Q

Show the Integer division operator in use

A

x = 7 DIV 2
X = 3

Do normal division - take away the decimal part

How many whole times does the number on the right fit into the number on the left.

2 can fit into 7, 3 whole times – the integer division operator returns this value

Different to normal division – 7/3 = 3.5

24
Q

What is the MODULUS operator

A

x = 7 MOD 2
x = 1
WORKS OUT THE REMAINDER
7/2 = 3 REMAINDER 1
MOD - It means how many whole numbers are left over once you fit the number on the right into the number on the left as many times as you can
2 can fit into 7, 3 whole times – and there is 1 left over – modulus returns the left over value

25
Q

What is the Integer Division operator

A

x = 7 DIV 2
X = 3

Do normal division - take away the decimal part

How many whole times does the number on the right fit into the number on the left.

2 can fit into 7, 3 whole times – the integer division operator returns this value

Different to normal division – 7/3 = 3.5

26
Q

State the common comparison operators

A

Equals to
Not equals to
Less than
Less than or equals to
Greater than
Greater than or equals to

27
Q

State the symbols for the common comparison operators

A

Equals to ==
Not equals to !=
Less than <
Less than or equals to <=
Greater than >
Greater than or equals to >=

28
Q

What is the difference between “==” and “=”

A

== is different to = (this is used for assignment, while “==” is used as an equals to comparison operator)

29
Q

Show the equals to operator in use

A

7 == 7

30
Q

Show the not equals operator in use

A

7 != 5

31
Q

Show the less than operator in use

A

5 < 7

32
Q

Show the less than or equals to operator in use

A

5 <=5

33
Q

Show the greater than operator in use

A

7 > 5

34
Q

Show the greater than or equals to operator in use

A

7 >=7