[Java-2C] Building a Simple Calculator: Deciding What to Do With Conditional Statements

Jan 7, 2020 · 4 min read

In the last article, we covered how to ask the user for input and storing that input properly in a variable of the correct type. It is time to ask the user to choose what arithmetic operation they want to perform and take the appropriate action based on that choice.

Making a Choice

Let’s prompt the user and store the user input into a variable:

`System.out.println(“What arithmetic operation would you like to perform?”);System.out.println(“1. Addition”);System.out.println(“2. Subtraction”);System.out.println(“3. Multiplication”);System.out.println(“4. Division”);userInput = s.nextLine();int arithmeticChoice = Integer.parseInt(userInput);`

Notice how we used an integer this time because we are only expecting the answer to be 1, 2, 3, or 4. But here’s another 60,000 dollar question: how can we make different choices and do different things based on certain criteria?

If Statements

In Java, we use if-statements/conditional statements to deal with conditions. In general, if-statements work like so:

`if (condition1 is true) {  perform action1;} else if (condition2 is true) {  perform action2;} else {  perform action3;}`

Translation to English:

`If condition1 is true, then we perform action1.If condition1 is false but condition2 is true, then we perform action2.If both condition1 and condition2 are false, then we perform action3.`

Seems straightforward, right? However, the tricky part that you must understand is the following:

`If condition1 and condition2 are both true, then we perform condition1 and that's it.`

Why is that? Because we chained `condition1` and `condition2` together using `else if`. If we want both `action1` and `action2` to be performed when both `condition1` and `condition2` are true, we must break up `condition1` and `condition2`:

`if (condition1 is true) {  perform action1;}if (condition2 is true) {  perform action2;} else {  perform action3;}`

Take note: the change above will also impact how and when `action3` is performed.

Let’s take a look at an example.

To Eat, or Not to Drink? (You what?)

It is most likely common sense that if we are hungry, we eat. If we aren’t hungry but are thirsty, we drink. Otherwise, we carry on doing what we’re doing. If this is not common sense to you, then there are bigger problems than not knowing how to program. We can transform this common sense into some pseudocode:

`if (hungry) {  eat;} else if (thirsty) {  drink;} else {  carry on;}`

Note that there is a difference between “If we are hungry, we eat. If we aren’t hungry but are thirsty, we drink. Otherwise, carry on” and “If we are hungry, we eat. If we are thirsty, we drink. Otherwise, carry on”. Let’s write the pseudocode for the latter since the difference is more obvious in code:

`if (hungry) {  eat;}if (thirsty) {  drink;} else {  carry on;}`

In the first piece of pseudocode, even if we are both hungry and thirsty, we could only eat because in the same conditional chain, once a condition is fulfilled, we immediately exit the conditional chain and all the conditions below it are discarded. In the second piece of pseudocode, however, we could both eat and drink in one go because they are two separate unrelated conditions.

In general, if you have a group of conditions but you only ever want a single one to trigger at once, chain them together. If you are fine with more than one of them being triggered in one go, separate them. In our case we can do either because the user can only type in 1, 2, 3, or 4. The number cannot just change halfway through the program’s execution. But for good practice, let’s chain them together regardless because the principle stands that we only want one of four conditions to be fulfilled. The pseudocode goes:

`if (arithmeticChoice is 1) {  do addition;} else if (arithmeticChoice is 2) {  do subtraction;} else if (arithmeticChoice is 3) {  do multiplication;} else if (arithmeticChoice is 4) {  do division;} else {  print “Invalid input! Aborting…”;  end program;}`

Yes, we are ending the program if the user doesn’t give us the proper answer. You may think the better thing to do is to loop back and ask the user again, and I agree. We will come back to this in another article.

Quick progress check! At this point, your program should look something like this (`/* */` is a multiline comment block to comment out our pseudocode. Recall all comments are ignored by the compiler.):

Great! We are now able to make choices based on the user’s input. You may be asking, however, how can we perform those arithmetic operations with only one number? Keep in mind our program has only asked the user for the first number and the operation they want to perform so far. We currently have no knowledge of what the second number is. We will end this article on this note and take some time to think about what the next steps are. In the next article, we will get to the solution to this problem and carry on with our simple calculator.

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

Medium sent you an email at to complete your subscription.

Written by

Jack Boyuan Xu

Co-founder & Tech Lead @ EthSign. Java, Blockchain, and iOS Teaching Assistant @ USC Viterbi School of Engineering.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Written by

Jack Boyuan Xu

Co-founder & Tech Lead @ EthSign. Java, Blockchain, and iOS Teaching Assistant @ USC Viterbi School of Engineering.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Red Black Tree : An Intuitive Approach

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app