Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!

Similar presentations


Presentation on theme: "Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!"— Presentation transcript:

1 Flow Control in Imperative Languages

2 Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!

3 Flow Control in Imperative Languages Introduction What is an Imperative Language and Flow Control? An imperative language is one which is has a set of statements to determine how to reach a goal. The FLOW of these statements is CONTROLLED by 3 different structures. - Sequencing - Selection - Iteration Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops).

4 Flow Control in Imperative Languages The 3 Constructs of Imperative Languages Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). And Then This Then This Do This While X = 2 Keep Looping! 1.Sequencing Performing one instruction after another 3. Iterations The program repeating, looping infinitely or for a set number of times. 2. Selection The program making decisions

5 Flow Control in Imperative Languages Sequencing A sequence is a control structure where the computer executes every instruction in the order in which they are written. Here is an example of sequencing in an algorithm: Each line is executed before moving on to the next. Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). And Then This Then This Do This 1.Sequencing Performing one instruction after another

6 Flow Control in Imperative Languages Selection – Conditional Operations Often we want programs to act differently when certain conditions occur. Selection is a control structure which contains instructions that DO NOT have to be executed in sequence (non-sequential). There are two main types of selection. - Selection of 2 possible pathways - IF-THEN-ELSE statements - Multiple Selections - CASE statements (uses the keyword ELIF in Python) Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 2. Selection The program making decisions

7 Flow Control in Imperative Languages Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 2. Selection The program making decisions Selection of 2 – IF-THEN-ELSE STATEMENTS These are used to see if a certain condition is true or false. IF the condition is true THEN a certain set of instructions will be executed. ELSE (condition is false) then a different set of instructions will be executed.

8 Flow Control in Imperative Languages Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 2. Selection The program making decisions Multiple Selection – CASE STATEMENTS (ELIF in Python) These are used to run one of many possible instructions based on various conditions It makes use of CASE OF and ENDCASE to select one instruction from a set of instructions, depending on the value of a variable. It does the same job as multiple (nested) IF-ELSE statements but is more efficient.

9 Flow Control in Imperative Languages Iterations – Iterative Operations Often we want programs to repeat a process until a condition is met. Iteration is the Control Structure which allows this to occur. There are two ways to get code to loop: 1. Counting a set number of loops 2. Setting a condition which must be met for the loop to end. Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 3. Iterations The program repeating, looping infinitely or for a set number of times.

10 Flow Control in Imperative Languages Counting a set number of loops If we know how many times we want a loop to run for we use a FOR loop. This uses a counter with FOR, TO and NEXT to repeat a block of code a set number of times. The counter can go up or down in different steps. Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 3. Iterations The program repeating, looping infinitely or for a set number of times.

11 Flow Control in Imperative Languages Setting a condition (which must be met for the loop to end) If we do not know how many times we want a loop to run for and instead want the loop to repeat until a certain condition is met we will use a WHILE loop. This states that WHILE a certain condition is true, the code is continue repeating. The check occurs at the start of each loop. The instructions in the loop will be ignored if the WHILE condition is false. Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 3. Iterations The program repeating, looping infinitely or for a set number of times.

12 Flow Control in Imperative Languages Setting a condition (which must be met for the loop to end) In most programming languages (but not python) there is also a REPEAT UNTIL loop which will repeat a block of code until a condition occurs. The check is always carried out at the end of the loop. Unlike the WHILE loop, the instructions will always be carried out at least once. Learning Objectives: (g) understand and use sequence in an algorithm (h) understand and use selection in an algorithm (IF and CASE statements) (i) understand and use iteration in an algorithm (FOR, WHILE and REPEAT loops). 3. Iterations The program repeating, looping infinitely or for a set number of times.


Download ppt "Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!"

Similar presentations


Ads by Google