Java’s
Selection Statements
Java supports two selection
statements: if and switch. These statements allow you to
control the flow of your program’s execution based upon conditions known only
during run time.
The if statement is Java’s
conditional branch statement. It can be used to route program execution through
two different paths. Here is the general form of the if statement:
if (condition) statement1; else statement2;
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It
looks like this:
if(condition) statement;
else if(condition) statement;
else if(condition) statement;
...
else statement;
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;
switch
The switch statement is
Java’s multiway 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. Here is the
general form of a switch statement:
switch (expression) { case value1:
// statement sequence break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence }
The 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 the expression. (An enumeration value can also be used to
control a switch statement.
Iteration
Statements
Java’s iteration statements are for, while, and do-while. These
statements create what we commonly call loops.
As you probably know, a loop repeatedly executes the same set of instructions
until a termination condition is met. As you will see, Java has a loop to fit
any programming need.
while
The while
loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression
is true. Here is its general form: while(condition)
{
// body of loop }
The condition can be any
Boolean expression. The body of the loop will be executed as long as the
conditional expression is true. When condition
becomes false, control passes to the next line of code immediately following
the loop. The curly braces are unnecessary if only a single statement is being
repeated.
do-while
if the conditional expression controlling a while loop is initially
false, then the
body of the loop will not be executed at all. However, sometimes it is
desirable to execute the body of a loop at
least once, even if the conditional expression is false to begin with. In
other words, there are times when
you would like to test the termination expression at the end of the loop rather
than at the beginning.
The do-while
loop always executes its body at least once, because its conditional expression
is at the bottom of the loop. Its general form is
do {
//
body of loop } while (condition);
Each iteration of the do-while
loop first executes the body of the loop and then evaluates the conditional
expression. If this expression is true, the loop will repeat. Otherwise, the
loop terminates.
For Loop:
Beginning with JDK 5, there are two forms of the for loop. The first is the traditional form that has been in use
since the original version of Java. The second is the new ―for-each‖ form. Both
types of for loops are discussed
here, beginning with the traditional form. Here is the general form of the
traditional for statement:
for(initialization; condition; iteration) { // body }
If only one statement is being repeated, there is no need for the curly
braces. The for loop operates as
follows. When the loop first starts, the initialization
portion of the loop is executed. Generally, this is an expression that sets the
value of the loop control variable,
which acts as a counter that controls the loop. It is important to understand
that the initialization expression is only
executed once. Next, condition is
evaluated. This must be a Boolean expression. It usually tests the loop control
variable against a target value. If this expression is true, then the body of
the loop is executed. If it is false, the loop terminates. Next, the iteration portion of the loop is
executed. This is usually an expression that increments or decrements the loop
control variable.
For-Each
Beginning with JDK 5, a second form of for was defined that implements a ―for-each‖ style loop. As you may
know, contemporary language theory has embraced the for-each concept, and it is
quickly becoming a standard feature that programmers have come to expect. A
foreach style loop is designed to cycle through a collection of objects, such
as an array, in strictly sequential fashion, from start to finish. Unlike some
languages, such as C#, that implement a for each loop by using the keyword foreach, Java adds the for-each
capability by enhancing the for
statement. The advantage of this approach is that no new keyword is required,
and no preexisting code is broken. The for-each style of for is also referred to as the enhanced
for loop. The general form of the
for-each version of the for is shown
here:
for(type
itr-var : collection) statement-block
Here, type specifies the type
and itr-var specifies the name of an iteration variable that will receive the
elements from a collection, one at a time, from beginning to end. The
collection being cycled through is specified by collection. There are various types of collections that can be used
with the for, but the only type used
in this chapter is the array. With each iteration of the loop, the next element
in the collection is retrieved and stored in itr-var. The loop repeats until all elements in the collection have
been obtained. Because the iteration variable receives values from the
collection, type must be the same as
(or compatible with) the elements stored in the collection. Thus, when
iterating over arrays type must be
compatible with the base type of the array.
The for-each style for
automates the preceding loop. Specifically, it eliminates the need to establish
a loop counter, specify a starting and ending value, and manually index the
array. Instead, it automatically cycles through the entire array, obtaining one
element at a time, in sequence, from beginning to end. For example, here is the
preceding fragment rewritten using a for-each version of the for:
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int
sum = 0;
for(int x: nums) sum += x;
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.