![]() |
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
Types of Java conditional statement
1. if the statement2.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
}
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
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
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
Syntax of if-else Statement in java
if(condition)
{
//Statement
}
else
{
//statement
}
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
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
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
Syntax of if-else-if Statement in java
if(condition)
{
//Statement
}
else if
{
//statement
}
else if()
{
//statement
}
else
{
//statement
}
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
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
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
Syntax of Nested if Statement in java
if(condition)
{
//Statement
}
if (condition)
{
//statement
}
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
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