Control Statement in java

java provide following control statement

  •  if block
  •  else
  • else if
Use of IF Block

In java if is condition block
let's see some example.

If.java

package Controlstatement;

public class If 
{

	public static void main(String[] args) 
	{
	    int a=10;
		
		if(a>0)
		{
			System.out.println("a is greater zero");
			
		}
			
	}

}

else if

package Controlstatement;

public class If 
{

	public static void main(String[] args) 
	{
	    String name="bhavesh";
	    
		if(name=="bhavesh")
		{
			System.out.println(" Hello bhavesh");
			
		}
		
		else if (name=="vivek") 
		{
		
		 System.out.println("hello vivek");
		}	
	}
}

Output: Hello bhavesh
       

Example of else-if

class Program1  
{ 

 public static void main(String[] args)  
 { 
 
  System.out.println("program started");  
  int a=5;  
  
  if(a>6)  
  { 
    System.out.println("number is above 6");  
  }  
  
  else 
  { 
    System.out.println("number is below 6"); 
  } 
  
 } 
} 


Output: 
Program started 
Number is below 6 
Program ended

Leave a Reply