Java Beginners - Java For Beginners Java Conditions Rating: 0.0/5 (0 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java Conditions - Making Decisions with if-elseAs we saw in java Control Flow Statements, making decisions in Java is accomplished by evaluating the state of program data using conditional operators such as ==, <, and >. if statements use the boolean value returned from these conditional expressions to determine whether the accompanying block of code should be executed. If the conditional expression returns true, the code block is executed. Otherwise, the code block is skipped. if Before we look at examples of the if statement, let's again take a look at their general form: if(conditional_expression) { statements_to_execute }
This general form illustrates the simple if structure. If the conditional_expression is true, then statements_to_execute will be executed. The statements_to_execute are wrapped in curly braces {}. If statements_to_execute consists of a single statement only, the curly braces are optional. Most java programmers consider it to be good coding style to always include the curly braces. Let's look at some examples of if: //Example 1 if(a == b) { c++; }
//Example 2 if((x <= y) && (y <= z)) { System.out.println("Of course:\n"); System.out.println("x is less than or equal to z"); }
//Example 3 if(true) { System.out.println("This line always executes"); }
//Example 4 if(!(employee.isManager())) { System.out.println("Employee is not a manager."); }
- In example 1, the if tests the equality of a and b. If a and b are equal, c is incremented by 1.
- Example 2 evaluates a compound conditional expression. First it tests whether x is less than or equal to y. If this is true, then it tests if y is less than or equal to z. If that is also true, then it prints the two lines of output.
- In example 3, instead of passing the if statement a conditional expression that returns a boolean value, we pass it the literal boolean value "true". The code block in this example will always be executed because true always evaluates to true.
- Example 4 introduces the not operator !. ! negates a boolean value. In this example, if employee.isManager() returns true, the ! operator will toggle it to false and the if statement will not execute the code block. If employee.isManager() returns false, the ! operator will toggle it to true and the code block will execute.
if-else What if you want to execute one block of code when an expression evaluates to true but execute a different block of code when the same expression evaluates to false? The if statement alone is not enough because it only controls a single block of code. There are two ways to accomplish this. The first way is to use two if statements with mutually exclusive conditional expressions. The second way is to use an if-else structure. else is always used in conjunction with, and following, an if statement. else is the Java keyword designed for those situations where you must run exactly one of two code blocks. Lets look at some examples of if-else: //Example 1 if(a == b) { c++; } if(a != b) { c--; }
//Example 2 if(a == b) { c++; } else { c--; }
- Example 1 uses two mutually exclusive if statements to simulate an if-else construct. In this example only one of the two if statements will be executed. This is less efficient that using an if-else to do the same thing.
- Example 2 is the if-else version of the previous example . You can see the if-else requires less code than two ifs and requires one less operation be performed. (The a != b is never explicitly tested.)
if-else structures can be followed by more if-else structures. This creates a control structure known as an if-else-if. The if-else-if structures can continue indefinitely, but many if-else-ifs strung together is usually a sign of a weak design. What differentiates a set of if statements from a set of if-else-if statements is that with the if-else-if statements, only one of the code blocks will be executed. Lets look at some examples of if-else-if: //Example 1 if(color == BLUE)) { System.out.println("The color is blue."); } else if(color == GREEN) { System.out.println("The color is green."); }
//Example 2 if(employee.isManager()) { System.out.println("Is a Manager"); } else if(employee.isVicePresident()) { System.out.println("Is a Vice-President"); } else { System.out.println("Is a Worker"); }
- Example 1 shows a simple if-else-if. In this case if the color equals BLUE or GREEN, the respective code block is executed. If color equals any other value none of the code blocks will be executed.
- Example 2 uses the if-else-if structure with an ending else. In this example two different conditional expressions are tested and if neither is true, then the else statement's code block is executed.
1 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|