A blog for computer science passionates.

Tuesday 7 April 2020

Java: Decision Making

Hello Friends!!!
Welcome to passionforcs,

Today I am going to share decision making statements :)
Make Firm Decision & Draw Your Path
Image Src: pixabay

as you are already familiar with decision making. So, let's start with the decision making statements.
There are different decision making statements available in Java such as,

Statement

Usage

Syntax  

How to use? Example

simple if

To make a simple decision on the basis of single condition. if the condition is true, it returns a result otherwise it doesnot return anything.

 if(condition){
    //code block
}

int age=18;
if(age>=18){
    System.out.print("Eligible for vote");
}

if-else    

Used to make two way decision i.e.  if the condition is true, it returns an appropriate answer and if condition is false, it returns the statements of else block.

if(condition){
    //if code block
}
else{
    //else code block
}

int age=15;
if(age>=18){
    System.out.println("Eligible for vote");
}
else{
    System.out.println("Not Eligible for vote");
}

else-if ladder (Multiple if else)

When we have multiple conditions we can work with else-if ladder. When we work on else-if ladder, else part is optional.

if(condition-1){
    //code block
}
else if(condition-2){
    //code block
}
else if(condition-n){
    //code block
}
else{
    //code block
}

int percent=70;
if(percent>=70){
    System.out.println("Distinction");
}
else if(percent>=60 && percent<70){
    System.out.println("First class");
}
else if(percent>=50 && percent<60){
    System.out.println("Second class");
}
else if(percent>=40 && percent<50){
    System.out.println("Pass");
}
else if(percent<40){
    System.out.println("Fail");
}

Nested if-else

It is known as nested as its’ syntax is like that. It means if inside if let’s understand through its syntax.

if(condition){

    if(condition){

        //code block

    }

    else{

       //code block

    }

}

else{

    if(condition){

       //code block

    }

    else{

       //code block

    }

}

int a=5,b=2;

if(a!=b){

    if(a>b){

        System.out.println(a+” is max”);

    }

    else{

       System.out.println(b+” is max”);

    }

}

else{

   System.out.println(“Both are same”);

}

switch - case

Generally, it is not a pure decision making statement, it uses an expression for int and char data types only and based on expression it chooses its label and executes code block. But if no label matches, it goes to default and perform default code block. Here, we use break statement it is not compulsory every time it depends on situation that we need to add break or continue type of jumping statement or not. Even if we have same values for multiple case we can write different cases together and at last we can add it’s code block

switch(expression){

   case label-1:

      //code block-1

    break;

    case label-2:

      //code block-2

    break;

    case label-n:

      //code block-n

    break;

    default:

      //default code block

}

int a=3;

switch(a){

    case 1:

        System.out.println(“ONE”);

    break;

    case 2:

        System.out.println(“TWO”);

    break;

    case 3:

        System.out.println(“THREE”);

    break;

    default:

        System.out.println(“Invalid Input”);

    break;      //break is optional in default.

}

No comments:

Post a Comment