If…Then…Else
In the previous section we looked at the If…Then statement that is used for making a decision. When using the If…Then statement a section of code is performed only if the Boolean statement is true. If the Boolean statement is false, nothing happens. In some situations, if the Boolean statement is false and the section of code is not performed, you would like an alternative piece of code to be performed instead. In this case an optional Else statement can be used. The If…Then…Else statement (in most computer programming languages), takes the generic form of:
In the previous example of asking the user how many students were in the class, you might have noticed that the user was given no feedback if there were 30 or fewer students. This can add confusion for the user; they might be unsure if the program worked correctly.
An example of what this would look like using an If .. Then .. Else statement is shown below:
Top-Down Design for If…Then…Else statement

Flowchart for If…Then…Else statement

Pseudocode for If…Then…Else statement
Code for If…Then…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 if there is over 30 students
6
7#include <stdio.h>
8
9int main() {
10 // this function checks if there is over 30 students
11 const int MAX_STUDENT_NUMBER = 30;
12 int numberOfStudents;
13
14 // input
15 printf("Enter the number of students: ");
16 scanf("%d", &numberOfStudents);
17
18 // process and output
19 if (numberOfStudents > MAX_STUDENT_NUMBER) {
20 printf("Too many students!\n");
21 } else {
22 printf("Not too many students.\n");
23 }
24
25 printf("\nDone.\n");
26 return 0;
27}
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program checks if there is over 30 students
6
7#include <iostream>
8
9int main() {
10 // this function checks if there is over 30 students
11 const int MAX_STUDENT_NUMBER = 30;
12 int numberOfStudents;
13
14 // input
15 std::cout << "Enter the number of students: ";
16 std::cin >> numberOfStudents;
17
18 // process and output
19 if (numberOfStudents > MAX_STUDENT_NUMBER) {
20 std::cout << "Too many students!\n";
21 } else {
22 std::cout << "Not too many students.\n";
23 }
24
25 std::cout << "\nDone." << std::endl;
26}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks if there is over 30 students
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function checks if there is over 30 students
14 const int MAX_STUDENT_NUMBER = 30;
15 int numberOfStudents;
16
17 // input
18 Console.Write("Enter the number of students: ");
19 numberOfStudents = Convert.ToInt32(Console.ReadLine());
20
21 // process and output
22 if (numberOfStudents > MAX_STUDENT_NUMBER) {
23 Console.WriteLine("Too many students!");
24 } else {
25 Console.WriteLine("Not too many students.");
26 }
27
28 Console.WriteLine("\nDone.");
29 }
30}
1/**
2 * Created by: Mr. Coxall
3 * Created on: Sep 2020
4 * This program checks if there is over 30 students
5 */
6
7package main
8
9import "fmt"
10
11func main() {
12 // this function checks if there is over 30 students
13 const maxStudentNumber int = 30
14 var numberOfStudents int
15
16 // input
17 fmt.Print("Enter the number of students: ")
18 fmt.Scan(&numberOfStudents)
19
20 // process and output
21 if numberOfStudents > maxStudentNumber {
22 fmt.Println("Too many students!")
23 } else {
24 fmt.Println("Not too many students.")
25 }
26
27 fmt.Println("\nDone.")
28}
1/*
2 * This program checks if there is over 30 students
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 if there is over 30 students
14 final int MAX_STUDENT_NUMBER = 30;
15 int numberOfStudents;
16
17 // input
18 Scanner input = new Scanner(System.in);
19 System.out.print("Enter the number of students: ");
20 numberOfStudents = input.nextInt();
21
22 // process and output
23 if (numberOfStudents > MAX_STUDENT_NUMBER) {
24 System.out.println("Too many students!");
25 } else {
26 System.out.println("Not too many students.");
27 }
28
29 System.out.println("\nDone.");
30 }
31}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program checks if there is over 30 students
4*/
5
6const prompt = require('prompt-sync')()
7const MAX_STUDENT_NUMBER = 30
8
9// input
10const numberOfStudents = parseInt(prompt('Enter the number of students: '))
11
12// process and output
13if (numberOfStudents > MAX_STUDENT_NUMBER) {
14 console.log("Too many students!")
15} else {
16 console.log("Not too many students.")
17}
18
19console.log("\nDone.")
constants.py
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module holds constants
6"""
7
8# constant definition
9MAX_STUDENT_NUMBER = 30
main.py
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module checks if there is over 30 students
6"""
7
8
9from constants import MAX_STUDENT_NUMBER
10
11
12def main() -> None:
13 """The main() function checks if over 30 students, returns None."""
14
15 # input
16 number_of_students = int(input("Enter the number of students: "))
17
18 # process and output
19 if number_of_students > MAX_STUDENT_NUMBER:
20 print("Too many students!")
21 else:
22 print("Not too many students.")
23
24 print("\nDone.")
25
26
27if __name__ == "__main__":
28 main()
Example Output
