Java If Else Statement Briefly Details With Examples

Java If Else Statement Briefly Details With Examples
Java If Else Statement Briefly Details With Examples


Java If Else Statement Briefly Details With Examples

If Statement: The ability to regulate the flow of your program, rental it builds choices on what code to execute, is effective to the applied scientist. 

The if statement permits you to regulate if a program enters a part of code or is not supported whether or not a given condition is true or false. 

An if statement is the most important statement in the java language.it can be used to decide control flow in the java program. condition is true than if the statement executes the program.


Important Same Others If Else Statement

👉C++ If-Else Statement 


👉C If-Else Statement 


👉Python If-Else Statement


Types of Java conditional statement

1. if the statement

2.if-else statement

3.if-else-if statement

4. Nested if statement

if statement to examine a user-entered positive identification, your program will decide whether or not a user is allowed access to the program.

if(age>=18)

Here if conditions are true then the statement is executed otherwise the program is executed but the output does not show in your output screen.


Syntax of if Statement

if(condition)

{

//Statement

}


How To Use if Statement in Java?

class icoderweb


{


public static void main(String[] args)


{


int age=18;


if(age<=18)


{


System. out.print("You can Drive Bike");


}


}


}

*****OUTPUT*****


You can Drive Bike



How To Use if from user input Statement in Java?

class icoderweb


importjava.util.Scanner;


{


public static void main(String[] args)


{


Scanner s=new Scanner(System.in);


int age;


Sytem.out.print("Please Enter Your Age:");


age=s.nextInt();


if(age<=18)


{


System. out.print("You can Drive Bike");


}


}


}

*****OUTPUT*****


Please Enter Your Age:21


You can drive Bike



if-else Statement: A if-else statement is also used to check conditions. if the condition is a true statement it will be executed, otherwise else statement executed.


Syntax of if-else Statement in java

if(condition)

{

//Statement

}

else

{

//statement

}


How To Use if-else Statement in java?

class icoderweb


{


public static void main(String[] args)


{


int age=17;


if(age>=18)


{


System. out.print("You can Drive Bike");


}


else


{


System. out.print("You can not Drive Bike");


}


}


}


*****OUTPUT*****


You can not Drive Bike



How To Use if-else Statement from user input in java?

class icoderweb


importjava.util.Scanner;


{


public static void main(String[] args)


{


Scanner s=new Scanner(System.in);


int age;


Sytem.out.print("Please Enter Your Age:");


age=s.nextInt();


if(age>=18)


{


System. out.print("You can Drive Bike");


}


else


{


System. out.print("You can not Drive Bike");


}


}


}


*****OUTPUT*****


Please Enter Your Age:16


You can not drive Bike



if-else-if Statement: A java if-else-if statement is a ladder executed one condition from multiple statements.

if executes only one condition at a time, a condition which is true condition is first true than the first statement executed.

Syntax of if-else-if Statement in java

if(condition)

{

   //Statement

}

else if

{

   //statement

}

else if()

{

  //statement

}

else

{

  //statement

}


How To Use if-else-if Statement in java?

class icoderweb


{


public static void main(String[] args)


{


int age=14;


if(age>=18)


{


System. out.print("You can Drive Bike");


}


else if(age>=14)


{


System.out.println("You are kid");


}


else if(age>=21)


{


System.out.println("You are Young");


}


else


{


System.out.println("condition are not match");


}


*****OUTPUT*****


You are kid



How To Use if-else-if Statement from user input in java?

class icoderweb


importjava.util.Scanner;


{


public static void main(String[] args)


{


Scanner s=new Scanner(System.in);


int age;


Sytem.out.print("Please Enter Your Age:");


age=s.nextInt();


if(age>=18)


{


System. out.print("You can Drive Bike");


}


else if(age>=13)


{


System.out.println("You are Kid");


}


else if(age<=21)


{


System. out.print("You can Drive Bike you are young");


}


else


{


System.out.println("condition are not match");


}


}


*****OUTPUT*****


Please Enter Your Age:13


You are kid



Nested if Statement: A nested statement is used to check the condition. nested if one if inside if another if condition.it is called the nested if statement.
if statement represents the if block within another if block.


Syntax of Nested if Statement in java

if(condition)

{

   //Statement

}

if (condition)

{

   //statement

}


How To Use Nested if Statement in java?

class icoderweb


{


public static void main(String[] args)


{


int age=24;


int weight=30;


if(age>=18)


  if(weight>38)


{


System.out.println("You are donate blood");


}


else 


{


System.out.println("You are not donate blood");


}


}


}


*****OUTPUT*****


You do not donate blood                


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.