Java
If Statement
Some
time, in programming, we need to take some decisions or we need to handle the
flow of our code according to our need. We can do this in Java with the help of
If Statement. This If statement is used to handle the flow of your program
execution, i.e. which statement should be executed when certain condition is fulfilled
and vice versa.
The
answer of the Java If Statement is in true, or false which is actually known as
Boolean in Programming. If condition answer is true, we can run specific block
of code and if condition answer is not true, we can run another block of code.
For Example
If
we have some students of Computer Science and Information Technology and we
want to assign them their Lab. So we can say that
If
student belongs to Information Technology, then assign IT LAB
If
student belongs to Computer Science, then assign CS LAB
Syntax
of Java IF Statement
If
(condition)
{
Your
code to execute
}
Here,
the condition answer can be either true or false. If true, then block of code
in curly braces will be executed
If
condition answer is not true, then the block of code in curly braces will be
skipped
Example
Program
Class
IfTest
{
public static void main(String[] ar)
{
int age;
Age
= 20;
If(age>15)
{
System.out.println(“You are allowed to
take admission”);
}
}
}
Now
as the number in the age variable is greater than 15, so statement inside curly
braces will be executed and output will be
You
are allowed to take admission
If
else Statement in Java
As
it is already discussed that Java If Statement answer can be in true or false
which is also known as Boolean. So when condition answer is true, the block
inside curly braces will be executed but if condition answer is not true, then
nothing will be printed. So in that case, we need another block and that is
known as ELSE block. This block is executed when condition answer is not true
Syntax
If
(condition)
{
}
else
{
}
For
Example
class
IfElseTest
{
public
static void main(String[] ar)
{
int
age;
age=14;
if(age>20)
{
System.out.println(“You are allowed to
take admission”);
}
else
{
System.out.println(“You are not
allowed to take admission”);
}
}
If-else-if
in Java
In
Java, if-else-if is used when we have multiple conditions to check
Syntax
If
(condition to check)
{
}
else if(condition to check)
{
}
else if(condition to check)
{
}
else
{
}
In
the above case, if all the conditions answers are false, then else portion is
executed
For
Example
Class
IfElseIfTest
{
public static void main(String[] ar)
{
int age;
age=20;
If(age>15)
{
System.out.println(“Age is
greater than 15”);
}
else if(age<15)
{
System.out.println(“Age is less
than 15”);
}
else
{
System.out.println(“Age is 15”);
}
Nested
If in Java
In Java, when we use if statement inside another if statement, this thing is known as Nested if
Syntax
If(condition)
{
If()
{
}
else
{
}
}
else
{
}
Second
if is the nested if and can only be executed when the first if condition is
true
For
Example
Class
NestIfTest
{
Public
static void main(String[] ar)
{
String name;
int age;
name
= “ali”;
age=20;
if(name==”ali)
{
if(age==20)
{
System.out.println(“Welcome”);
}
}
}
}
Output
will be
Welcome
No comments:
Post a Comment