Pages

Friday, September 7, 2012

Looping and Branching Events and Event handling

JavaScript performs several types of repetitive operations, called "looping". Loops are set of instructions used to repeat the same block of code till a specified condition returns false or true depending on how you need it. To control the loops you can use counter variable that  increments or decrements with each repetition of the loop.

JavaScript supports two loop statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the break and continue statements within loop statements.
The For Loop
for is the most common loop for mathematical calculations. It looks like following for (type var1 = inticialization; var (condition to continue); var(+ operation)) for instance

for (int i = 0; i < 10; i++) { cout << "result is: " << i << endl; }

This code will print numbers from 0 to 9 each on a new line: result is: 0 result is: 1 ... result is: 9

The While Loop
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loop checks the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.

For example, in the C programming language (as well as Java and C++, which use the same syntax in this case), the code fragment

int x = 0;
while (x < 5) {
    printf ("x = %d\n", x);
    x++;
}

first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.

Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop. For example:

while (true) {
    //do complicated stuff
    if (someCondition)
       break;
    //more stuff
}
break and continue Statements
C break statement is used to terminate any type of loop such as while loop, do while loop and for loop. C break statement terminates the loop body immediately and passes control to the next statement after the loop.

C continue statement is used to skip over the rest of the current iteration in  . After continue statement, the control returns to the top of the loop.

Here is an example of using C break and C continue statement:

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...            print n, 'equals', x, '*', n/x
...            break
...     else:
...          print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

No comments:

Post a Comment