Control Statement in Java


Control Statement

Control Statements are used to control the flow of a program in java.It has three types.

1. Branching statement

2. Looping statement

3. Jumping statement

ddd

 

 

 

 

 1.Branching statement:

It has two types.

1. Conditional statements

2. Decision making statements

1.Conditional statements : are used to apply condition in the program and also used to control the flow of the program.If the condition is true then the block of the condition is executed otherwise it is terminated.It has four types.

(1) if

(2) if-else

(3) elseif

(4) Nested if

1.If  Condition:- In this only if block is used if the condition is true then the block is executed other wise t is terminated.for example

//**simple if**

class if9{

public static void main(String aa[]){

int a;

a=11;

if (a>11){

System.out.println(“hello”);

}}}

Output:-

 

 

 

 

 

 

 

 

2.if-else Statement:In this statement if and its else part is used .If the if condition is false then the program is not terminated its else part is executed.For example

//**If-Else**f**

class dd{

public static void main(String aa[]){

int a,b;

a=Integer.parseInt(aa[0]);

b=Integer.parseInt(aa[1]);

if (a>b){

System.out.println(“a is greater”);}

else{

System.out.println}(“b is greater”);}}

}}}

Output:-

 

 

 

 

 

 

 

 

3.elseif :- In this types of statement first the condition1 is tested and if it is true then, statement1 is executed and all other if-else-if condition ends.If the condition1 is false,then the statement1 is ignored and condition2 is tested.If it is true then statement2 is executed and remaining else if condition is terminated and if the condtion2 is false,statement2 is ignored and condition3 is tested.This process continues in this way.e.g

class ddd{

public static void main(String aa[]){

int a,b,c;

a=Integer.parseInt(aa[0]);

b=Integer.parseInt(aa[1]);

c=Integer.parseInt(aa[2]);

if (a>b && a>c){

System.out.println(“a is great”);}

else if(b>a && b>c){

System.out.println(“b is greater”);}

else{

System.out.println(“c is greater”);

}}}

output:

cc

 

 

 

 

 

 

 

4. Nested if:- If if condition may have another if condition then,it is known as Nested if or Ladder if condition.e.g

class nested11{

public static void main(String aa[]){

int a,b,c;

a=Integer.parseInt(aa[0]);

b=Integer.parseInt(aa[1]);

c=Integer.parseInt(aa[2]);

if (a>b){

if(a>c){
System.out.println(“a is great”);}
else{
System.out.println(“c is greater”);}}

else if(b>c){
System.out.println(“b is greater”);}
else{

System.out.println(“b is greater”);
}}}

output:

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments are closed.