Control Statement in java

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

Your email address will not be published. Required fields are marked *