Java If Statement with Integers

Instructor: Sergey Segal

Sergey has a Masters in Biomedical Engineering and has taught science and mathematics courses at the University of Wisconsin-Madison.

This lesson will teach you how to implement the Java if-statement with integers. We will look at a particular example of coding a program to calculate the price of a cake based on its size and ingredients.

Introduction to the If-Statement

The if-statement is a conditional statement that is used to specify code execution based on a given input. This input is usually derived from previous program output or given by the user. In other words, it is a programming construct that allows you to set conditional statements in your code.

Suppose you are creating an application to sell cakes online. The price of a given cake would be determined by its size and type. Let's imagine that there are three types of cakes: carrot, chocolate, and cream. For each of these types, there are three possible sizes: small, medium, and large. Your application would have to prompt the clients to determine which type and size they want to buy. This is where the if-statement comes in handy. Once your client enters the information, you will need to use the if-statement to determine the final price. Let's see how this can be implemented by representing our parameters with integers.

If-Statement Implementation with Integers

Continuing with our cake example, we can implement the if-statement by assigning integers to represent the different cake types:

Carrot: 0

Chocolate: 1

Cream: 2

This means that 0 corresponds to a carrot cake, 1 corresponds to a chocolate cake, and 2 corresponds to a cream cake. Translating this to Java code looks like this:

int cakeType;
int cakeSize;
double cakePrice;
if(cakeType == 0) {
  cakePrice = 10;
if(cakeType == 1) {
  cakePrice = 11;
if(cakeType == 2) {
  cakePrice = 12;

In this code, we begin by initializing the cakeType and cakeSize as integers, while cakePrice, the final price of the cake in dollars and cents, is initialized as a double. Subsequently, if the cake type is 0, then the price is 10 dollars. If the cake type is 1, then the price is 11 dollars. And, if the cake type is 2, the price is 12 dollars.

Note that you must always use the double equal signs, ==, inside an if-statement. If we were to use a single equal sign, =, it would become an assignment statement rather than a conditional statement. In addition, it is good practice to use curly brackets to denote the scope of an if-statement.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account