Conditional Statements

Selection

Computer programs require to make decisions and execute different sets of instructions depending on the value variables.  For example, a program will take a different action if an exam mark is a pass than if the mark is a fail.

Selection Operators

Conditional Statements (IF Statements)

Allowing programs to make decisions is an important part of programming. ‘If statements’ are used to let a program select an action depending upon the value of variables held in the program.

Here is an example of how an if statement can be used to select what happens next in a section of code designed to:

  1. Check if the user is old enough to vote
  2. Check if the user qualifies for a discount

Complex Conditional Statements (IF Statements)

A complex condition depends upon two or more conditional statements being true or false.  Programming languages have logical operators  –   AND, OR, NOT – that can be used to implement complex conditions in program instruction.

IF-ELIF Statements

Using multiply IF statements would mean that the program will check every IF statement condition.  Even if the first condition is met, the program will still check all conditions.  This will take up more processing time that is not required.  To overcome this, we can use an IF-ElIF statement.  With an elif statement – once a condition has been met, the program will move on.

Structure Diagram – If Statement Example