Topics Covered

On this section of my java tutorial series we will be covering topics regarding the different conditional statements available according to java specification. The following topics will be covered:

  • if then
  • if then else
  • switch statements

Overview

For every programming language that i have handled, there will always be a conditional statements. Java is no different, it uses the condition statements to control the flow of the program. This is very important, since at some point we have to satisfy conditions in order to proceed further on our code. Like for instance an input of yes or no from the console will decide if the program will continue or would it be terminated. This is handled by using conditional statements.

Let’s tackle this topic thoroughly.

If then statement

The if-then condition is the most basic of  the conditional statements in java. This tells the program to execute a code block if the condition has been satisfied or it returns true.

Let’s take an example

Initially we have declared x = 6. So the condition x > 5 will evaluate to true thus the code block   System.out.println("Threshold breach"); will get executed. This code block basically just prints to the console “Threshold breached”. What if we have declared x to have a value of 4, will the “Threshold breached” be printed out on the console. The answer would be no, because the condition x>5 will return false.

Let’s take a more complicated example for you to better understand it.

 

Basically the above example is just to generate a random random number and depending on the user input, it keeps generating if the user input is “Y” or “y”. Initially we have set the boolean variable repeat to true and we used that variable in our for loop. Since the we have set the repeat variable to true, the loop will get executed. The code block in generating a random number will get in first and then after we print out the random number, we asked the user if s/she wants to get random number. This is accomplished by calling the BufferedReader class from the java API. Maybe it’s not yet very clear to you the different packages available in java, however as we go along I will introduce it one by one. So chill out. Moving on, we use the if-then condition to evaluate the user input. If the user input is “n” or “N” as we have declared inside our if condition, the variable repeat is assigned a false value. This will invalidate the while loop condition while(repeat).

if-then else

The if-else just purely evaluate a certain condition and execute a code block, the if-then else provides another path of execution.

Let’s tackle first a simple if-else condition. Basically if the if condition returns true then the else code block will get executed

The above example just demonstrated on how if-else condition works. Any numbers that satisfy the condition x>=5 will print on the console that “Threshold Breached” else a message “Still in range” will get printed. A little exercise, if x is 10, what message will get printed? It will print “Threshold breached”. But what if x is 1? The console will print “Still in range”. It’s very easy, isn’t it?

Most of the time the if-else condition is all just you need, however what if from our example above we need to add another condition that if x is < 3 , message is “Less than the threshold”. How could we accomplished that? We can use if, else if, and then else. Let’s take a look on the implementation of our example.