Contol Structures
Contol Structures
java
Selection statements
Iterative statements
Jump statements
Java’s Selection Statements
if (condition)
statement1;
else
statement2;
int a, b;
// ...
if(a < b)
a = 0;
else
b = 0;
Nested ifs
Example:
if(i == 10) {
if(j < 20)
a = b;
if(k > 100)
c = d; // this if is
else
a = c; // associated with this else
}
else
a = d;
The if-else-if Ladder
A common programming construct that is based upon a sequence of
nested ifs is the if-else-if ladder.
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
switch
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is
zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is
one");
break;
}
break;
case 2: // ...
There are three important features
of the switch statement to note:
■ The switch differs from the if in that switch can only test for
equality, whereas if can evaluate any type of Boolean expression. That
is, the switch looks only for a match between the value of the
expression and one of its case constants.
■ 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.
Iteration Statements
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. As with all of Java’s loops, condition
must be a Boolean expression.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
} while(--n > 0);
}
}
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. The loop then iterates, first evaluating the
conditional expression, then executing the body of the loop,
and then executing the iteration expression with each pass.
This process repeats until the controlling expression is false.
Example:
class ForTick {
public static void main(String args[]) {
int n;
for( n=10; n>0; n--)
System.out.println("tick " + n);
}
}
Declaring Loop Control Variables Inside the for Loop
Often the variable that controls a for loop is only needed for the purposes
of the loop and is not used elsewhere. When this is the case, it is possible to
declare the variable inside the initialization portion of the for.
When you declare a variable inside a for loop, the scope of that variable
ends when the for statement does.
Outside the for loop, the variable will cease to exist.
Using the Comma
There will be times when you will want to include more than one statement in
the initialization and iteration portions of the for loop.
Java permits you to include multiple statements in both the initialization and
iteration portions of the for. Each statement is separated from the next by a
comma.
For example:
for( ; ; ) {
// ...
}
This loop will run forever, because there is no condition under which it
will terminate.
Nested Loops
Like all other programming languages, Java allows loops to be nested. That
is, one loop may be inside another.
When used inside a set of nested loops, the break statement will
only break out of the innermost loop.
// Using break with nested loops.
output:
class BreakLoop3 {
Pass 0: 0 1 2 3 4 5 6 7 8
public static void main(String args[]) { 9
for(int i=0; i<3; i++) { Pass 1: 0 1 2 3 4 5 6 7 8
9
System.out.print("Pass " + i + ": ");
Pass 2: 0 1 2 3 4 5 6 7 8
for(int j=0; j<100; j++) { 9
if(j == 10) Loops complete.
break; // terminate
loop if j is 10
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loops complete.");
}
}
Using break as a Form of Goto –
Labelled break
Java does not have a goto statement, because it provides a way to
branch in an arbitrary and unstructured manner.
Use of goto: the goto can be useful when you are exiting from a
deeply nested set of loops.
Java defines an expanded form of the break statement.
By using this form of break, you can break out of one or more blocks
of code.
These blocks need not be part of a loop or a switch. They can be any
block.
Further, you can specify precisely where execution will resume,
because this form of break works with a label.
Using break as a Form of Goto
The general form of the labeled break statement is shown here:
break label;
Here, label is the name of a label that identifies a block of code. When
this form of break executes, control is transferred out of the named
block of code.
The labeled block of code must enclose the break statement, but it does
not need to be the immediately enclosing block. This means that you can
use a labeled break statement to exit from a set of nested blocks.
A label is any valid Java identifier followed by a colon. Once you have
labeled a block, you can then use this label as the target of a break
statement.
You cannot break to any label which is not defined for an enclosing block.
// Using break to exit from nested loops
class BreakLoop4 {
public static void main(String args[]) {
outer: for(int i=0; i<3; i++)
{
System.out.print("Pass " + i + ": ");
for(int j=0; j<100; j++)
{
if(j == 10)
break outer; // exit both loops
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}
This program generates the following output:
Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete.
Using continue
Continue statement continue running the loop, but stop processing
the remainder of the code in its body for this particular iteration.
System.out.println("");
}
}
}
This code uses the % operator to check if i is even. If it is, the loop
continues without printing a newline.
Labelled continue
continue may specify a label to describe which enclosing loop to continue.
The break statement results in the termination of the loop, it will come out of
the loop and stops further iterations.
class Break
{
public static void main(String args[])
{ Output:
// Illustrating break Break
System.out.println("Break\n----------------"); ---------
1
for(int i=1;i<=5;i++)
{
2
if(i==4) 3
I AM HERE
break;
System.out.println(i);
}
System.out.println(“I AM HERE");
}
}
Difference between break and continue staments.
}
}
Return
The return statement is used to explicitly return from a method.
That is, it causes program control to transfer back to the caller of the method.
At any time in a method the return statement can be used to cause execution
to branch back to the caller of the method. The return statement immediately
terminates the method in which it is executed.
Example:
// Demonstrate return.
The output :
class Return {
Before the return.
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t)
return; // return to caller
System.out.println("This won't execute.");
}
}
The final println( ) statement is not executed. As soon as return is executed,
Difference between while and do-while
loop.