Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.

  1. if statement
  2. if-else statement
  3. nested if statement
  4. if-else-if ladder

Java IF Statement

The Java if statement tests the condition. It executes the if block if condition is true.

The argument to the if statement should be boolean type by mistech if we are try to provide any other type then we will get compile error.


if(condition) // check condition ;
{
}
if(true); // condition true.


Example:


public class Java64bit {
public static void main(String[] args) {
if(true){
System.out.print("print java64bit");
}
}
}


Output: print java64bit

Java If-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:


if(x) // x Should be boolean types.
{
Action if x is true.
}
else
{
Action if x is false
}


Example:


public class IfElse{
public static void main(String[] args) {
boolean b =true;
if(b=false){
System.out.println("java64bit");
}
else{
System.out.println("Read more java64bit");
}
}
}


Output: Read more java64bit

Java IF-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:


if(x) // x Should be boolean types.
{
Action if x is true.
}
else if(y) //Should be boolean types
{
Action if y is true.
}
else if(z) //Should be boolean types
{
Action if z is true.
}
..... else
{
condition is false
}


Example:


public class EsleIfStatement {
public static void main(String[] args) {
int marks=92;
if(marks<33){
System.out.println("fail");
}
else if(marks>=33 && marks<45){
System.out.println("D grade");
}
else if(marks>=45 && marks<60){
System.out.println("C grade");
}
else if(marks>=60 && marks<75){
System.out.println("B grade");
}
else if(marks>=75 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("Good for Java64bit");
}else{
System.out.println("Invalid!");
}
}
}


Output: Good for Java64bit

Copyright ©2017 Design& development