成人快手

The ELSE IF instruction

The ELSE IF instruction allows there to be more than two paths through an . Any number of ELSE IF can be added to an algorithm. It is used along with other instructions:

  • IF represents a question
  • THEN points to what to do if the answer to the question is true
  • ELSE IF represents another question
  • THEN points to what to do if the answer to that question is true
  • ELSE IF represents another question
  • THEN points to what to do if the answer to that question is true
  • ELSE points to what to do if the answer to the question is false

Using this method, the bus fare algorithm could be improved like this:

  1. ask how old you are
  2. IF you are under 5, THEN pay no fare
  3. ELSE IF you are under 16, THEN pay half fare
  4. ELSE IF you are an OAP, THEN pay no fare
  5. ELSE pay full fare

As this algorithm uses ELSE IF statements, a 14 year old would stop at step 3. However, if the algorithm was made up of three different IF statements, the same 14 year old would have to check to see if they were an OAP (step 4) even though they had already found the correct option for themselves.

ELSE IF is covered in more detail in Selection in programming.