|
|
[edit] Java if Statement
|
| if statement is the most common branching statment used in any programming language. The purpose of if statement is that the programmer can decide which block of code to execute at runtime based on some values on a variable.
if (condition) statement1;
else statement2;
In the above mentioned code if the value of condition variable is evaluated as boolean true then statement1 will be executed otherwise statement2 will be executed. In no case will both statements be executed.
Example Program
public class IfTestProgram {
public static void main(String args[]) {
String doorIsOpen = args[0];
if(doorIsOpen.equals("y")) {
System.out.println("The door is OPEN"); //statement1
} else {
System.out.println("The door is CLOSE"); //statement2
}
}
}
In the above program if the value of variable doorIsOpen is equal to “y” then the system will print “The door is OPEN” otherwise it will print “The door is CLOSE”. If statement are very common statements in any program where branching in involved as it is happening in the above program.
|
[edit] Variants of Java if statement
|
| The above mentioned if statment is one of the simplest form. If statement in JAVA has two more variants 1) Nested Ifs and 2) if-else-if Ladder. In a complex program if statements could grow into a very complicated tree like structure which could led to a maintenance nightmare. There are ways to simplify those solutions.
|
[edit] Java Nested Ifs
|
if(i == 10) {
if(j < 20) a = b;
if(j > 20)
c = d; // this if is
else
a = c; // associated with this else
}
else a = d;
|
|
|
[edit] Java if-else-if Ladder
|
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
.
.
.
else
statement;
Try this code
public class IfElseProgram {
public static void main(String args[]) {
int month = 12; // December
String season;
if (month == 12 || month == 1 || month == 2)
season = "Winter";
else if (month == 3 || month == 4 || month == 5)
season = "Spring";
else if (month == 6 || month == 7 || month == 8)
season = "Summer";
else if (month == 9 || month == 10 || month == 11)
season = "Autumn";
else {
season = "Bogus Month";
System.out.println("You have entered invalid month");
return;
}
System.out.println("It is " + season + ".");
}
}
|
|
|
[edit] Java Switch Statements
|
Switch statement is another form of Selection Statement. The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements. Here is the general form of a switch statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
case value3:
// statement sequence
break;
case value4:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
|
| The expression must be of type byte, short, int, or char and each of the values mentioned in the case statements must be of a type compatible with the expression. Using any other type of expression might cause a compilation error. Each case value must be a unique literal (that is it a constant and not a variable). Duplicate case values are not allowed. This is how switch statement works : The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken. The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement.
|
|
|
| Try this code
|
public class SamleSwitch {
public static void main(String args[]) {
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("You scored Excellent");
break;
case 'B':
System.out.println("You scored Good");
break;
case 'C':
System.out.println("You scored Average");
break;
case 'D':
System.out.println("You scored below Average");
break;
}
}
}
|
|
|