Control flow

4.1 - If statement


The if statement is a conditional statement which takes a condition and only executes the code inside it if the condition is true. The if statement has this form:

if (condition) {
    // code that executes when the condition is true
}

For example, this code snippet checks if the variable age is 18 or above, if it is true then it prints out "You are legally allowed to drink."

if (age >= 18) {
    std::cout << "You are legally allowed to drink." << std::endl;
}

These are the comparison operators in C++:

  • Equality (==)*
  • Inequality (!=)
  • Less than (<)
  • Less than or equal (<=)
  • Greater than (>)
  • Greater than or equal (>=)

**Note that the equality operator has two equal signs (==). It's a common error to mistake it with the assignment operator which has one equal sign (=) *

Task

Your task is to create a program which takes how many minutes the teacher is late, and if it is 15 minutes or above, prints out "You are legally allowed to leave."

Sample input

20

Sample output

You are legally allowed to leave.
#include <iostream> int main() { // Start your code here! }