CONDITIONAL STATEMENTS IN JAVA
By: Nonso Biose
Tags: Java, Learning To Code, Programming, Web Development, Software Development

As we go about our daily lives, as we try to get things done daily, we come in contact with objects of various kinds and we use these objects to carry out various task. Now one category of objects that we happen to come in contact with are devices(analog / digital), they range from your phone to your computer up to your car and the self opening door in your office or at the mall, including a television. Now you might be wondering where all this is going…… patience

In relation to what the topic is all about today, dear reader i, introduce to you the concept of CONDITIONAL STATEMENTS IN JAVA. what is a conditional statement as it relates to java programming, well simply put, its all about making decisions geared towards a desired output. Everyday we make decisions and for those decisions, we get a certain result.

In java, there are two types of decision making statements that can be used when programming a device or a software that can be run on any device such as the ones stated above. Now these statements help the device and the user of the device as well to perform an action based on a certain condition or state.

The first conditional statement is known as the IF-STATEMENT

N OTE: The if statement has three versions which are: 1. IF — STATEMENTS 2. IF / ELSE STATEMENTS 3. ELSE — IF STATEMENTS

. Using the first mentioned device, we shall see how an if statement works.

Now in java, we use if statements to check for a condition (state) of the phone and if that condition is true then an action is executed. If the condition is not true then nothing happens. As in the case of the phone, if the condition or state of our phones battery is low, then the phone tells us to charge it, if our phone storage is full, we get a notification to fix it also when there’s a software update, we are told to download….. pretty simple

The next version of the if — statement is known as the if / else statements.

. Using the second mentioned device, we shall see how an if / else statement works.

Looking at the above pic, you can see that the “if conditional” statements have been extended to perform another action in case the first is false. In the if/else statements, in addition to the first condition, we can also define another action that can be performed if the first condition is not true(“the first condition is false). The above conditions are pretty self explanatory as they happen to be evident in out daily use of computers.

The final version of the if statements is called the else if statements.

. Using the third mentioned device, we shall see how an else- if statement works.

Now in addition to the if / else, in java when you want to test other conditions you can use the else if keyword. Take the first case for example, we can see that after testing the first condition, we decided to test for two more conditions which are possible outcomes in our daily use of air conditions. So basically with the java else if statements, we have the ability to provide actions to be carried out in relation to any possible condition.

Then finally, the second type of conditional statements that are used in java is known as the “Switch Case”. Now as the name implies, it acts as a switch case that controls a light bulb. so basically there can be as many switches that act as conditions that light up various bulbs(“carry out actions”)…..see pic below

. Using the fourth mentioned device, we shall see how a switch case statement works.

Looking at the above pic, its obvious that the distance required to automatically open the door is stored in the distance variable . Following the variable declaration and initialization, the switch case follows, testing all possible distances which have their corresponding actions. So what happens there is that we store the major variable needed to carryout a specific action and then also include other cases that can perform other actions.

NOTE: you don’t always have to use a switch case to call only one specific method, it can be used to also create a scenario where several actions can be carried out depending on what the user wants.

The switch case can also, have as many cases as needed and in addition, the default statement is used to carry out an action if all the other cases don’t equal the action needed(“This is optional”)…. see pic below.

. Using the fifth mentioned device, we shall see how a switch case statement works with a default statement.

End of topic…..you have now seen how conditional statements are created in java, so go ahead and include it in your programs as you code….. see ya next time……