# 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:

IF (Boolean expression) THEN
statements to be performed …
ELSE
alternate statements to be performed …
ENDIF

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:

## Pseudocode for If…Then…Else statement¶

GET number_of_students
IF (number_of_students > 30) THEN
SHOW “Too many students!”
ELSE
SHOW “Not too many students.”
ENDIF

## Code for If…Then…Else statement¶

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27``` ```// Copyright (c) 2020 Mr. Coxall All rights reserved. // // Created by: Mr. Coxall // Created on: Sep 2020 // This program checks if there is over 30 students #include int main() { // this function checks if there is over 30 students const int MAX_STUDENT_NUMBER = 30; int numberOfStudents; // input printf("Enter the number of students: "); scanf("%d", &numberOfStudents); // process and output if (numberOfStudents > MAX_STUDENT_NUMBER) { printf("Too many students!\n"); } else { printf("Not too many students.\n"); } printf("\nDone.\n"); return 0; } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```// Copyright (c) 2020 Mr. Coxall All rights reserved. // // Created by: Mr. Coxall // Created on: Sep 2020 // This program checks if there is over 30 students #include int main() { // this function checks if there is over 30 students const int MAX_STUDENT_NUMBER = 30; int numberOfStudents; // input std::cout << "Enter the number of students: "; std::cin >> numberOfStudents; // process and output if (numberOfStudents > MAX_STUDENT_NUMBER) { std::cout << "Too many students!\n"; } else { std::cout << "Not too many students.\n"; } std::cout << "\nDone." << std::endl; } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```/* Created by: Mr. Coxall * Created on: Sep 2020 * This program checks if there is over 30 students */ using System; /* * The Program class */ class Program { static void Main() { // this function checks if there is over 30 students const int MAX_STUDENT_NUMBER = 30; int numberOfStudents; // input Console.Write("Enter the number of students: "); numberOfStudents = Convert.ToInt32(Console.ReadLine()); // process and output if (numberOfStudents > MAX_STUDENT_NUMBER) { Console.WriteLine("Too many students!"); } else { Console.WriteLine("Not too many students."); } Console.WriteLine("\nDone."); } } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28``` ```/** * Created by: Mr. Coxall * Created on: Sep 2020 * This program checks if there is over 30 students */ package main import "fmt" func main() { // this function checks if there is over 30 students const maxStudentNumber int = 30 var numberOfStudents int // input fmt.Print("Enter the number of students: ") fmt.Scan(&numberOfStudents) // process and output if numberOfStudents > maxStudentNumber { fmt.Println("Too many students!") } else { fmt.Println("Not too many students.") } fmt.Println("\nDone.") } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31``` ```/* * This program checks if there is over 30 students * * @author Mr Coxall * @version 1.0 * @since 2020-09-01 */ import java.util.Scanner; public class Main { public static void main(String[] args) { // this function checks if there is over 30 students final int MAX_STUDENT_NUMBER = 30; int numberOfStudents; // input Scanner input = new Scanner(System.in); System.out.print("Enter the number of students: "); numberOfStudents = input.nextInt(); // process and output if (numberOfStudents > MAX_STUDENT_NUMBER) { System.out.println("Too many students!"); } else { System.out.println("Not too many students."); } System.out.println("\nDone."); } } ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``` ```/* Created by: Mr. Coxall * Created on: Sep 2020 * This program checks if there is over 30 students */ const prompt = require('prompt-sync')() const MAX_STUDENT_NUMBER = 30 // input const numberOfStudents = parseInt(prompt('Enter the number of students: ')) // process and output if (numberOfStudents > MAX_STUDENT_NUMBER) { console.log("Too many students!") } else { console.log("Not too many students.") } console.log("\nDone.") ```

constants.py

 ```1 2 3 4 5 6 7 8 9``` ```#!/usr/bin/env python3 """ Created by: Mr. Coxall Created on: Sep 2020 This module holds constants """ # constant definition MAX_STUDENT_NUMBER = 30 ```

main.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28``` ```#!/usr/bin/env python3 """ Created by: Mr. Coxall Created on: Sep 2020 This module checks if there is over 30 students """ from constants import MAX_STUDENT_NUMBER def main() -> None: """The main() function checks if over 30 students, returns None.""" # input number_of_students = int(input("Enter the number of students: ")) # process and output if number_of_students > MAX_STUDENT_NUMBER: print("Too many students!") else: print("Not too many students.") print("\nDone.") if __name__ == "__main__": main() ```