Monday, January 27, 2014

Decisions

The If Statement

The if statement allows a program to carry out different actions depending on the nature of the data to be processed.

The if statement is used to implement a decision.

When a condition is fulfilled, one set of statements is executed. Otherwise, another set of statements is executed.

Ex:

int actualFloor;
if (floor > 13)
{
      actualFloor = floor - 1;
}
else
{
     actualFloor = floor;

}

In our example, each branch of the if statement contains a single statement. You can include as many statements in each branch as you like.

Sometimes, it happens that there is nothing to do in the else branch of the statement. In that case, you can omit it entirely.

Note: Common error is, placing semicolon(;) after if condition.

The conditional operator

Java has a conditional operator of the form
       condition ? value1 : value2

The value of that expression is either value1 if the test passes or value2 if it fails.

Comparing numbers and strings

Every if statement contains a condition. In many cases, the condition involves comparing two values. We use relational operators for this.

In Java, = is assignment operator and == is equality operator.

To test whether two strings are equal to each other, you must use the method called equals: if (string1.equals(string2)) ....
Note: Do not use the == operator to compare strings. Use the equals method instead.

Lexicographic ordering of strings

If two strings are not identical to each other, you still may want to know the relationship between them.

The compareTo method compares strings in “lexicographic” order.

string1.compareTo(string2) < 0 then the string string1 comes before the string string2.

When comparing two strings, you compare the first letters of each word, then the second letters, and so on, until one of the strings ends or you find the first letter pair that doesn’t match.

Multiple alternatives

Use if - else if - else if - .... - else block to check for multiple alternatives.
When using multiple if statements, test general conditions after more specific conditions.
The Switch statement

An if/else if/else sequence that compares a value against several alternatives can be  implemented as a switch statement.

int digit = . . .;
switch (digit)
{
case 1: digitName = "one"; break;
case 2: digitName = "two"; break;
case 3: digitName = "three"; break;
case 4: digitName = "four"; break;
case 5: digitName = "five"; break;
case 6: digitName = "six"; break;
case 7: digitName = "seven"; break;
case 8: digitName = "eight"; break;
case 9: digitName = "nine"; break;
default: digitName = ""; break;

}

In switch, it is obvious that all branches test the same value.

  • The values in the case clauses must be constants.
  • They can be integers or characters. In Java 7, strings are permitted as well.
  • Every branch of switch should be terminated by break instruction. If break is missing execution falls through next branch, and so on.
Nested branches

It is often necessary to include an if statement inside another. Such an arrangement is called a nested set of statements.


Nested decisions are required for problems that have two levels of decision making.

In theory, nesting can go deeper than two levels.

Boolean variables and operators


Sometimes, you need to evaluate a logical condition 
in one part of a program and use
it elsewhere.
To store a condition that can be true or false, you use a Boolean variable.

In Java, the boolean data type has exactly two values, denoted false and true.

boolean failed = true;

You can use the value later in your program to make a decision:
if (failed) // Only executed if failed has been set to true
{
. . .
}

When you make complex decisions, you often need to combine Boolean values. An operator that combines Boolean conditions is called a Boolean operator.

Note: In Java, a boolean value cannot be cast into a value of another type, nor can a value of another type be cast into a boolean value.

In Java, the && operator (called and) yields true only when both conditions are true. The || operator (called or) yields the result true if at least one of the conditions is true.

To invert a condition, use the ! (not) operator.


No comments:

Post a Comment