Krivalar Tutorials 
Krivalar Tutorials

Java Control Statements


<<Previous

Next >>





Control Statements

Control statements are used to control the flow of program execution. Control statements can control a single statement or a block of statement.

Statement: is any statement that ends with a semi colon (;)

Block: Java statements that are placed between opening brace { and closing brace }


if statement

if statement is used as shown below:

public class If {
public static void main(String a[]){
int i=3+3-6+8-9+4-6+10;
if(i>1){
	System.out.println("Just do it");
}
}
}
In the above statement, the System.out.println statement is executed only if the condition i>1 returns true. if the condition returns false, the statement after the closing brace is executed A condition can only return one of the two boolean values: true or false

if-else statement

Example of if-else statement:
public class IfElse {
public static void main(String a[]){
 int i=3+3-6+8-9+4-6;
 if(i>1){
 	System.out.println("Just do it");
 }
 else{
	System.out.println("Just leave it");
 }
}
}
Above code will print "Just do it" if the value of i is greater than 1. Otherwise, code prints "Just Leave it". In this case, value of i is -3 which is less than 1 and hence prints "Just Leave it"

if-else-if statement

Example of if-else-if statement:

public class IfElseIf {
public static void main(String a[]){
 int marks=75;
 if(marks>=90){
	System.out.println("Excellent");
 }
 else if(marks>=80){
	System.out.println("Good");
 }
 System.out.println("Nothing");
}
}
Above code will print "Excellent" if the value of marks is greater than 90. Otherwise, if marks is greater than 80, "Good" will be printed. If the other two conditions returns false, then it prints "Nothing". In this case, since marks=75,it prints "Nothing"





Nested if Statement

Example of Nested if Statement:


public class NestedIf {
public static void main(String a[]){
 int sides=4;
 int side1,side2,side3,side4;
 side1=side2=side3=side4=5;
 int angle1,angle2,angle3,angle4;
 angle1=angle2=angle3=angle4=90;

 if(sides == 4 )
 {
	System.out.println(
		"It can be a quadrilateral,"
		+ "parallelogram,\n rectangle,"
		+ " rhombus or square");

   if(side1==side2
		&& side2==side3
		&& side3==side4)
	{

	  System.out.println(
		"It can be rhombus or square");

	  if(angle1==angle2
			&& angle2==angle3
			&& angle3==angle4)
		{
			System.out.println(
			"It is a square");
  		}
	}
  }
}
}
This will print
It can be a quadrilateral, parallelogram,
rectangle, rhombus or square
It can be rhombus or square
It is a square


Multiple if-else-if statements

if-else-if statement can be chained to check conditions and take different actions for each condition


public class MultipleIfElseIf {
public static void main(String a[]){
  int marks=95;
  if(marks<70){
	System.out.println("Did not qualify");
  }
  else if(marks<80){
	System.out.println("Average");
  }
  else if(marks<90){
	System.out.println("Good");
  }
  else{
	System.out.println("Excellent");
  }
}
}

This will print:

Excellent

In the above code, if condition is checked and if it returns true, the statement within the block is executed. This is followed by the sequence of else if statements and finally the else statement.


while statement

while is a conditional loop statement. The statements within the while block are executed only if the condition returns true.

while - Syntax


while(condition)
{
	statement;
}

while - Example


public class While {
public static void main(String a[]){
  int i=0;

  System.out.println(
		"\nbefore first while loop");
  while(i<10){
	System.out.print(
		"" + i + ",");
	i=i+1;
  }

  i=20;

  System.out.println(
		"\n\nbefore second while loop");
  while(i<10){
	System.out.print("" + i + ",");
	i=i+1;
  }
  System.out.println(
	"\nafter second while loop");
}
}

The above while loop prints:

before first while loop
0,1,2,3,4,5,6,7,8,9,

before second while loop

after second while loop

do while statement

do while is a conditional loop statement. do while loop is similar to while loop except that do while executes the block and then only checks the condition specified. Thus, do while will execute the block atleast once, even if the condition evaluates to false the first time itself.

do while - Syntax

do
{
	statement;
}while(condition)

do while - Example


public class DoWhile {
public static void main(String a[]){
	int i=0;

	System.out.println(
		"\nbefore first while loop");
	do{
		System.out.print("" + i + ",");
		i=i+1;
	}while(i<10);

	i=20;

	System.out.println(
		"\n\nbefore second while loop");
	do{
		System.out.print("" + i + ",");
		i=i+1;
	}while(i<10);
	System.out.println(
		"\nafter second while loop");

}
}
The above do while loop prints:
before first while loop
0,1,2,3,4,5,6,7,8,9,

before second while loop
20,
after second while loop

for statement

for statement - Syntax

for(initialization statement;
		termination statement that returns true or false;
		loop counter){
	statement;
}

for statement - Example


public class ForLoop {
public static void main(String a[]){
	for(int i=0; i<10;i++)
	{
		System.out.print("" + i + ",");
	}
}
}

The above for loop prints

0,1,2,3,4,5,6,7,8,9,

In the above for loop:

int i=0 - is the initialization statment
i<10 - is the termination statement based on which the block executes if the condition returns true
i++ - is the loop counter and it is an expression that can be used to modify the values in the termination statement.

This statement can be increment or decrement or any other evaluation.

  • the initialization statement in the for loop executes once and only once at the start of the for loop
  • termination statement is executed each and every time, the block is executed and the block executes only when the condition in the termination statement is true.
  • loop counter is executed each and every time after the termination statement in for loop

for loop can work without initialization statement

public class ForLoopNoInitialization {
public static void main(String a[]){
	int i=0;
	for(;i<10;i++)	{
		System.out.print(
			"" + i + ",");
	}
}
}
Above code will print:
0,1,2,3,4,5,6,7,8,9,

for loop can work without termination statement, in which case, you have to take care of terminating the loop using break statement or any other way.


public class ForLoopNoTermination {
public static void main(String a[]){
	int i=0;
	for(;;i++){
		System.out.print("" + i + ",");
		if(i>=10){ break; }

	}
}
}
Above code will print:
0,1,2,3,4,5,6,7,8,9,
for loop can work without loop counter statement in which case you need to find a way to run the loop for different values.

public class ForLoopNoInitNoConditionNoTermination {
public static void main(String a[]){
	int i=0;
	for(;;){
		if(i>=10){
			break;
		}
		System.out.print("" + i + ",");
		i++;
	}
}
}
This will print:
0,1,2,3,4,5,6,7,8,9,

Nested for Statement

Syntax:
for(initialization statement;
		termination statement that returns true or false;
		loop counter){
	for(initialization statement;
		termination statement that returns true or false;
		loop counter){
	statement;
	}
}
Example 1:

public class NestedForLoop {
public static void main(String a[]){
	for(int i=0; i<5;i++)
	{
		for(int j=5; j>0;j--)
		{
			System.out.print("" + i
				+ "," + j + " | ");
		}
		System.out.println();
	}
}
}
The above for loop prints
0,5 | 0,4 | 0,3 | 0,2 | 0,1 |
1,5 | 1,4 | 1,3 | 1,2 | 1,1 |
2,5 | 2,4 | 2,3 | 2,2 | 2,1 |
3,5 | 3,4 | 3,3 | 3,2 | 3,1 |
4,5 | 4,4 | 4,3 | 4,2 | 4,1 |

Example 2:

public class NestedForLoop2 {
public static void main(String a[]){
	for(int i=0; i<2;i++)
	{
	 for(int j=3; j>0;j--)
	 {
	   for(int k=0; k<2;k++)
	   {
	   System.out.print("" + i + ","
			+ j + "," + k + " | ");
 	   }
	    System.out.println();
	 }
	System.out.println();
	}
}
}
The above for loop prints
0,3,0 | 0,3,1 |
0,2,0 | 0,2,1 |
0,1,0 | 0,1,1 |

1,3,0 | 1,3,1 |
1,2,0 | 1,2,1 |
1,1,0 | 1,1,1 |

Example 3:

public class NestedForLoop3 {
public static void main(String a[]){
 for(int i=0; i<2;i++)
 {
  for(int j=3,k=0; j>0 && k<2;j--,k++)
  {
	System.out.print("" + i + ","
				+ j + "," + k + " | ");
  }
 System.out.println();
 }
}
}
This will print:
0,3,0 | 0,2,1 |
1,3,0 | 1,2,1 |

break statement

break statement
  • used to break out of a for, while or do while loop.
  • also used to break out of a switch - case block.
  • used to break out of a specific loop marked with a label.

break - syntax

break;
break <label> 

break - Example


public class Break {
public static void main(String a[]){
	for(int i=1; i<=5; i++){
		System.out.print("\nloop count "
							+ i + ": ");
		for(int j=1; j<=5; j++){
			if(j>i){
				break;
			}
			System.out.print( ""+ j + " ");
		}
	}
}
}
The above code would print
loop count 1: 1
loop count 2: 1 2
loop count 3: 1 2 3
loop count 4: 1 2 3 4
loop count 5: 1 2 3 4 5

Example 2:

public class BreakLabel {
public static void main(String a[]){
outer:
for(int i=1; i<=5; i++){
	System.out.print("\nloop count "
						+ i + ":");
	for(int j=1; j<=5; j++){
		if(i==3){
			break outer;
		}
		System.out.print(""+ j + " ");
	}
}

System.out.println();

for(int i=1; i<=5; i++){
	System.out.print("\nloop count "
						+ i + ":");
	inner2:
	for(int j=1; j<=5; j++){
		if(i==3){
			System.out.println(
				"\nCalling break inner2...");
			break inner2;
		}
		System.out.print(""+ j + " ");
	}
}

}
}
This would print
loop count 1:1 2 3 4 5
loop count 2:1 2 3 4 5
loop count 3:

loop count 1:1 2 3 4 5
loop count 2:1 2 3 4 5
loop count 3:
Calling break inner2...

loop count 4:1 2 3 4 5
loop count 5:1 2 3 4 5
In the above example, 'outer' is the label. break outer is to break out of the for loop having the label 'outer'.

continue statement

continue statement can be used to start the next iteration in a for, while or do while loop. syntax examples:
	continue;
	continue 
Example 1:

public class Continue {
public static void main(String a[]){
System.out.println("Before for loop");

for(int i=1; i<=5; i++){
	System.out.print("\nloop count "
						+ i + ":");
	for(int j=1; j<=5; j++){
		if(j==i){
			continue;
		}
		System.out.print(" " + j);
	}
}

System.out.println("\n\nAfter for loop");

}
}
The above code would print:
Before for loop

loop count 1: 2 3 4 5
loop count 2: 1 3 4 5
loop count 3: 1 2 4 5
loop count 4: 1 2 3 5
loop count 5: 1 2 3 4

After for loop
Example 2:

public class ContinueLabel {
public static void main(String a[]){
System.out.println("Before for loop");
outer:
	for(int i=1; i<=5; i++){
			System.out.print("\nloop count "
								+ i + ":");
		for(int j=1; j<=5; j++){
			if(j==i){
				continue outer;
			}
			System.out.print( " " + j);
		}
	}
System.out.println("\n\n After for loop");

}
} 
The above code would print:
Before for loop

loop count 1:
loop count 2: 1
loop count 3: 1 2
loop count 4: 1 2 3
loop count 5: 1 2 3 4

After for loop

return statement

return statement can be used to return a value in a method definition syntax examples:
	return;
	return 

switch-case-default statement

switch-case is also another form of branching statement which can be used to execute different actions for different values as shown below. If none of the values mentioned in case match, then statements provided under default will execute. Example Switch Case 1:

public class SwitchCase {
public static void main(String a[]){
int sides=4;

switch(sides ){
	case 1:
		System.out.println("Line");
		break;
	case 2:
		System.out.println("Angle");
		break;
	case 3:
		System.out.println("Triangle");
		break;
	case 4:
		System.out.println("Quadrilateral");
		break;
	default:
		System.out.println("Polygon");
		break;
}
}
}
The above code will print:
Quadrilateral
Please note that break is used in each of the case clause. This causes the execution to break outside of switch and execute the next statement that follows the switch block Example Switch Case 2:

public class SwitchCaseNoBreak {
public static void main(String a[]){
String sides="JAPAN";

switch(sides ){

	case "INDIA":
		System.out.println("TAJ MAHAL");
	case "AMRITSAR":
		System.out.println("Golden Temple");
		break;

	case "JAPAN":
		System.out.println("Todaiji Temple");
	case "KYOTO":
		System.out.println("Kyoto Imperial Palace");
		break;

	case "GREECE":
		System.out.println("Kyoto Imperial Palace");
		break;

	case "EGYPT":
		System.out.println("Pyramids");
		break;

	case "AMERICA":
		System.out.println("Pyramid of the Sun, Mexico");
	case "COLUMBIA":
		System.out.println("San Agustín");
		break;

	}
}
}
The above code will print:
		Todaiji Temple
		Kyoto Imperial Palace

<<Previous

Next >>





















Searching using Binary Search Tree