Decision Making Statements,The if Statement, SIMPLE IF STATEMENT, The If…else Statement, Nesting of IF..Else Statements, THE else if ladder, The Switch Statement, rules apply to a switch statement
Size: 1.04 MB
Language: en
Added: Apr 26, 2014
Slides: 13 pages
Slide Content
Decision Making and Branching
In JAVA
Decision Making Statements
•Therearetwotypesofdecisionmakingstatementsin
Java.Theyare:
ifstatements
switchstatements
The if Statement
•The if statement is a powerful decision making statement and is used to
control the flow of execution of statements. The if statement is Java’s
conditional branch statement. It can be used to route programexecution
through two different paths. Here is the general form of the if statement:
if (condition) statement1;
•the if statement may be implemented in different forms depending on the
complexity of condition to be tested.
•Simple if Statement
•If…else Statement
•Nested if..else Statement
•else if ladder
SIMPLE IF STATEMENT
•The general form of a simple if statement is
if(test_expression)
{
Statement_true_block;
} Statement_x;
•The statement block may be a single statement or a
group of statement. If the test expression is true then
statement_true_blockwill be executed otherwise it
will be skipped.
The If…else Statement
•The If…else Statement is an extension of the simple if statement. An if
statement can be followed by an optionalelsestatement, which executes
when the Boolean expression is false.
•Syntax:
if(test expression)
{
True-block statement;
}else
{
False-block statement;
}Statement-x;
Nesting of IF..Else Statements
When a series of decision are involved then we may have to use more
than one if…else statement in nested form as follows:
The general syntax is
if(test condition1)
{
if(test condition2)
{ Statement1;
}
else
{ Statement2;
}
}
else
{ Statement3;
}Statement-x
The Switch Statement
switchstatement
allows a variable to be
tested for equality
against a list of values.
Each value is called a
case, and the variable
being switched on is
checked for each case.
The syntax of enhanced for loop is:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case
statements.
default : //Optional
//Statements
}
rules apply to a switch statement
•The variable used in a switch statement can only be a byte,
short, int, or char.
•You can have any number of case statements within a
switch. Each case is followed by the value to be compared to
and a colon.
•The value for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
•When the variable being switched on is equal to a case, the
statements following that case will execute until
abreakstatement is reached.
Continued..
•When abreakstatement is reached, the switch terminates,
and the flow of control jumps to the next line following the
switch statement.
•Not every case needs to contain a break. If no break
appears, the flow of control willfall throughtosubsequent
cases until a break is reached.
•Aswitchstatement can have an optional default case, which
must appear at the end of the switch. The default case can
be used for performing a task when none of the cases is
true. No break is needed in the default case.
The ?; operator
•The value of a variable often depends on whether a
particular booleanexpression is or is not true and on
nothing else. For instance one common operation is
setting the value of a variable to the maximum of two
quantities.
•In Java you might write
if (a > b) {max = a;
}
else {
max = b;}
The ?; operator
•Setting a single variable to one of two states based on a
single condition is such a common use ofif-elsethat a
shortcut has been devised for it, the conditional
operator, ?;. Using the conditional operator you can
rewrite the above example in a single line like this:
max = (a > b) ? a : b;
•(a > b) ? a : b;is an expression which returns one of two
values,aorb. The condition,(a > b), is tested. If it is
true the first value,a, is returned. If it is false, the
second value,b, is returned