This article summarizes some noteworthy points about the execution control statements in the Java programming language such as if…else, while, do…while, for loop, etc. This kind of statements is very important as it decides the execution flows of the program. If you ever worked with C/C++, you will see that Java is very similar to C/C++ in this aspect.

Here’s summary of all execution control statements in Java:

  • if…else: if this then do that, else do another. This is called branching statement.
  • while and do…while: these loop statements are used when the number of iterations is not known.
  • for loop: is used when the number of iteration is known exactly.
  • switch…case: this selection statement is useful when you want to test an expression against a list of values.
  • return: terminates execution of the current method and returns a value (or void) to the caller.
  • continue: is used in the loop statements for continuing execution of the next iteration (the code after is skipped).
  • break: stops execution of a loop and returns control to the outer code.

Okay, that’s the summary. And here are the noteworthy points:

1. if…else in Java

If the code in the if or else clause contains only one statement, you can omit the brackets ({ and }). For example:

if (x > 0)
	y += x;
else
	y -= x;

If there is more than one statement in the if or elseclause, you have to use the brackets:

if (x > 0) {
	y += x;
	x = 1;
} else {
	y -= x;	
	x = 0;
}

However, it is always recommended to use the brackets to make the code more readable and avoid potential bugs in future.

2. while and do…while in Java

Here you need to be able to understand the difference between while and do…while.

The while structure executes the enclosed code 0 or more times while the condition is still true. For example:

while (x > 0) {
	y += x;
	x--;
}

Whereas the do…while structure executes the enclosed code at least one time, regardless of the condition. For example:

do {
	y += x;
	x--;
} while (x > 0);

In this example, the code is always executed once even if x <= 0.

The while(true) statement executes the enclosed code forever, until a break is encountered:

while (true) {
    y += x;
    x++;
}

 

3. for loop in Java

Typically, a for loop has the following form:

for (initialization; condition; stepping) {
	// code
}

You can declare multiple variables in the initialization part as long as they are of the same type. For example:

for (int x = 0, y = 1; x < 10; x++) {

	// code
}

Here the two variables x and y are declared in the initialization part of the loop.

You can omit one, two or all the three parts. For example:

for ( ; ; ) {
	// code
}

This causes the loop runs forever, like the while(true) statement.

4. switch…case in Java

In this selection structure, remember using break in every case, otherwise the execution will fall through to the next case. For example:

switch (x) {
	case 1:
		y += 3;
	case 2:
		y -= 1;
		break;
	case 3:
		y *= 2;
		break;

}

In this example, if x = 1, the execution will fall through to the case 2 because the case 1 is missing a break, thus compromising the consistence of the program.

Although the default clause is optional in the switch-case construct, you should include it to handle the default case when the tested variable doesn’t fall within the specific cases. For example:

switch (variable) {
	case 1:
		// code	
		break;
	case 2:
		// code
		break;
	case 3:
		// code
		break;
	default:
		// default case, when the variable 
		// doesn't fall within the above cases
}

You can also throw an exception in the default case to indicate an error if the tested variable doesn’t fall within the range. For example:

switch (variable) {
	case 1:
		// code	
		break;
	case 2:
		// code
		break;
	case 3:
		// code
		break;
	default:
		throw new IllegalArgumentException();
}

From Java 7, use can test a String variable in the switch…case statement. Here’s an example:

String type = "classic";

switch (type) {
	case "classic":
		// code
		break;
	case "morden":
		// code
		break;
	case "new":
		// code
		break;
	default:
		// code
}

 

5. continue and break with labels

By default, the continue statement skips the rest code of the current iteration and continues execution of the next iteration in the loop structures like for and while.

Similarly, the break statement stops the execution of the loop and returns control for the code after the loop.

Let’s say we have two nested for loops like this:

for (int i = 0; i < 10; i++) {
	for (int j = 10; j > 0; j--) {
		// do something
		if (condition) {
			// break or continue;
		}

		// do other things
	}
}

What if we want to continue or break the execution of the outer loop, not the inner?

In this case, a labeled continue or labeled break will help. Let’s see an example:

outer:
for (int i = 0; i < 10; i++) {
	for (int j = 10; j > 0; j--) {
		// do something
		if (j == 3 && i == 5) {
			continue outer;
		}

		// do other things
	}
}

Here, the continue statement will continue the execution from the outer loop which is marked with the label ‘outer’. Note that the label must be placed right before the loop (no other statements can be between the label and the loop). 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment