If and Switch

How do you write if and switch statements in Java code?

Frequently in a program you have to make decisions…
	if something is true then 
		do this
	else
		do that

Program statements like this are called flow of control statements. In Java there are two types: if and switch.

The if statement

An if statement consists of:
This may be followed by a single line of code to execute if the conditional statement is true or it may be followed by a number of statements enclosed in braces, {} all of which will be executed if the conditional is true. Look at these examples:

	if(x == 3)
		System.out.println("Menage " + á + " trois anyone?"); 

	if(myDog == null)
	{
		System.out.println("Where, oh where has my little dog gone?);
		System.out.println("Where, oh where can he be?);
	}

You can also create if statements in which there is code to be executed when the conditional is false. These statements are placed in an else block. For example:
	if(x < 0)
		System.out.println("X is negative."); 
	else
		System.out.println("X is positive"); 

You can also construct if statements in which there is more than one if. These are else if blocks. Here is a simple example:
	if(x < 0)
		System.out.println("X is negative."); 
	else if(x == 0)
		System.out.println("X is zero."); 
	else
		System.out.println("X is positive"); 

Just as in the if block you can enclose more than one statement in the else if and the else blocks in braces ({}.

Conditional (Logical) Operators

There are a number of conditional operators you can use in if statements in additional to the ones in the examples above:
Operator
Meaning
==
Equals
Yes, that 2 equal signs.
<
Less than
>
Greater than
<=
Less than or equal
>=
Greater than or equal
!=
Not equal


Multiple Contitionals

You can construct a conditional statement with more than one item to be checked. If you want to check to see if both x AND y have certain values you write two conditionals and then AND them together using the logical AND operator which consists of two ampersands: &&. Here are two examples:




If, on the other hand, you want to check to see if either of two variables has a certain value, e.g. either a OR b have certain values then you write two conditionals and OR them together using the logical OR operator which consists of two vertical bars: ||. Here are two examples:





In fact you can create quite complex conditionals by combining both the AND and the OR operators as shown in this example..."



In come programming situations you may have a variable in which you need to check to see if it is one of several possible values. You could do this with multiple else if blocks but that can get to be tedious. Fortunately Java has another statement which makes this easy to do. It is called the switch statement. The figure below shows the basic structure of the switch statement. It consists of the keyword switch followed by an integer value (short, int, or char) in parentheses. Then in a set of braces there can be any number of case statements. The syntax of each consists of the keyword case followed by a possible value for the switch variable. Remember that only integer values are allowed. After the value is a semicolon, : and then all the statements to be executed if this case holds the correct value. The set of statements for each case value must be ended with the keyword break or execution will fall through and continue with the next case statement. You can also have as a "last case" a default statement if the none of the other case statements was for the current value of the variable being "switched" on.



Below is an example of a real switch statement in code. The variable being switched on, x, is an int variable.



Sometimes you might want to take advantage of the fact that without a break statement at the end of a case you fall through to the next case. In the example below the switch variable ans is a char variable. These variables are also considered an integer type. The user was asked to enter an answer to a multiple choice question. The programmer wants to accept the correct answer whether the user entered an upper or lower case letter. As you can see, if the user entered an upper case letter it will "switch" on that letter, but immediately fall into the next case statement which is the lower case letter. Both these cases then get handled together by the same code. The lower case letter case then ends with the break statement.



Finally, here is a slightly more complex switch statement in a separate program called SwitchOff. The switch statement will be executed each time through the for loop with different values for the loop counter i.