Ad Code

Responsive Advertisement

Conditional Statement (If, Switch)

 

CONDITIONAL STATEMENTS

The conditional statements in Java include If statement, switch statement, Loops statements. The loops are further classified as for statement, while, do while and for- each statements.



If conditional statement

The syntax of the If loop is:

if (condition 1)

{

Statement 1

}

else if (condition 2)

{

Statement 2

}

else

{

Statement 3

}

In the above syntax, whenever the condition 1 is satisfied, the statement 1 gets executed. If the condition 2 gets satisfied, the statement 2 gets executed. If both the condition fails, statement 3 gets executed.

Eg) Consider a vending machine that lends KFC Big 12 bucket when the user lends money greater than 700. Try writing out this code using If conditional statement in Java.

import java.util.Scanner;

class
java {
   
public static void main(String args[]) {
        System.
out.println("Hi, Welcome to KFC");
       
System.out.println("Enter the amount you are ready to pay");
        int
money;
       
Scanner input=new Scanner(System.in);
       
money= input.nextInt();
        if
(money>700)
        {
            System.
out.println("Enjoy your big 12 bucket :)");
       
}
       
else
       
{
            System.
out.println("Insufficient balance");
       
}
    }
}

 

Output


After If condition, if you are trying to put only one statement, then you can neglect the curly braces like,

If(condition 1)

Statement 1

However, when more statements are used within the If condition, it’s necessary to use curly braces.

Switch Statement

In case of ‘If’ statements, when many conditions are present, the length of the code becomes bigger. Hence, in such cases, ‘switch’ statements can be used to avoid complexity. The ‘switch’ keyword is used while using ‘Switch’ conditional statements. Within the switch keyword, the input variable is given. When ‘break’ keyword is used, the compiler stops executing the other lines else it will start checking other condition also. ‘Default’ keyword is used so that it gets executed when all the condition fails. The syntax of the switch statement is:

switch(input)

{

case 1:

{

statement 1;

break;

}

case 2:

{

statement 2;

break;

}

default:

statement;

}

Here, input is checked with the case and whenever case 1 is satisfied, statement 1 is executed. Since break keyword is used, the execution stops. If suppose, case 2 condition is satisfied, statement 2 gets executed. If neither condition matches, the statement in the default keyword, gets implemented.

Eg) Consider 3 persons Kalai, Juan and Ash who have different nick names as Nutella, party monster and Mocacinno. Based on the user’s input, their nick names must be displayed using switch statements.

import java.util.Scanner;

class
java {
   
public static void main(String args[]) {
        System.
out.println("Hi, What's your sweet name?");
       
String name;
       
Scanner input=new Scanner(System.in);
       
name= input.next();
        switch
(name)
        {
           
case "Kalai":
                System.
out.println("Hey Nutella :)");
                break;
            case
"Juan":
                System.
out.println("Gorgeous Party monster :)");
                break;
            case
"Ash":
                System.
out.println("Mocacinno :)");
                break;
            default
:
                System.
out.println("Have a great day :)"+ name);
                break;
       
}

 

Output

The input is exactly checked with the case statement. Even, when we give ‘kalai’, the execution output was the default statement which proves these are case sensitive.  

PREVIOUS

                        NEXT

Post a Comment

0 Comments