In programming, we use If Statements to allow our programs to make decisions. For example, a program might want to ask the user if they're sure they want to exit a program, and if the user says yes, the program terminates. In another example, a program might ask the user to enter a log-in name to use an order entry system. If the name is valid, the order entry screen is displayed. If the name is not valid, an error message appears.
If statements can be single-sided, double-sided, or can be multi-sided. We'll look at each of these and try some examples.
A single sided If in Java has the general form:
if (relationalExpression) { // code to execute if relationalExpression // evaluates to true }
The if-block (the code between the braces) only executes if the relational expression evaluates to true. If there is only one statement in the if-block, you can omit the braces. For example, this is perfectly valid:
if (x > 3) System.out.println("x is larger than 3");
However, this code below will not give the results you would expect:
if (x > 3) System.out.println("x is larger"); System.out.println(" than 3);
In this example, the second print statement is not part of the if-block and will be executed regardless of whether or not x > 3 is true or false. To try this out, you can enter this code segment into a new program and declare/initialize the variable x to 5, then run the program. Then change x's value to 2 and run the program.
For beginning programmers, or programmers new to a Java style of syntax, I would get into the habit of leaving the braces on all the time until you become comfortable with leaving them off. As you'll see later on, leaving off the braces at the wrong time can cause some serious logic errors.
1. Write a program that asks the user for a number less than 100. If the user's input is 100 or more, display a message on the console.
2. Modify the program: Instead of checking to see if the number is 100 or more, check to see if the number is less than 100. If it is, calculate the square root of the number, the square of the number, and the cube of the number. Then display these on the console.
A double-sided if has two actions: one to perform if the condition is true, and one to perform if the condition is false. The general form of the double-sided If statement is:
if (relationalExpression) { // code to execute if relationalExpression // evaluates to true } else { // code to execute if relationalExpression // evaluates to false }
Note that we still only have one condition, but now we have two blocks of code. The if-block executes when the condition is true, and the else-block executes when the condition is false. Only one of the two blocks will execute!
As with the single-sided If, if you have only one code statement in either the if-block or the else-block, you can leave off the braces. For example, all of these are acceptable:
if (x > 3) System.out.println("x is bigger than 3"); else System.out.println("x is 3 or less"); ... if (x > 3) { System.out.println("x is bigger"); System.out.println(" than 3"); } else System.out.println("x is 3 or less"); ... if (x > 3) System.out.println("x is bigger than 3"); else { System.out.println("x is 3"); System.out.println(" or less"); }
Write programs for each of the following: