Control flow
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++:
==
)*!=
)<
)<=
)>
)>=
)**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 (=
) *
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."
20
You are legally allowed to leave.