Lectures 7 to 8 Revision Flashcards

(13 cards)

1
Q

What does the term FLOW OF CONTROL mean?

A

Controlling how your code is executed. So the FLOW OF CONTROL describes the order in which commands are executed.

So far we have executed our code in a sequential or linear fashion. Statements are executed one line at a time in the order that they appear. Common for simple scripts. But does not give much power in our programs.

If we want to change that we have to change the flow of control. i.e change the order in which our statements are executed.

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

What is a BLOCK?

A

A block is a group of related statements grouped together. We can execute different blocks in order or even repeat a block over and over depending on conditions.

In Python, use an indentation to define a block (tab)

In Python, blocks can be nested (i.e you can have a block within a block). Use two tab spaces for a block within a block.

A block can contain many statements (or sub blocks). Group them together by indenting them the same amount.

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

How can we ALTER the flow of control?

A

Two standard ways are:

SELECTION - chosing actions based on some condition

ITERATION - repeating actions based on some condition. You have to have a means to stop this i.e keep repeating until this condition changes.

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

What is an elif ?

A

elif is an ‘ELSE IF’ condition.

Allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions is True. It’s part of the control flow structures used to handle decision-making in your code.

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

What defines a BLOCK?

A

Indentation defines a block. Use tab to get the indentation

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

Define the general syntax of an IF statement

A
  • Always starts with the word if
  • After that supply a CONDITION. Must be a binary (TRUE or FALSE condition).
    Then an indented block that gets executed if that condition is true.
  • Then optional ELSE IF part(s) of the statement. Any number you want or none at all, and an indented block that gets executed if those or that is TRUE.
  • Then lastly, another optional condition ELSE. Used to define the default behaviour you want. i.e if NONE of the other conditions are TRUE. Whatever is in your ELSE indented block gets executed instead.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ITERATION?

A

ITERATION or ITERATIVE PROGRAMMING is looping. Looping the same block over and over again until something happens.

Remember you need it to stop looping otherwise it will continue looping infinitely and the code will not execute.

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

What are the two types of loops that we can use for iteration?

A

WHILE loop

FOR loop

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

How do you use a WHILE loop (what is the syntax)?

A

The standard iterative command is WHILE.

To use it first need to create a counter variable outside the while (before the while command). Called the INITIALISATION.
Then within the while block we use that variable as part of our condition. At some point within our block we need to modify the condition so the loop ends (i.e we need the condition to become false so the loop ends).
The GUARD phase is the condition that will be evaluated to true or false. When it becomes false the loop stops.
The PROGRESS phase is the modification in some way so that the guard becomes false.

An example:
i = 0 (INITIALISATION)
while i < 5: (GUARD)
print (“Hello”) (PROGRESS)
i = i + 1

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

How do you use the FOR loop?

A

It is used to iterate over a list of items.

So you create a variable with a list (e.g a list of names).
Then you create an operation to perform on each item.
The loop continues until there are no more items in the list.

An example:
names = [“Mark”, “Ray”, “Pavel”]
for name in names:
print(“Hello”, name)

or
list = [“Hello”, “Hello”, “Hello”]
for value in list:
print(value)

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

How do you use the FOR loop with RANGE?

A

range is a command build into python that will generate a list for us. We give range a value to tell it how many elements in the list. It takes overr the initialisation, guard and progress for us (they are not seen in the code but they are there).

for value in range(3):
print(“Hello”)

This will also print hello three times.

So the range function takes an integer value as a parameter and generates a list between zero and that value.
so range(3) returns a list [0, 1, 2]

Can also use it to access elements in a list. e.g.
list = [“Fergus”, “Ray”, “Pavel”]
for i in range(len(list)):
print(i, “:”, list[i])

Will output to screen:
0 : Fergus
1 : Ray
2 : Pavel

So range function lets us specify the number of iterations in a loop

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

Give an example of how the range(N) function works

A

range(N) generates a list from zero up (but not including N. By default it increments by 1.

examples:
range(5,10) gives from 5 up to (not including) 10, so [5,6,7,8,9]

can also give it a third number to tell it how much to ‘increment’ by:

range(5,10,2) would give from 5 up to (not including 10 in ‘hops’ of 2. So gives [5,7,9]

range(7,15,3) would give [7,10,13]

using a - increment value counts backwards in the specified number.

range(9,5,-1) gives [9,8,7,6]

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

What must we be careful of when using for loops in blocks?

A

Don’t edit the list that your loop is running through or you can end up with an infinite loop that will never execute.
NEVER ALTER A LIST THAT YOU ARE ITERATING THROUGH IN A BLOCK WHEN USING A FOR LOOP

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