Loop
A loop is a sequence of instructions that is continually repeated until a certain condition is met in programming.
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false when analyzed. A loop will continue running until the defined condition returns false.
The For Loop
For loop syntax:
for (1st statement; 2nd statement; 3rd statement) {
// code block to be executed
}
Example 1
// program to display text 10 times
const n = 10;
// looping from i = 1 to 10
// i = 1 is a variable
// i <= n is a condition
// i++ increases a value each time the code block in the loop has been executed.
for (let i = 1; i <= n; i++){
console.log('Hello world');
}
Example 2
// This program will display numbers from 1 to 5
const k = 5;
// looping from i = 1 to 5
// in each iteration, i is increased by 1
for (let i = 1; i <= k; i++){
console.log(i); // printing the value of i
}
Example 3
// program to display the sum of natural numbers
let sum = 0;
const l = 20
// looping from i = 1 to n
// in each iteration, i is increased by 1
for (let i = 1; i <= l; i++){
sum += i; // sum = sum + i
}
console.log('sum', sum);
The first statement is executed one time before the code block is executed.
The second statement defines the condition for executing the code block.
The third statement is executed every time after the execution of the code block.
While loop;
This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Here is the syntax for while loop;
while (condition) {
// body of loop
}
Example
/Displaying thenumbers from 1-6
// This program will display numbers from 1 to 6
// initialize the variable
let i= 1, m = 6;
// while loop from i = 1 to 6
while (i <= m) {
console.log(i);
i += 1;
}
A while loop evaluates the condition inside the parenthesis ().
If the condition is evaluated to true, the code inside the while loop is executed.
The condition is evaluated again.
Then the process continues until the condition is false.
When the condition evaluates to false, this is when the loop stops.
Do While Loop;
The do/while statement creates a loop that executes a block of code once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. The do/while statement is used when you want to run a loop at least one time, no matter what.
Syntax;
do {
// body of loop
} while(condition)
Example
// This program will display numbers.
let p = 1;
const r = 5;
// do...while loop from 1 to 5
do {
console.log(p);
p++;
} while(p <= r);
The body of the loop is executed at first. Then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
Then the condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
This process continues until the condition evaluates to false. Then the loop stops.
NB: do…while loop is similar to the while loop. The only difference is that in do…while loop, the body of the loop is executed at least once.
How to Add, Subtract, Divide and Multiply Numbers in JavaScript
How To add numbers in JavaScript
What you are required to do is to assign values to variables and add them together. Variable: This is something that can be changed any time. JavaScript variables are containers for storing data values.
Example 1
var a = 4;// in this state you assign the value 4 to a
var b = 6;// you assign the value 6 to b
var c = 8;// you assign the value 8 to c
var d = a+b+c;//you assign the value 18 to d which is(4+6+8)
console.log(d);
Example 2
var a = 10;
a +=2;
console.log(a);// In this case the answer will be (12)
How to subtract numbers in JavaScript
Example 1
var a = 5;
var b = 5;
var c = a-b;
console.log(c);// The answer will be (0)
Example 2
var a = 4;
a -=3;
console.log(a); // The expected ans is (1)
How to divide numbers in JavaScript
Example 1
var a = 20;
var b = 10;
var c = a/b;
console.log(c); //The correct ans is (2)
Example 2
var a = 10;
a /=2;
console.log(a); // The correct ans is (5)
How to multiply numbers using JavaScript
Example 1
var a = 5;
var b = 5;
var c = a*b;
console.log(c); // The ans should be (25)
Example 2
var a = 20;
a *=20;
console.log(a); // The ans will be (400)