New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

Control Statement


If Else Statement

In Java, if statement is used for testing the conditions. The condition matches the statement it returns true else it returns false. There are four types of If statement they are:

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

if Statement

In Java, if statement is used for testing conditions. It is used for only true condition.

Syntax:

	
if(condition)
{  
//code
  
}  
	

Data-flow-diagram of If Block

If Block Flow

Example:

	
public class IfDemo1 {  
public static void main(String[] args) 
	{  
	int marks=70;  
	if(marks > 65)
		{  
		System.out.print("First division");  
		}  
	}  
}  
	

if-else-program

if-else Statement

In Java, the if-else statement is used for testing conditions. It is used for true as well as for false condition.

Syntax:

	
if(condition)
{  
//code for true  
}
Else
{  
//code for false  
}  
	

Data-flow-diagram of If Else Block

IF else DFD Diagram

Syntax:

	
public class IfElseDemo1 {  
public static void main(String[] args) 
{  
int marks=50;  
if(marks > 65)
{  
System.out.print("First division");  
}  
else
{  
System.out.print("Second division");  
}
}  
}  
	

if else program

if-else-if ladder Statement

In Java, the if-else-if ladder statement is used for testing conditions. It is used for testing one condition from multiple statements.

Syntax:

	
if(condition1)
{  
//code for if condition1 is true  
}
else if(condition2)
{  
//code for if condition2 is true  
}  
else if(condition3)
{  
//code for if condition3 is true  
}  
...  
else
{  
//code for all the false conditions   
}  
	

Data-flow-diagram of If Else If Block

DFD of if-else-if block

Example:

	
public class IfElseIfDemo1 {  
public static void main(String[] args) {  
int marks=75;  
    if(marks<50){
System.out.println("fail");  
    }  
    else if(marks>=50 && marks<60){
System.out.println("D grade");  
    }  
    else if(marks>=60 && marks<70){
System.out.println("C grade");  
    }  
    else if(marks>=70 && marks<80){
System.out.println("B grade");  
    }  
    else if(marks>=80 && marks<90){
System.out.println("A grade");  
}else if(marks>=90 && marks<100){  
System.out.println("A+ grade");  
}else{  
System.out.println("Invalid!");  
    }  
}  
}  
	

if-else-if-output

Nested if statement

In Java, the Nested if statement is used for testing conditions. In this, one if block is created inside another if block when the outer block is true then only the inner block is executed.

Syntax:

	
if(condition)
{    
     		//statement
     if(condition)
{  
             //statement 
    }    
}  
	

Data-flow-diagram of Nested If Block

nested-if DFD

Example:

	
public class NestedIfDemo1 {    
public static void main(String[] args) 
{      
int age=25;  
int weight=70;    
if(age>=18)
{    
if(weight>50)
{  
System.out.println("You are eligible");  
}    
}    
}	
}  
	

Nested If block Output

Nested try block

In Java, there can be multiple try blocks. A try block within another try block is known as nested try block. In a program when any block of code can cause an error and the entire program can cause another error then try blocks are made

	
try  
{  
    		statements
    		try  
    		{  
        		Statements
			try  
    			{  
        			statements
    			}  
    			catch(Exception e)  
   			{	  
    			}  
    		}  
    		catch(Exception e)  
   		{	  
    		}  
}  
catch(Exception e)  
{  
}  
	

Example:

	
class NestedDemo1
{  
public static void main(String args[])
{  
try{
			try{
				System.out.println("Divide 39/0");  
				int b =39/0;  
			}
catch(ArithmeticException e)
{
System.out.println(e);
}  
			try{
				inta[]=new int[8];  
				a[8]=4;  
			}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}  
		}
catch(Exception e)
{
System.out.println(e);
}  

	}  
} 
	

nested-try-block

For Loop

In Java, for loop is used for executing a part of the program again and again. When the number of execution is fixed then it is suggested to use for loop. In java there are 3 types of for loops, they are as follows:

  1. Simple for loop
  2. For-each loop
  3. labelled for loop

Syntax:

	
for(initialization;condition;increment/decrement)
{  
//statement 
}  	
	

Parameters:

1) Initialization

It is the initial condition which is executed only once at the starting of a loop. It is an optional condition.

2) Condition

It is used to test a condition each time while executing. The execution continues until the condition is false. It is an optional condition.

3) Statement

It is executed every time until the condition is false.

4) Increment/Decrement

It is used for incrementing and decrementing the value of a variable. It is an optional condition.

Data Flow Diagram of for loop

for loop DFD

Example for simple For loop

	
public class ForDemo1
{  
public static void main(String[] args) 
	{  
		int n, i;
		n=2;
		for(i=1;i<=10;i++)
		{  
			System.out.println(n+"*"+i+"="+n*i);  
		}  
}  	
}  
	

for-loop-output

Example for Nested for loop

	
public class ForDemo2
{  
public static void main(String[] args) 
{  
for(inti=1;i<=5;i++)
{  
for(int j=1;j<=i;j++)
{  
				System.out.print("* ");  
}  
System.out.println(); 
}  
}  
}  
	

nested-for-loop program

for-each Loop

In Java, for each loop is used for traversing array or collection. In this loop, there is no need for increment or decrement operator.

	
Syntax:
for(Type var:array)
{  
//code for execution
}  
	

Example:

	
public class ForEachDemo1
{  
public static void main(String[] args) 
		{    
			inta[]={20,21,22,23,24};   
			for(int i:a)
{  
				System.out.println(i);  
			}  
}  
}  
	

for-each-loop

Labelled For Loop

In Java, Labelled For Loop is used to give label before any for loop. It is very useful for nesting for loop.

Syntax:

	
labelname:  
for(initialization;condition;incr/decr)
{  
//code for execution 
}  
	

Example:

	
public class LabeledForDemo1
{  
public static void main(String[] args) 
{ 
	num:  
		for(inti=1;i<=5;i++)
{  
	num1:  
		for(int j=1;j<=5;j++)
{  
			if(i==2&&j==2)
{  
			break num;  
			}  
		System.out.println(i+" "+j);  
		}  
		}  
}  
}  
	

labelled-for-loop

Switch Statement

In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder. In a switch statement,the expression can be of byte, short, char and int data types. From JDK7 enum, String class and the Wrapper classes can also be used. Following are some of the rules while using the switch statement:

  1. There can be one or N numbers of cases.
  2. The values in the case must be unique.
  3. Each statement of the case can have a break statement. It is optional.

Syntax:

	
switch(expression)
{    
case value1:    
 			//code for execution;    
 			break;  //optional  
case value2:    
 // code for execution
 break;  //optional  
......    
......
......
......
Case value n:
// code for execution
 break;  //optional  

default:     
 code for execution when none of the case is true;    
}    
	

Data Flow Diagram Of switch Block

switch block DFD

Example:

	
public class SwitchDemo1{ 
    public static void main(String[] args) 
    { 
int day = 3; 
        String dayName; 
        switch (day) { 
        case 1: 
dayName = "Today is Monday"; 
            break; 
        case 2: 
dayName = "Today is Tuesday"; 
            break; 
        case 3: 
dayName = "Today is Wednesday"; 
            break; 
        case 4: 
dayName = "Today is Thursday"; 
            break; 
        case 5: 
dayName = "Today is Friday"; 
            break; 
        case 6: 
dayName = "Today is Saturday"; 
            break; 
        case 7: 
dayName = "Today is Sunday"; 
            break; 
        default: 
dayName = "Invalid day"; 
            break; 
        } 
System.out.println(dayName); 
    } 
}
	

output switch Example

Example using Enum in Switch statement

	
public class SwitchDemo2{      
       public enumvowel{a, e, i, o, u}    
       public static void main(String args[])    
       {    
vowel[] character= vowel.values();    
           for (vowel Now : character)    
           {    
                switch (Now)    
                {    
                    case a:    
System.out.println("'a' is a Vowel");    
                        break;    
                    case e:    
System.out.println("'e' is a Vowel");    
                        break;    
                    case i:    
System.out.println("'i' is a Vowel");    
                        break;         
                    case o:    
System.out.println("'o' is a Vowel");    
                        break;    
                    case u:    
System.out.println("'u' is a Vowel");    
                        break;    
                    default:    
System.out.println("It is a consonant");    
                }    
            }    
        }    
}  
	

output-enum-switch program output

While Loop

In Java, While loop is a control statement. It is used for iterating a part of the program several times. When the number of iteration is not fixed then while loop is used.

Syntax:

	
while(condition)
{  
//code for execution
}  
	

Data-flow-diagram of While Block

while loop DFD

Example:

	
public class WhileDemo1
{  
	public static void main(String[] args) 
	{  
		inti=1;  
		while(i<=10)
		{  
			System.out.println(i);  
			i++;  
		}  
	}  
}  
	

while-loop-output

Example for infinite while loop

	
public class WhileDemo2 
{  
	public static void main(String[] args) 
	{  
		while(true)
		{  
		System.out.println("infinitive while loop");  
		}  
	}  
}  
	

infinite-while-loop jpg

do-while loop

In Java, the do-while loop is used to execute a part of the program again and again. If the number of iteration is not fixed then the do-while loop is used. This loop executes at least once because the loop is executed before the condition is checked.

Syntax:

	
do
{  
//code for execution 
}
while(condition);  
	

Data Flow Diagram of do-while Block

do-while-dfd diagram

Example:

	
public class DoWhileDemo1 
{  
public static void main(String[] args) 
{  
inti=1;  
do
{  
System.out.println(i);  
i++;  
}while(i<=10);  
        }  
}  
	

output-of-do-while-program

Example for infinite do-while loop

	
public class DoWhileDemo2 
{  
public static void main(String[] args) 
{  
        do
{  
System.out.println("infinitive do while loop");  
}while(true);  
}  
} 
	

output-of-do-while-infinte-program

Break Statement

In Java, a break statement is used inside a loop. The loop is terminated immediately when a break statement is encountered and resumes from the next statement.

Syntax:

	
jump-statement;    
break;   
	

Data Flow Diagram of break statement

break-statement-DFD

Example:

	
public class BreakDemo1 {  
public static void main(String[] args) {  

for(inti=1;i<=10;i++){  
        if(i==8){

            break;  
        }  
System.out.println(i);  	
    }  
}  
}  
	

break-statement-output

Example using break in do while loop

	
public class BreakDoWhileDemo1
{  
public static void main(String[] args) 
{    
		inti=1;   
		do
{  
			if(i==15)
{  
			i++;  
				break; 
			}  
			System.out.println(i);  
			i++;  
		}while(i<=20);  
\}  
}  
	

break-statement-output-do-while

continue Statement

In Java, the Continue statement is used in loops. Continue statement is used to jump to the next iteration of the loop immediately. It is used with for loop, while loop and do-while loop.

	
jump-statement;    
continue;   
	

Example:

	
public class ContinueDemo1
{  
public static void main(String[] args) 
{  
		for(inti=1;i<=10;i++)
		{
			if(i==5)
{
			continue;
			}  
			System.out.println(i);  
		}  
}  
}  
	

output-of-continue-statement

Example:

	
public class ContinueDemo2 {  
public static void main(String[] args) {  
xy:  
for(inti=1;i<=5;i++){    
pq:  
for(int j=1;j<=5;j++){    
                        if(i==2&&j==2){
                            continue xy;    
                        }    
System.out.println(i+" "+j);    
                    }    
            }    
}  
}
	

output-of-continue-statement

Goto statement

In Java, the goto statement is not supported. Its is a reserved keyword which can be added in the future versions of java. But the label is supported in Java and it can be placed before a nested loop statement.

In the below example, label and break are used instead of the goto statement.

Example:

	
public class LabelDemo1{ 
    public static void main(String[] args) 
    { 
    a: 
        for (inti = 0; i< 10; i++) { 
            for (int j = 0; j < 10; j++) { 
                if (j == 5) 
                    break a; 
System.out.println("j = " + j); 
            } 
        } 
    } 
}
	

output-of-goto-statement

Example using label and continue

	
public class LabelDemo2{     
public static void main(String[] args) 
    {  
    a: 
        for (inti = 0; i< 5; i++) { 
            for (int j = 0; j < 5; j++) { 
                if (j == 2)
                    continue a; 
System.out.println("j = " + j); 
            } 
        } 
    } 
}
	

goto-statement-with-label