If…Then…ElseIf…Else
In some problems there are not just two different outcomes but more than two. If this is the case, then a simple If…Then…Else structure will not work. In these situations an If…Then…ElseIf…Else might be used. In this type of structure there can be more than just one Boolean condition and each is checked in sequence. Once a Boolean expression is met (the value is true), then the specified section of code for that Boolean expression is executed. Once executed, all other conditions are skipped over and the flow of logic goes right down to the bottom of the structure. It is important to remember that one and only one section of code can be executed. Even if several of the Boolean conditions happen to be met, the first and only the first one will be evaluated and run.
The structure can contain many ElseIfs, as many as the programmer needs. Another optional piece of the structure is the, “Else”. If none of the above Boolean conditions are met, the programmer might want a section of code to be executed that is a catch all. If this is the case, then the code is placed in the else section. Any code in the else section is run if and only if none of the Boolean expressions were met. It should also be noted that there is no Boolean condition associated with the else. That is because it is run only if all the above Boolean conditions are not met. The If…Then…ElseIf…Else statement (in most computer programming languages) takes the generic form of:
When you approach a street light, there are not just 2 choices there are actually 3 (or maybe more). The following example will tell the driver (or the self-driving car), what to do as they approach a stop light. The driver will need to know what to do if the light is red, yellow or green. If the light is red, the driver needs to stop. If the light is yellow, the driver needs to slow down. If the light is green, the driver needs to go. If the light is not red, yellow or green, the driver needs to do something else. The following code shows how this might be done:
Top-Down Design for If…Then…ElseIf…Else statement

Flowchart for If…Then…ElseIf…Else statement

Pseudocode for If…Then…ElseIf…Else statement
Code for If…Then…ElseIf…Else statement
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program checks a traffic light
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11int main() {
12 // this function checks a traffic light
13 char lightColor[10];
14
15 // input
16 printf("Enter the color of the traffic light: ");
17 scanf("%s", lightColor);
18
19 // process and output
20 if (strcmp(lightColor, "red") == 0) {
21 printf("Stop!\n");
22 } else if (strcmp(lightColor, "yellow") == 0) {
23 printf("Slow down!\n");
24 } else if (strcmp(lightColor, "green") == 0) {
25 printf("Go!\n");
26 } else {
27 printf("Not a valid color.\n");
28 }
29
30 printf("\nDone.\n");
31 return 0;
32}
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program checks a traffic light
6
7#include <iostream>
8#include <string>
9
10int main() {
11 // this function checks a traffic light
12 std::string lightColor;
13
14 // input
15 std::cout << "Enter the color of the traffic light: ";
16 std::cin >> lightColor;
17
18 // process and output
19 if (lightColor == "red") {
20 std::cout << "Stop!" << std::endl;
21 } else if (lightColor == "yellow") {
22 std::cout << "Slow down!" << std::endl;
23 } else if (lightColor == "green") {
24 std::cout << "Go!" << std::endl;
25 } else {
26 std::cout << "Not a valid color." << std::endl;
27 }
28
29 std::cout << std::endl
30 << "Done." << std::endl;
31 return 0;
32}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks a traffic light
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function checks a traffic light
14 string lightColor;
15
16 // input
17 Console.Write("Enter the color of the traffic light: ");
18 lightColor = Console.ReadLine();
19
20 // process and output
21 if (lightColor == "red") {
22 Console.WriteLine("Stop!");
23 } else if (lightColor == "yellow") {
24 Console.WriteLine("Slow down!");
25 } else if (lightColor == "green") {
26 Console.WriteLine("Go!");
27 } else {
28 Console.WriteLine("Not a valid color.");
29 }
30
31 Console.WriteLine("\nDone.");
32 }
33}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program checks a traffic light
5 */
6//nolint:gocritic
7package main
8
9import "fmt"
10
11func main() {
12 // this function checks a traffic light
13 var lightColor string
14
15 // input
16 fmt.Print("Enter the color of the traffic light: ")
17 fmt.Scan(&lightColor)
18
19 // process and output
20 if lightColor == "red" {
21 fmt.Println("Stop!")
22 } else if lightColor == "yellow" {
23 fmt.Println("Slow down!")
24 } else if lightColor == "green" {
25 fmt.Println("Go!")
26 } else {
27 fmt.Println("Not a valid color.")
28 }
29
30 fmt.Println("\nDone.")
31}
1/*
2 * This program checks a traffic light
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9import java.util.Scanner;
10
11public class Main {
12 public static void main(String[] args) {
13 // this function checks a traffic light
14 String lightColor;
15
16 // input
17 Scanner scanner = new Scanner(System.in);
18 System.out.print("Enter the color of the traffic light: ");
19 lightColor = scanner.nextLine();
20
21 // process and output
22 if (lightColor.equals("red")) {
23 System.out.println("Stop!");
24 } else if (lightColor.equals("yellow")) {
25 System.out.println("Slow down!");
26 } else if (lightColor.equals("green")) {
27 System.out.println("Go!");
28 } else {
29 System.out.println("Not a valid color.");
30 }
31
32 System.out.println("\nDone.");
33 }
34}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks a traffic light
4*/
5
6const prompt = require('prompt-sync')()
7
8// input
9const lightColor = prompt('Enter the color of the traffic light: ')
10
11// process and output
12if (lightColor === 'red') {
13 console.log('Stop!')
14} else if (lightColor === 'yellow') {
15 console.log('Slow down!')
16} else if (lightColor === 'green') {
17 console.log('Go!')
18} else {
19 console.log('Not a valid color.')
20}
21
22console.log('\nDone.')
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module checks a traffic light
6"""
7
8
9def main() -> None:
10 """The main() function checks a traffic light, returns None."""
11
12 # input
13 light_color = input("Enter the color of the traffic light: ")
14
15 # process and output
16 if light_color == "red":
17 print("Stop!")
18 elif light_color == "yellow":
19 print("Slow down!")
20 elif light_color == "green":
21 print("Go!")
22 else:
23 print("Not a valid color.")
24
25 print("\nDone.")
26
27
28if __name__ == "__main__":
29 main()
Example Output
