Skip to main content
Logo image

Discrete Math with SageMath: Learn math with open-source software

Section 1.3 Flow Control Structures

Generally, when writing programs, we often want to control the flow of execution. Flow control structures allow your code to make decisions or repeat actions based on conditions. These structures are part of Python and work the same way in Sage. There are three primary types of flow control structures:
  • Assignment statements store values in variables. These let us reuse results and build more complex expressions step by step. An assignment is performed using the = operator as seen earlier (refer to "Variable and Names" in the Sage introduction section). Note that Sage also supports compound assignment operators like +=, -=, *=, /=, and %= which combine assignment with basic arithmetic operations (addition, subtraction, multiplication, division and modulus).
  • Branching uses conditional statements like if, elif, and else to execute different blocks of code based on logical tests.
  • Loops such as for and while let us iterate over some data structures and also repeat blocks of code multiple times. This is useful when processing sequences, performing computations, or automating repetitive tasks.
These core concepts apply to almost every programming language and are fully supported in Sage through its Python foundation.

Subsection 1.3.1 Conditional Statements

The if statement lets your program execute a block of code only when a condition is true. You can add else and elif clauses to cover additional conditions.
Use indentation to define blocks of code that belong to the if, elif, or else clauses. Just like in Python, the indentation is significant and is used to define code blocks.

Subsection 1.3.2 Iteration

Iteration is a programming technique that allows us to efficiently repeat instructions with minimal syntax. The for loop assigns a value from a sequence to the loop variable and executes the loop body once for each value.
Here is a basic example of a for loop:
By default, range(n) starts at \(0\text{.}\) To specify a starting value, provide two arguments:
You can also define a step value to control the increment:

Subsubsection 1.3.2.1 List Comprehension

List comprehension is a concise way to create lists. Unlike Python’s range(), Sage’s list comprehension syntax includes the ending value in a range.
You can also filter elements using a condition. Below, we create a list containing only the cubes of even numbers:

Subsection 1.3.3 Other Flow Control Structures

In addition to if statements, Sage supports other common Python control structures:
  • The while loops: repeat a block of code while a condition remains true.
  • The break statement: terminate and exit a loop early.
  • The continue statement: to skip the rest of the loop body and jump to the next iteration.
  • The pass statement: simply do nothing (it serves as a placeholder for future code).
We’ll see examples on how to use these statements later on in the book. Here is a quick example of a while loop that print out the numbers from \(0\) to \(4\text{:}\)