Statements in Java Part-2

Loop Statements in Java:

The for Statement:

A for statement has the form for (initialization; condition; step) body:

where initialization is a variable-declaration an expression, condition is an expression of type boolean, step is an expression, and body is a statement. More generally, the initialization and step may also be comma-separated lists of expressions; the expressions in such a list are evaluated from left to right when the list is evaluated. The initialization, condition, and step may be empty. An empty condition is equivalent to true. Thus for (;;) body means "forever execute body." The for statement is executed as follows:


1. The initialization is executed.
2. The condition is evaluated. If it is false, the loop terminates.
3. If it is true, then
a. The body is executed.
b. The step is executed.
c. Execution continues at (2).

The whi1e Statement:

A while statement has the form while (condition) body where condition is an expression of type boolean, and body is a statement. It is executed as follows:

1. The condition is evaluated. If it is false, the loop terminates.
2. If it is true, then
a. The body is executed.
b. Execution continues at (1).

The do-while Statement:

A do-while statement has the form do body while (condition);

where condition is an expression of type boolean, and body is a statement. The body is executed at least once, because the do-while statement is executed as follows:

1. The body is executed.
2. The condition is evaluated. If it is false, the loop terminates.
3. If it is true, then execution continues at (1).

Nested for Loops:

This program prints a four-line triangle of asterisks (*):

for (int i=1; i<=4; i++) {
for (int j=1; j<=i; j++)
System.out.print("*");
System.out.println();
}

Infinite Loop Because of Misplaced Semicolon:

Here a misplaced semicolon (;) creates an empty loop body statement, where the increment i++ is not part of the loop. Hence it will not terminate but will loop forever.

int i=0;
while (i<10); style="font-weight: bold;">Using do-while:

Roll a die and compute sum until 5 or 6 comes up. Here we can use do-while but while is usually safer because it tests the loop condition before executing the loop body.

static int waitsum() {
int sum = 0, eyes;
do {
eyes = (int) (1 + 6 * Math.random());
sum += eyes;
} while (eyes < style="font-weight: bold; color: rgb(204, 0, 0);">Returns, Labeled Statements, Exits, and Exceptions:

The return Statement:

The simplest form of a return statement, without an expression argument, is return;

That form of return statement must occur inside the body of a method whose return type is void, or inside the body of a constructor. Execution of the return statement exits the method or constructor and continues execution at the place from which the method or constructor was called. Alternatively, a return statement may have an expression argument:


return expression;

That form of return statement must occur inside the body of a method (not constructor) whose return type is a super type of the type of the expression. The return statement is executed as follows: First the expression is evaluated to some value v. Then it exits the method and continues execution at the method call expression that called the method; the value of that expression will be v.


static int wdayno3(String wday) {
for (int i=0; i <>

Labeled Statements:

A labeled statement has the form label : statement

where label is a name. The scope of label is statement, where it can be used in break and continue . The label cannot be reused inside statement, except inside a local class.

Using break to Exit a Labeled Statement Block:

for(int i=0;i<5;i++){ j="0;j<5;j++){" i="=">Using continue to continue Labeled statement Block:

l1:
for(int i=0;i<5;i++){ j="0;j<5;j++){" i="=" style="font-weight: bold; color: rgb(204, 0, 0);"> The break Statement:

Executing break exits the innermost enclosing switch or loop and continues execution after that switch or loop. Executing break label exits the enclosing statement that has label label, and continues execution after that statement. Such a statement must exist in the innermost enclosing method, constructor, or initializer block.


For Example

int i=0;
for(i=0;i<10;i++){ i="=" style="color: rgb(204, 0, 0); font-weight: bold;">The continue Statement:

A continue statement is legal only inside a loop and has one of the forms

continue;
continue label;

Executing continue terminates the current iteration of the innermost enclosing loop and continues the execution at the step in for loops the condition in while and do-while loops. Executing continue label terminates the current iteration of the enclosing loop that has label label, and continues the execution at the step or the condition. There must be such a loop in the innermost enclosing method or constructor or initializer block.

RELATED POST

STATEMENTS IN JAVA PART ONE

No comments:

Post a Comment