Selection Control Structures


Selection control structures are used when you want to make a decision based on a value.

The simplest form is the "if" statement. An "if" statement test the truth of a condition. If the condition is true, the code is executed.

	if ( x==5 ) y = 10;
When you have one line of code to execute and the line is short, it is common to place the line of code on the same line as the if statement.

If you have more than one line of code to execute when the condition is true, you must enclose ALL lines inside of a code block using the curly braces.

	if ( args.length == 2 ) {
		String word1 = args[0];
		String word2 = args[1];
	}
If you leave off the curly braces:
	if ( args.length == 2 )
		String word1 = args[0];
		String word2 = args[1];
The first assignment statement statement will execute only if the condition is true, but the second assignment statement will ALWAYS execute.

The "if" executes code only when the condition is true. To execute code when the condition is false, use the "else" clause:

	if ( x==5 ) y = 10;
	else y = 2;

	if ( args.length == 2 ) {
		String word1 = args[0];
		String word2 = args[1];
	} else
		System.out.println("You must enter two command line arguments!");

If you leave off the curly braces:

	if ( args.length == 2 )
		String word1 = args[0];
		String word2 = args[1];
	else
		System.out.println("You must enter two command line arguments!");
You will get an "else without if" compile-time error. The compiler thinks the first line is for the true path of the if, then thinks the "if" structure is done.

Notice that some of the examples test the size of the args array:

	if ( args.length == 2 )
This test gives us a way to make sure that the correct number of command line arguments are passed into our program.

The first homework assignment asked to print a word passed on the command line:

		System.out.println(args[0]);
What happened when you ran the program without entering a command line argument?

You got an "IndexOutOfBoundsExcepton" because you were trying to access an element in an array that didn't exist.

To prevent the error, use the if statement:

	if ( args.length == 1 ) System.out.println(args[0]);
	else System.out.println("No Command-Line Arguments!!");
All homework assignments from this point on will be required to test the length of the args array if it is used in the program. This is one way of trapping possible errors in a program.

"if" statements are good when you want to make a choice between two different paths. But, what if you want to choose among more than two?

Use the "switch" statement.

NOTE: The "switch" statement only uses int or char datatypes. NOT String as indicated on the laminated study guide from the bookstore.

An example of a switch statement that will assign a message based on a letter grade:


	switch ( grade ) {
		case 'A':
			msg = "Excellent";
			break;
		case 'B':
			msg = "Good";
			break;
		case 'C':
			msg = "Fair";
			break;
		case 'D':
			msg = "Poor";
			break;
		case 'F':
			msg = "Failed";
			break;
		default:
			msg = "Unknown Value";
	}
Each path you want to take is a case based on the value of your variable. The "break" is necessary so that the JVM knows when to stop executing code for a case. The "default" covers the case when the value does not match any of the other cases.