শুক্রবার, ১৩ ফেব্রুয়ারি, ২০১৫

JavaScript

Different Kinds of Loops(JavaScripts)

JavaScript supports different kinds of loops:
  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

The For Loop

The for loop is often the tool you will use when you want to create a loop.
The for loop has the following syntax:
for (statement 1; statement 2; statement 3) {
    code block to be executed
}
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed.
<!DOCTYPE html>
<html>
<body>

<p>Click the button to loop through a block of code five times.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var text = "";
    var i;
    for (i = 0; i < 5; i++) {
        text += "The number is " + i + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>
Result
Click the button to loop through a block of code five times.
From the example above, you can read:
Statement 1 sets a variable before the loop starts (var i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5).
Statement 3 increases a value (i++) each time the code block in the loop has been executed.

বৃহস্পতিবার, ৫ ফেব্রুয়ারি, ২০১৫

Looping Statements IN JAVA

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.