Java Conditional Statements
Java Basics - Part 5
Forward: In this part of the series, I explain how a group of statements can be executed based on a condition.
By: Chrysanthus Date Published: 2 Sep 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The if Statement
In Java, there is a reserved word, which is “if”. The “if” must be in lowercase. This is used to check if a condition is true. If it is true, one or more statements are executed. Let us look at an example. Consider the following code:
class IfCond
{
public static void main(String[] args)
{
int hisVar = 20;
if (hisVar == 20)
{
System.out.println("I am studying Java.");
}
}
}
There are three blocks here. The outer block is known as the block for the class. You have seen this block many times. The middle block is known as the block for the Main Method. You have also seen this block many times. It is in this block of the main method that I have been writing a lot of the statements. There is another block inside the middle block (see its use below); this is the innermost block. The detail differences between the blocks will be explained much later. In this tutorial, the interest is what is in the middle block.
In the block of the main method, the first statement assigns the value 20 to the field named (identified by), hisVar. Then you have what is known as the if-construct. The if-construct begins with the reserved word, “if” and ends with the curly brace, }. What goes inside the parentheses is the condition. If this condition is true (correct), some statements are executed. The statements to be executed are in the curly brackets, which form the innermost block mentioned above. This innermost block is like independent of the middle (main method) block. In fact this innermost block is part of the if-construct; that is how you should see it. Remember, a block is a pair of curly brackets and its content. Now, identify the middle block of the main method and that of the if-construct in the above code, if you have not already done so. If there is only one statement in the block of the if-construct, you do not need the curly brackets for the if-block. If you have more than one statement, separate them with semicolons and put them within the curly brackets, {} to form the if-block.
If the condition is correct, it will be replaced with, true, internally; you do not see that. If it is wrong, it will be replaced with, false, internally; you also do not see that.
In the above code, 20 was assigned to, hisVar. So, hisVar equals 20. In the condition the equal sign is two-assignment operators: one just next to the other. In math the equal sign is the assignment operator, while in Java, the equal sign is two-assignment operators. Remember, avoid making analogy between Java and math. The if-construct above can be read like this: if hisVar equals 20 then display, 'I am studying Java.'. Since I assigned the value 20 to the field named (identified by) hisVar, the condition of the if-construct is true. So the statement in the curly brackets is executed. Try the above code.
In the above code, the statement(s) in the curly brackets is(are) executed if the condition is true. What about, if it were false? It would be false if I never assigned 20 to hisVar. If it were false, nothing will happen. That is, the statement(s) in the curly brackets will not be executed. There is an else sub construct you can attach to the if-statement. The else part is similar in coding to the if-part. However, its block (curly brackets) is executed when the if’s condition is false. The else part does not have any condition. Read and try the following code:
class ElsePart
{
public static void main(String[] args)
{
int hisVar = 36;
if (hisVar == 20)
{
System.out.println("I am studying Java.");
}
else
{
System.out.println("I am doing something else.");
}
}
}
In the above code, a value of 36 is assigned to hisVar. In the if-condition, we test if hisVar equals 20. So the condition returns false, and the statement(s) in the else-block is (are) executed. Note how the else section has been typed. Also note that else is a reserved word. In the code the if-block is not executed.
Reserved words also known as keywords, are words that have special meaning in Java and you cannot use them arbitrary. Examples of reserved words you have seen are, if, else, int, double, boolean, char, void, class, static, and main.
You may have more than one test to make in a particular situation or for the same variable. In this case you include the “else if” phrase as in the following code. Read and try it.
class ElseIfPart
{
public static void main(String[] args)
{
int hisVar = 1000;
if (hisVar == 10)
{
System.out.println("Value is small");
}
else if (hisVar == 100)
{
System.out.println("Value is medium");
}
else if (hisVar == 1000)
{
System.out.println("Value is large");
}
}
}
A value of 1000 is assigned to hisVar. The if-else-if coding will first test if hisVar is 10; if it is (which it is not) the corresponding block will display 'Value is small'. The code will then test if hisVar is 100; if it is (which it is not), the corresponding block will display, 'Value is medium'. The code will then test if hisVar is 1000; if it is, the corresponding block will display, 'Value is large'. With the if-else-if coding only one of the blocks can be executed; that is, only one of the conditions can be true (the rest should be false).
In the if-else-if coding, the very first line must be the if-condition; the rest are else-if conditions. The “else if” phrase takes a condition, but the else word never takes a condition.
Always remember this: the if-else-if coding is used only for situations where only one of the conditions is satisfied (is true).
What about the situation for an if-else-if coding where none of the conditions is true? For that situation you will need to report (inform the user) of something to that effect. This is an opportunity to give some default answer. You do this by simply adding the else (no condition) section at the end of the if-else-if coding. The following code illustrates this:
class ElseIfElsePart
{
public static void main(String[] args)
{
int hisVar = 10000;
if (hisVar == 10)
{
System.out.println("Value is small");
}
else if (hisVar == 100)
{
System.out.println("Value is medium");
}
else if (hisVar == 1000)
{
System.out.println("Value is large");
}
else
{
System.out.println("hisVar is very large");
}
}
}
Try the above code if you have not already done so. At the start of the code, 10,000 is assigned to the variable. Note that when you are applying numbers with more than 3 digits, you do not use commas (you type 10000 and not 10,000). In the code, none of the conditions is satisfied, so the last block, which does not have any condition (which is the else part), is executed. Read through the code again, to appreciate this.
Complete Syntax for if-Construct
The complete syntax for the if-Construct is:
if (condition)
{
statements
}
else if (condition)
{
statements
}
else if (condition)
{
statements
}
- - -
else
{
statements
}
Note: if the “if” or “else if” or “else” part has just one statement, then you do not need curly brackets for the statement. You need curly brackets if there is more than one statement.
The previous code is replaced by the following. Read and try it.
class Switching
{
public static void main(String[] args)
{
int hisVar = 10000;
switch (hisVar)
{
case 10:
System.out.println("Value is small");
break;
case 100:
System.out.println("Value is medium");
break;
case 1000:
System.out.println("Value is large");
break;
default:
System.out.println("hisVar is very large");
break;
}
}
}
The syntax for the switch statement is:
switch (expression)
{
case label :
statements;
break;
case label :
statements;
break;
- - -
default :
statements;
break;
}
The word, expression in the parentheses above is an integer or any expression that boils down to an integer. An example is just a variable, as in the above situation. label is a number compared to the expression to result in true or false. Each case section ends with “break;”. If the comparison is true, the statements between label and break are executed. The last situation does not have a label (corresponds to else). Note the use of colons and semicolons. If you have more than one statement for a case, separate them with semicolons. Also note the use of the curly brackets. You use the switch statement instead of the if-else-if statement, when you may want to compare the same integer variable (or expression) with many different integers.
Quotation Marks
If your value is a number (int or double) in the condition, it should not be in quotes. If it is a string, you need to have it in double quotes. If it is a char, you should have it in single quotes. There is actually more to strings than I have indicated, see later.
We take a break here and continue in the next part of the series.
Chrys