Skip to content
Learnearn.uk » Home » Sequencing and Selection

Sequencing and Selection

Sequencing

Sequencing

Sequencing refers to the order in which statements are executed. Code is typically executed in a sequential manner, line by line, from top to bottom. Each statement is executed after the previous statement has finished.

Sequencing in Python

Sequencing in Python is achieved by executing code on line at a time, from the top to the bottom.

# Statement 1
x = 5

# Statement 2
y = x + 3

# Statement 3
print(y)

Sequencing in flowcharts

Sequencing in flowcharts is indicated using arrows, flowing from one command to the next.

Sequencing in flowcharts is indicated using arrows

Selection

Selection

Selection, also known as conditional execution, allows you to choose different paths of execution based on a condition. It involves using conditional statements (such as if, else if/elif, and else) to determine which code block should be executed.

Selection in Python

In Python selection can be achieved through:

  • If/Elif/Else statements
  • Match Case Statements (Python 3.10+)

Selection in flowcharts

Selection in flowcharts is indicated using a diamond shape and multiple arrows

If Statements

Python If Statements

In Python, the if statement is used to conditionally execute a block of code based on a specified condition. It allows you to control the flow of your program based on whether a condition evaluates to True or False. Here’s the basic syntax of an if statement in Python:

if condition:
    # code block to be executed if the condition is True

Here’s an example that demonstrates the usage of if statement in Python:

x = 10

if x > 5:
    print("x is greater than 5")

In the above example, the if statement checks whether the condition x > 5 is True. If the condition is True, the code block indented below the if statement is executed.

Else Statements

You can also use an else statement to specify an alternative code block to execute when the condition in the if statement is False. Here’s an example:

x = 3

if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")

In this case, since the condition x > 5 is False, the code block under the else statement is executed.

Elif statements

You can also use elif (short for “else if”) to specify additional conditions to check. The elif statement allows you to chain multiple conditions together. Here’s an example:

x = 7

if x > 10:
    print("x is greater than 10")
elif x > 5:
    print("x is greater than 5 but not greater than 10")
else:
    print("x is not greater than 5")

In this example, the elif statement checks the second condition (x > 5) after the first condition (x > 10) is False. If both conditions are False, the code block under the else statement is executed.

It’s important to indent the code blocks consistently within the if statement. Python uses indentation to define the scope of the code blocks, so make sure to use the same level of indentation for each block of code within the if, elif, and else statements.

Switch Statement

Python Switch Statement (only works with Python 3.10+ )

Most programming language contain a swtich statement, where a number of different patterns are matched against an option. This was only added to Python in version 3.10, in the form of the match-case statements.

The match statement allows you to match a value against multiple patterns and execute corresponding code blocks based on the matching pattern. Here’s an example of how the match statement works in Python:

name = input("please input your name: ")

match name:
	case "Arnold Schwarzenegger":
		print("I'll be back")
	case "James Bond":
		print("Shaken , not stirred")
	case "Michael Corleone":
		print("I'm going to make him an offer he can't refuse")
	case "The Joker":
		print("Why so serious?")

Resources

Resources

Python Challenges Topic 2