Loops in JavaScript Assignment Help
Loops in JavaScript
Looping statement is used to repeat a specific task several times until the criteria is satisfied.
In Java script there are three types of looping such as
- While
- For looping
- do-while looping.
8.1 While loop
While loop repeats the given task until the expression returns false value. Entry point of the while loop is controlled through condition check. It is otherwise called as entry – controlled loop.
Syntax:
{` While (constraint) { Assigned task; Incrementing(++) or decrementing(--); } `}
Example:
![Loops loops img1](https://www.assignmenthelp.net/webimg/ahp/javascript-assignment-help/loops-img1.jpg)
8.2 For ... loop
A loop is to perform an assigned task repeatedly while a given constraint returns true.
Syntax:
{` for (initialization; condition-expression; increment or decrement) { //Assigned task } `}
Here is the flow of execution in a 'for' loop
- initialization phase:
- Constraint Checking
- After finishing the assigned task, it moves to incrementation and decrementation phase. This phase modifies the loop controlling parameters.
- The control moves to phase 2. Repeat 2 & 3. Otherwise it terminates the for loop.
Declaring and assigning values for the variables. Initial stage of looping is assigning values to variables.
Each part of for loop is terminated with semicolon(;).
In this phase, the expression will be evaluated. If the constraint is satisfied, the loop allows the program to perform assigned task. Otherwise the control transferred out of the for loop.
The above mentioned task illustrated in detail through the below figure.
Example:
for (i=1;i<=10;i++)
Initially the variable i holds the value ‘1’. It checks with the maximum value ‘10’ whether it is less than or equal to 10. Yes, the condition is satisfied. i value will be incremented by ‘1’. Now the i value is ‘2’. Again the condition is checked. Yes condition is true. Likewise, the i value is incremented by ‘1’until it reached the value ‘10’.
For-loop architecture
![Loops loops img2](https://www.assignmenthelp.net/webimg/ahp/javascript-assignment-help/loops-img2.jpg)
Example:
![Loops loops img3](https://www.assignmenthelp.net/webimg/ahp/javascript-assignment-help/loops-img3.jpg)
8.3 Do—while
Do-while loop is primarily doing the execution of body of statements followed by condition checking. Whether the constraint is ‘true’ or ‘false’ the assigned task performed only once. It is also termed an exit-controlled loop. The termination point of the block only ‘Do..while’ statement checks the constraint.
Syntax:
{` do { Block of statements } while (condition); `}
Example:
![Loops loops img4](https://www.assignmenthelp.net/webimg/ahp/javascript-assignment-help/loops-img4.jpg)
8.4 Break Statement
It immediately terminates the execution of looping statement. It is always coupled with if statement. When the program encounters the keyword break, the control is transferred out of the loop.
Syntax:
{` if (condition); break; `}
8.5 Continue Statement
It immediately skips the execution of current looping statement. It is always coupled with if statement. When the program encounters the keyword break, the control is transferred to the next iteration.
Syntax:
{` if (condition); continue; `}
- Namespaces
- Preprocessor Directives
- Regular Expressions
- Exception Handling
- File I/O
- Attributes
- Reflection
- Properties
- Indexers
- Delegates
- Events
- Collections
- Generics
- Anonymous Methods
- Unsafe Code
- Multithreading
- Variables Declarations
- Build In Types
- Operators
- Unary Operators
- Binary Operators
- Array Operations