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

Jack Boyuan Xu
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:

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:

Translation to English:

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

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:

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:

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:

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:

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.

Sign up for Top 10 Stories

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.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Jack Boyuan Xu

Written by

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.

Jack Boyuan Xu

Written by

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.

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