Reachability of Statements


In decision making statements, must write in such a way so that at least for one exception condition java program must be reachable. There must be at least one exception path of execution that will “reach” to statement.

For Example
This code will not compile because ++count is unreachable.

class TestExample {
	public static void main(String[] args) {
	int count = 0;
	//This won't compile because reachability rule not with while
	//statments inside block for false
	while (false) {
		++count;
		}
	}
}

The exception to the reachability rule apply with the if statement.
The following code does compile:

class TestExample {
	public static void main(String[] args) {
	// This compiles fine.
	int count = 0;
	if (false)
	{
	++count;
	}
}
}

Note: If statement is special so that can be used for conditional compilation. In above code would compile file , but the if (false) { ++Count; } code would not appear in the class file.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s