JAVA If statement

JAVA If statement with sample coding

If condition is used to evaluate either the statement is true or false.The code within if condition is only execute when the statement will be true.

I.e. Java code example

public class example {

public void
main(String args[ ]) {

int x = 5;

if( x == 5 )
{

System.out.print(“x is equal to five”);

}

}

}

In this example the value of x=5. so, the if condition is true in the above example so output will be x is equal to five we can also use If else statement else part will execute only when the condition will false. i.e example of Java code

public class example 2
{

public void main(String args[ ]) {

int x = 5;

if( x == 3 ) {

System.out.print(“x is equal to three”);

}

else {

System.out.print(“x is equal to five”);

}

}

}

In this example if condition will be false because x=5 . Therefore else statement will be execute and output will be value of x is equal to five.

We can also use nested if statement. Here is example of nested if statement.

i.e nested if Java code

public class example 3
{

public void main(String args[ ]) {

int x = 5;

int x = 3;

if( x == 3 ) {

System.out.print(“x is equal to three”);

}

If(x== 5{

System.out.print(“x is equal to five”);

}

}

}

In this example both conditions are true. so, both statement will be print as a output.

Output:

x is equal to three

x is equal to five