Looping Statements
Looping statements enable you to execute blocks of statements repeatedly.
The Java programming language supports four types of loop constructs:
for (two variations), while, and do loops. The while loop and one
variation of the for loop test the loop condition before executing the loop
body; the do loop checks the loop condition after executing the loop body.
This implies that the do loop always executes the loop body at least once.
The second variation of the for loop, known as the enhanced for loop,
which is used to iterate through the elements from a group (such as an
array), has no explicit loop condition.
The for Loops
The for loop syntax is:
for ( <init_expr>; <test_expr>; <alter_expr> )
<statement_or_block>
For example:
for ( int i = 0; i < 10; i++ )
System.out.println(i + " squared is " + (i*i));
However, you should place all loop-clause statements into a block. For
example:
for ( int i = 0; i < 10; i++ ) {
System.out.println(i + " squared is " + (i*i));
}
In the previous example, int i is declared and defined within the for
block. The variable i is accessible only within the scope of this particular
for block.
Note – The Java programming language permits the comma separator in a
for() loop structure. For example,
for (i = 0, j = 0; j < 10; i++, j++) { } is legal, and it initializes
both i and j to 0, and increments both i and j after executing the loop
body.
The while Loop
The while loop syntax is:
while ( <test_expr> )
<statement_or_block>
For example:
int i = 0;
while ( i < 10 ) {
System.out.println(i + " squared is " + (i*i));
i++;
}
Ensure that the loop-control variable is initialized appropriately before the
loop body begins execution. You must update the control variable
appropriately to prevent an infinite loop.
Looping statements enable you to execute blocks of statements repeatedly.
The Java programming language supports four types of loop constructs:
for (two variations), while, and do loops. The while loop and one
variation of the for loop test the loop condition before executing the loop
body; the do loop checks the loop condition after executing the loop body.
This implies that the do loop always executes the loop body at least once.
The second variation of the for loop, known as the enhanced for loop,
which is used to iterate through the elements from a group (such as an
array), has no explicit loop condition.
The for Loops
The for loop syntax is:
for ( <init_expr>; <test_expr>; <alter_expr> )
<statement_or_block>
For example:
for ( int i = 0; i < 10; i++ )
System.out.println(i + " squared is " + (i*i));
However, you should place all loop-clause statements into a block. For
example:
for ( int i = 0; i < 10; i++ ) {
System.out.println(i + " squared is " + (i*i));
}
In the previous example, int i is declared and defined within the for
block. The variable i is accessible only within the scope of this particular
for block.
Note – The Java programming language permits the comma separator in a
for() loop structure. For example,
for (i = 0, j = 0; j < 10; i++, j++) { } is legal, and it initializes
both i and j to 0, and increments both i and j after executing the loop
body.
The while Loop
The while loop syntax is:
while ( <test_expr> )
<statement_or_block>
For example:
int i = 0;
while ( i < 10 ) {
System.out.println(i + " squared is " + (i*i));
i++;
}
Ensure that the loop-control variable is initialized appropriately before the
loop body begins execution. You must update the control variable
appropriately to prevent an infinite loop.
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন