|
Flow Control and Exception
Handling
Write
code using if and switch statements and identify legal
argument types for these statements.
The if statement
The
legal argument type for if
statements is a boolean
value i.e. true, false or any expression which
evaluates to these values. In Java true and false are
not represented by integers like in C or C++.
Only
one statement can appear after
if or else. If you
want to include more statements, you will need to
create a block using {}. You can also include the
curly braces when using the if,
even if there is only one statement in each clause.
This makes it easy to add another statement at a later
time, and you do not have to worry about forgetting
the braces.
Nested if statements
When
you nest if statements,
the main thing to remember is that an
else statement always
refers to the nearest if
statement that is within the same block as the
else and that is not
already associated with an else.
The if-else-if ladder
The
if statements are
executed from the top down. As soon as one of the
conditions controlling the if
is true, the statement associated with that
if is executed, and the
rest of the ladder is bypassed. If none of the
conditions is true, then the final
else statement will be
executed. The final else
acts as a default condition; that is, if all other
conditional tests fail, then the last
else statement is
performed. If there is no final else and all other
conditions are false, then no action will be taken.
The switch statement
It is
Java’s multi-way 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.
The
switch expression must be
of type byte,
short,
int, or
char; each of the values
specified in the case statements must be of a type
compatible with or assignable to the expression. Each
case value must be a constant, not a variable.
Duplicate case values are not allowed. A
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. This has the effect of
jumping out of the switch.
The
break statement is
optional. If it is omitted, execution will continue on
into the next case. It is sometimes desirable to have
multiple cases without break
statements between them.
Nested switch statements
You
can use a switch as part
of the statement sequence of an outer
switch. Since a
switch statement defines
its own block, no conflicts arise between the case
constants in the inner
switch and those in the outer
switch.
The
important features of switch
statement:
-
The switch
differs from the if in
that switch can test
for equality, whereas if
can evaluate any type of
boolean expression.
-
No two case constants in the same
switch can have
identical values. Of course, a
switch statement enclosed by an outer
switch can have case
constants in common.
-
A switch
statement is usually more efficient than a set of
nested ifs.
section2-1 | section2-2 | section2-3
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
If you wish to download the complete notes
(around 100 pages - pdf or doc file) for a small price, click here. |