Conditionals and logic

2.3. Conditionals and logic

Conditionals are central in programming, but also central in everyday life. Let’s start simply with ticket prices on the bus. If the age is under 18, the price will be 3 dollars, and half the price of an adult ticket. We can describe this with the following pseudocode:

if age < 18 then price = 3
if not then price = 62

In Python this program will look like this:

if age < 18:
    price = 3
else:
    price = 62

Now we are going to look at examples where conditionals can be used to illustrate concepts in your discipline. We are using simple examples here, but it can of course be applied to more complex problems. You can choose the tab with your discipline.

A good way to work with logic and programming is to concentrate on how a problem can be solved, rather than emphasizing on the syntax (that is, how we write the code).

To structure a program code, you may want to use a flow chart. A flow chart provides both information about the mathematical premises and an overview of the structure of the program.

An example is the following flow chart, which shows what possible outcomes we can have when solving a quadratic equation:

Quadratic equation flowchart

In Python, code belonging to a condition is indented. We can therefore say that each individual condition in the flow chart represents one indent.

This logic can be explored using programming puzzles.

In a programming puzzle, all the lines of code are given as “puzzle pieces”. The task is to sort these code parts in the correct order and with the correct structure. Such puzzles are also called “Parsons problems”. The puzzles can be generated digitally from the following webpage.

First solve this puzzle about ticket prices. Then use the flow chart above to solve this programming puzzle. The program will find out how many solutions we have given the coefficients a, b and c in a quadratic equation.

A good way to work with logic and programming is to concentrate on how a problem can be solved, rather than emphasizing on the syntax (that is, how we write the code).

To structure a program code, you may want to use a flow chart. A flow chart provides both information about the biological premises and an overview of the structure of the program.

An example is the following flow chart, which shows possible outcomes and calculations when computing the fractions of individuals with certain genotypes in a population in Hardy-Weinberg equilibrium.

Hardy-Weinberg flowchart

This logic can be explored using programming puzzles.

In a programming puzzle, all the lines of code are given as “puzzle pieces”. The task is to sort these code parts in the correct order and with the correct structure. Such puzzles are also called “Parsons problems”. The puzzles can be generated digitally from the following webpage.

First solve this puzzle about ticket prices. Then use the flow chart above to solve this programming puzzle. The program will find out how many individuals there are with the genotypes AA, Aa and aa.

A good way to work with logic and programming is to concentrate on how a problem can be solved, rather than emphasizing on the syntax (that is, how we write the code).

To structure a program code, you may want to use a flow chart. A flow chart provides both information about the chemical premises and an overview of the structure of the program.

An example is the following flow chart, which shows the relation between the pH and the acidity of the solution.

pH flowchart

While this is a trivial example, implementing such a program is not always straightforward: Do you need the pH to be exactly 7 (which is almost never the case) to call it neutral? Your students need to actively think about this before making a program. (Hint: You should use a “tolerance”, that is define an interval [7-tol, 7+tol] where a solution is neutral).

The flow chart logic can be explored using programming puzzles.

In a programming puzzle, all the lines of code are given as “puzzle pieces”. The task is to sort these code parts in the correct order and with the correct structure. Such puzzles are also called “Parsons problems”. The puzzles can be generated digitally from the following webpage.

First solve this puzzle about ticket prices. Then use the flow chart above to solve this programming puzzle.

We have now seen that we use if-tests to program conditionals in Python. An example is this program which determines whether a number is odd or even:

number = 18

if number%2 == 0:
    print("The number is even")
else:
    print("The number is odd")
The number is even

The operator % in the program above finds the remainder after an integer division of the variable number divided by 2. If the remainder is zero, the number is an even number. Note that the executed code is indented under the different conditions. Also notice the double equals sign. It’s easy to forget!

When we assign a value to a variable, we use simple equal signs. When we are going to check if something is zero, however, we must use double equal signs.

We can add more terms with the command elif, which means “else if”. First the if statement is evaluated, then all the elif statements in turn. If none of these are true, another is performed. The first condition that is true ends the row with if-elif-else. The code word else does not take any conditions and is executed if none of the above conditions are met.

number = 0

if number > 0:
    print("The number is positive")
elif number < 0:
    print("The number is negative")
else:
    print("The number is zero")
The number is zero

Study the example above and explain how the syntax for conditionals is structured. Then create a program that checks if a given number exists in the interval [0, 10].

Discussion

  • Flowcharts and programming puzzles are two different ways of working with programming and computational thinking. How can you use this in your teaching?

  • When students are to program conditionals they must think through different outcomes and structure this as a yes/no question. How does this train students in computational thinking and problem solving?