Programming selection and iteration Flashcards

1
Q

Selection - if statement

A

Selection or conditional statements execute a block of code based on the result of some test or condition we have set in the code

Testing to see whether the result is TRUE or FALSE and we can set different actions depending on the result of our test.
Format for an IF statement is:

IF Condition = True
THEN execute instructions 1
END IF

A condition is an expression that has a value of TRUE or FALSE. If the condition is true then the instructions are executed

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

Indentation

A

Indentation signifies the start and end of a block of code
Correct indentation is crucial, or the program may either not run, or give an incorrect result

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

Selection - if else statement

A

Sometimes, the IF statement also contains instructions to be carried out if the condition is False

IF Condition = True
THEN execute instructions 1
ELSE
THEN execute instructions 2
END IF

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

Selection: nesting if statements

A

We can also use IF/ELIF statements to check sub-conditions in a program:
x=5
y=8
if x == y:
print(“x and y are equal.”)
else:
if x < y:
print(“x is less than y.”)
else:
print(“x is greater than y.”)

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

Selection - comparison and logical operators: ==

A

Equality operator
checks whether both values are the same

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

Selection - comparison and logical operators: !=

A

Not equal to

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

Selection - comparison and logical operators: >

A

Greater than

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

Selection - comparison and logical operators: <

A

Less than

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

Selection - comparison and logical operators: >=

A

Greater than or equal to

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

Selection - comparison and logical operators: <=

A

Less than or equal to

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

Selection - comparison and logical operators: AND

A

Checks whether both conditions are true or false
Returns true only when both conditions are true

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

Selection - comparison and logical operators: OR

A

Checks whether either of the conditions are true
Returns true when either input is true

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

Selection - comparison and logical operators: NOT

A

Reverses a Boolean value (true or false)

If true then it becomes false
If false then it becomes true

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

3 basic programming constructs

A

Sequence
Selection
Iteration

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

Iteration

A

Allows a group of statements in the code to be executed several times
Useful because we would otherwise have to write the instructions out the correct number of times
There are two different types of iteration – indefinite and definite
Both can be used but in some cases one is more appropriate than the other

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

Iteration - FOR loop - definite or counter controlled

A

FOR Counter = 1 TO 10
OUTPUT print Counter
ENDFOR

17
Q

Iteration - WHILE loop - Indefinite or conditional

A

Counter = 1
WHILE Counter < 10
OUTPUT print Counter
Counter = Counter + 1
ENDWHILE

18
Q

Iteration - REPEAT UNTIL loop - Indefinite or conditional
(not covered in Python)

A

Counter = 1
REPEAT
OUTPUT print Counter
Counter = Counter + 1
UNTIL Counter <10
ENDREPEAT

19
Q

Iteration - while loops

A

Counter = 1
REPEAT
OUTPUT print Counter
Counter = Counter + 1
UNTIL Counter <10
ENDREPEAT

WHILE loops are also known as conditional loops as they will continue to iterate or loop until a condition, which you have set in your code, is met
Important to make sure that you have written code that will allow your loop to finish and avoid an infinite loop
The condition might be a condition that requires a certain variable to be in a certain range, or a certain statement to be true or false
This type of iteration is characterised by WHILE loops