Introduction:
Generally a program executes it's statements from beginning to end. But not many programs execute all their statements in strict order from beginning to end. Most programs decide what to do in response to changing circumstances.
In a program statements may be executed sequentially, selectively or iteratively. Every programming language provides constructs to support sequence,selection or iteration.
When a program breaks the sequential flow and jumps to another part of the code, it is called
selection or branching.
When the branching is based on a particular condition, it is known as
conditional branching.
If branching takes place without any condition, it is known as
unconditional branching.
Java language supports two types of selections statements:
if and switch. In addition, in certain circumstances '?' operator can be used as an alternative to if statements.
DECISION MAKING WITH IF STATEMENT:
An if statement tests a particular condition; if the condition evaluates to true , a course of action is followed i.e a statement or set of statements is executed . Otherwise (if the condition evaluates to false) ,the course-of-action is ignored.
The if statement may be implemented in different forms depending on the complexity of conditions to be tested.
- Simple if statement
- if....else statement
- Nested if....else statement
- else if ladder
SIMPLE IF STATEMENT:
The
syntax of the if statement is shown below:
if(expression)
statements;
Where a statement may consist of a single statement , a compound statement, or nothing. The expression must be enclosed in parantheses. If the expression evaluates to true, the statement is executed, otherwise ignored.
Following figure illustrates the if construct of java:
|
Flow Chart of IF control |
Example:
if(a>10)
if(b<15)
c=(a-b)*(a+b);
If the value of a is greater than 10, then the following statement is executed which in turn is another if statement. This if statement tests b and if b is less than 15, then c is claculated.
THE IF....ELSE STATEMENT:
This form of if allows either-or condition by providing an else clause. The syntax of the if-else statement is the following:
if(expression)
statement-1;
else
statement-2;
If the expression evaluates to true i.e., a non-zero value, the statement-1 is executed, otherwise statement-2 is executed. The statement-1 and statement-2 can be a single statement, or a compound statement, or a null statement.
Following figure illustrates the if else construct of java:
|
Flow chart of if-else control |
Example:
int a,b;
if(a<=b)
a=0;
else
b=0;
NESTED IF....ELSE STATEMENT:
A Nested if is an if that has another if in it's 'if's' body or in it's else's body. The nested if can have one of the following three forms:
1. if(expression1)
{
if(expression2)
statement-1;
else
statement-2;
}
else
body of else;
2. if(expression1)
body-of-if;
else
{
if(expression2)
statement-1;
else
statement-2;
}
3. if(expression1)
{
if(expression2)
statement-1;
else
statement-2;
}
else
{
if(expression3)
statement-3;
else
statement-4;
}
THE ELSE-IF LADDER:
A common programming construct in the java is the if-else-if ladder , which is often also called the if-else-if staircase because of it's appearence.
It takes the following general form:
if(expression1) statement1;
else
if(expression2) statement2;
else
if(expression3) statement3;
:
else statement n;
The expressions are evaluated from top downward. As soon as expression evaluates to true , the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expression are true, the final else gets executed. If the final else is missing , no action takes place if all other conditions are false.
Although it is technically correct to have indentation in the if-else-if ladder as shown above. however, it generally leads to overly deep indentation . For this reason, the if-else-if ladder is generally indented like this:
if(expression1)
statement 1;
elseif(expression2)
statement 2;
elseif(expression3)
statement 3;
:
else
statement n;
THE SWITCH STATEMENT:
Java provides a multiple branch selection statement known as
switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.
The general form of switch is:
switch(expression)
{
case value1:
Codesegment 1
case value2:
Codesegment 2
...
case valuen:
Codesegment n
default:
default Codesegment
}
A
switch statement is used for multiple way selection that will branch to different code segments based on the value of a variable or an expression . The optional default label is used to specify the code segment to be executed when the value of the variable or expression does not match with any of the case values. if there is no
break statement as the last statement in the code segment for a certain case, the execution will continue on into the code segment for the next case clause without checking the case value.
NESTED SWITCH:
You can use
switch as part of the statement sequence of an
outer switch. This is called a
nested switch. Since a switch statement defines in its own block, no conflicts arise between the case statements in the inner switch and those in the outer switch.
switch(count)
{
case 1:
switch(target)
{
case 0:
System.out.println("target is zero");
break;
case 1:
System.out.println("target is one");
break;
}
break;
case2: //. . .
Here the case 1: statement is inner switch does not conflict with the case 1: statement in the outer switch. The count variable is only compared with the list of cases at the outer level. if the count is 1 , then target is compared with the inner list cases.
No two case constants in the same switch can have individual values. A switch statement enclosed by an outer switch can have case constants in common.
THE ?: OPERATOR:
Java has an operator that can be used as an alternative to if statement. You are familier with this operator, the conditional operator ?: This operator can be used to relace if-else statements of the general form:
if (expression 1)
expression 2;
else
expression 3;
The above form of if can be alternativlely written using ?: as follows:
expression 1? expression 2: expression 3;