top of page

SELECTION STATEMENTS

Two Way Selectors

The general form of two-way selection statement of Python is the following format:

                                                     if  <expression> :

                                                               statement(s)

                                                     else:

                                                               statement(s)

The control expression in Python can be arithmetic. The clauses in Python must use indentation or it will be an indentation error.

2 way.png
Two Way Selectors

IF-ELSE

multiway 1.png

NESTED-IF

multiway 2.png

Multi Way Selectors 

Multiple way selectors allow the selection of one of any number of statements or statement groups. Python uses IF-ELIF-ELSE statements for decision making.

Multi Way Selectors

ITERATIVE STATEMENTS

The “for” loop will iterate once for each item defined in the list passed to it at the beginning of the loop. Below, we will use the first example in the picture to explain this process. For the first time, the “for” loop will iterate the target variable “name” and the first item of value “Jack” in the list will be assumed. Then, this process will keep continue until it reaches the end of the list which is “Emily”.

Counter-Controlled Loops 
counter controlled l.png
Counter-Controled Loops

We use while loop as a logically-controlled loops example since Python does not have do… while loop statement. We first initialise a default value as ‘0’ for the counter. Then, the while loop condition statement will check whether the counter is less than 4, and if this is true, it will print the Hello World! statement. At the end of the loop body, it will update counter by incrementing by 1. This process will keep continuing to print the “Hello World!” statement until the counter is not less than 4.

Logically-Controlled Loops 
logically controlled l.png
Logically-Controlled Loops
User located loop control mechanisms

Continue

While in a loop, the “continue” statement can check the current iteration of the loop so that it can skip and continue with the next one.

Break

The “break” statement can stop the loop if we want the loop to stop at a certain iteration of the loop.

User located Loop Control Mechanisms

Iteration Based on Data Structure

Different looping techniques are primarily useful in the places where we don’t need to actually manipulate the structure and ordering of overall container, rather only print the elements for a single use instance, no in place change occurs in the container. This can also be used in instances to save time.

Iteration based on Data Structures

Proudly created by TacoCat

bottom of page