Topics Covered
- if then
- if then else
- switch statements
Overview
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javatutorial.example; /* * This is demo program * in using if-then conditional statement */ public class IfConditionDemo { public static void main(String[] args) { int x = 6; if(x>5){ System.out.println("Threshold breach"); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javatutorial.example; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; /* * This is demo program * in using if-then conditional statement */ public class IfConditionDemo { public static void main(String[] args) throws IOException { // generate random number 1-100 int min = 1; int max = 100; Random random = new Random(); boolean repeat = true; while(repeat){ int randInt = random.nextInt((max - min) + 1) + min; //print a random number generated System.out.println("Random Number:"+randInt); // ask the user if s/he wants to generate another random number System.out.print("Do you want to generate another (y/n)?"); // read the user input using bufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strVal = br.readLine(); System.out.println(strVal); // check the user input if(strVal.equals("n")||strVal.equals("N")){ repeat=false; } } } } |
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
1 2 3 4 5 6 |
if(x>=5){ System.out.println("Threshold Breached"); } else{ System.out.println("Still in range"); } |
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.