Articles → Java → Conditional Statements In Java

Conditional Statements In Java






What Are Conditional Statements?




  1. If there is no client call in the evening (Condition) I will go for movie (Action).
  2. If you want to play games (Condition) then we will go to gaming zone (Action) otherwise we will go home (Action if condition is not met).
  3. If air tickets are cheap (Condition) then I will go by flight (Action) or if train tickets are available (Condition) then I will go by train (Action) else I will hire a cab (Action if both the Conditions are not met).



Types Of Conditional Statements In Java




  1. If statement – executes some code if certain condition is true.
  2. If..else statement – Execute some code if condition is true or execute some other code if condition is false.
  3. If..elseif..else statement – Executes some code based on which condition is true.
  4. Switch statement – Similar to if..elseif..else statement switch statement executes some code based on which condition is true. There is a difference between if..else and switch statement which will be covered in last section.

If Statement




if (condition) {
    code to be executed
    if condition is true;
}




Boolean is_client_call = true;

if (is_client_call == true) {
    System.out.println("I will go for movie");
}





If..Else Statement




if (condition) {
    code to be executed
    if condition is true;
} else {
    code to be executed
    if condition is false;
}




Boolean wish_to_play_game = true;
if (wish_to_play_game == true) {
    System.out.println("Go to gaming zone");
} else {
    System.out.println("Go Home");
}





If..Elseif..Else Statement




if (condition) {
    code to be executed
    if condition is true;
}
elseif(condition) {
    code to be executed
    if condition is true;
} else {
    code to be executed
    if condition is false;
}




Boolean cheap_air_tickets = false;
Boolean train_tickets_available = true;

if (cheap_air_tickets == true) {
    System.out.println("Go by flight");
} else if (train_tickets_available == true) {
    System.out.println("Go by train");
} else {
    System.out.println("Hire a cab");
}





Switch Statement




switch (n) {
    case label1:
        code to be executed
        if n = label1;
        break;
    case label2:
        code to be executed
        if n = label2;
        break;
    case label3:
        code to be executed
        if n = label3;
        break;
        ...
    default:
        code to be executed
        if n is different from all labels;
}




String mode = "FLIGHT";
switch (mode) {
    case "FLIGHT":
        System.out.println("I will go by flight");
        break;
    case "CAB":
        System.out.println("I will go by Cab");
        break;
    default:
        System.out.println("I will go by Train");
}





Difference Between If..Else And Switch Statement




  1. In case of switch statement we cannot apply logical operators like less than, greater then etc.
  2. Switch statement can only contains constant values.
  3. Switch statement is quicker in execution as compared to if..else.



Posted By  -  Karan Gupta
 
Posted On  -  Sunday, September 18, 2016

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250