Now that we've gotten a calculator that can take in any numbers that we choose, let's try making it accept different operations. Taking in user input changes the behavior of our program, but it still does basically the same thing: it adds two numbers together. We're not limited to just that, though. We can make our program do entirely different things based on what the user enters (for example, we can add if the user enters a plus sign and subtract if a user enters a negative sign). To do this, we'll use if statements.
If statements are basically a way to run certain parts of our code based on some condition. For example, if we want to make our program do something different based on whether the user enters a plus sign or a minus sign, we can use if statements. Here's a little example of them:
Conditions can be pretty complex, but to keep things simply, we'll start off with comparisons. With comparisons, we can compare some value to another (like checking if a variable is equal to one, for example). Here's an example of it:
Here's a little list of all the comparison operators that you can use:
===- equals operator to check if two things are the same.
!==- not equals operator to check if two things are different.
>- greater than operator.
<- less than operator.
>=- greater than or equal to operator.
<=- less than or equal to operator.
You can use any of the operations in your if statements, but we're dealing with symbols, so try sticking with the equals operator for now.
Before we can continue, we also need to take a look at elses and else ifs. If statements let us run code under certain conditions, and else statements let us run code if our if statement didn't run. Here's a little example:
In this example, we'll see
Condition 1 in our console. If we set
a to 1, then we'd see
Condition 2, and if we set it to 15, then we'd see
Invalid A. These statements can be chained together, but they need to always look like:
- An if statement.
- Zero or more else if statements.
- Zero or one else statements.
For our calculator, you can use these to check which operation the user entered, and run certain code based on that operation. Try adding support for
/, and if you want, try making a little error message if the user entered an invalid operation. Good luck!