Session 10
Conditionals

Purpose & Structure

Conditionals, or if-statements, are used to evaluate truth conditions. Truth conditions were introduced in the previous session in connection with while- and repeat-loops. With loops, truth conditions serve as stop conditions. With if-statements, truth conditions serve to branch the procedural execution of code. The syntactic frame of conditionals is pretty close to natural language use: If something is the case do this, else do that. The 'something is the case'-part is a condition which can be true or false. Depending on the truth value of the condition, the 'do this' or the 'do that'-part is executed. This is meant by 'branching': You provide several branches and you define conditions to tell the script which branch to take. Any if-statement ends with endif to tell the script where to continue with the normal procedural execution.

if condition is true code block else code block endif

The else section is optional:

if condition is true code block endif

In this case, the condition is evaluated; if it is true the code block is executed and the script continues after endif; if it is false the code block is ignored and the script continues after endif.

With the optional elsif, it is possible to evaluate more than one condition:

if first condition is true code block elsif second condition is true code block elsif third condition is true code block else code block endif

The order of conditions
does matter!

Conditions are evaluated from top to bottom. The first condition that is true triggers the following code block to be executed, then the script jumps to endif and continues. If all conditions are false the code block following else is executed (if it is available).

That's all there is to the syntax of if-statements, so we are ready for a legit example:

# query minimum of selected pitch object pitch_floor = Get minimum: 0, 0, "Hertz", "Parabolic" # do something if minimum pitch is less than 130 Hz if pitch_floor < 130 speaker$ = "male" endif

The condition pitch_floor < 130 is evaluated. If it is true, the code block (only one statement in the example) is executed. If it is false, nothing happens and the script continues after endif. Let's add female voices:

pitch_floor = Get minimum: 0, 0, "Hertz", "Parabolic" if pitch_floor < 130 speaker$ = "male" elsif pitch_floor < 220 speaker$ = "female" endif

Suppose pitch_floor is 100, which is less than 130 as well as less than 220. What's the value of speaker$ at the end of the if-statement? It's male, because, as I said before, Praat aborts evaluation and exits the if-statement (i.e. jumps to endif) as soon as one condition is true. Obviously, the first true condition is 100 < 130.

If you use elsif-sections you have to pay attention to the order of conditions!

If pitch_floor is 175, speaker$ has the value female, because the first condition (175 < 130) is false but the second (175 < 220) is true. If pitch_floor is 245, speaker$ is undefined, because both conditions are false. So, let's add kids:

if pitch_floor < 130 speaker$ = "male" elsif pitch_floor < 220 speaker$ = "female" else speaker$ = "child" endif

Now, speaker$ contains child whenever the pitch_floor is greater than 220.

A summary of the few rules concerning if-statements:

  • A minimal if-statement consists of the reserved word if, a condition, a code block, and the reserved word endif
  • if and endif are allowed only once per if-statement
  • any number (including 0) of elsif sections are permitted
  • 0 or 1 else section is allowed

You see, the syntactic structure of conditionals is pretty simple. Nonetheless, they are important and powerful tools. To make the most of conditionals it is essential to understand and exploit the potential of truth conditions, that's why we'll take a closer look at truth conditions in the next section. Of course, this topic is also relevant for while- and repeat loops.

© Jörg Mayer

Last modified: