Loops in JavaScript
A loop in JavaScript is a programming construct that allows you to execute a block of code repeatedly as long as a specified condition is true.
It's a fundamental tool for automating repetitive tasks and iterating over data structure like arrays or objects.
Iteration : Understanding how loops iterate over a set of instructions until a specific condition is met.
Loop Control Statements : Knowing how to control the flow of loops using statements like break and continue.
Loop termination : Identifying the conditions that determine when a loop should stop executing.
Loop optimization : Learning techniques to optimize loop performance, such as minimizing unnecessary computations within the loop body.
2. while loop
3. do-while loop
4. for...in loop
5. for...of loop
A for loop in used to execute a block of code repeatedly for a specified number of times.
Its syatax consists of three optional expressions enclosed in parentheses: initialization, condition, and increment/decrement.
Anatomy of a for Loop :
for(initialization; condition; increment/decrement)
{
  //code block to be executed
}
Initialization : Declares and initializes the loop variable.
Condition : Specifies when the loop should continue executing.
Increment/Decrement : Modifies the loop variable after each iteration.
- i=0; initialize the loop variable i to 0.
- i < 5; specifies the condition. The loop will continue as long as i is less than 5.
- i++ is the increment expression, which increases the value of i by 1 after each iteration
Output :
Enter a Number : 5
Factorial=120
Output :
Sum of Even Numbers=30
Sum of Odd Numbers=25
Output :
Enter a Number : 5
Prime
Enter a Number : 4
Not prime
A while loop repeatedly executes a block of code as long as a specified condition evaluates to true.
Its syntax consists of the while keyword followed by a condition enclosed in parentheses, and the code block to be executed enclosed in curly braces.
Anatomy of a While Loop :
while (condition) {
// code block to be executed
}
Condition: Specifies when the loop should continue executing.
Example :
- i = 0; initializes the loop variable i to 0.
- i < 5; is the condition. The loop will continue as long as i is less than 5.
- i++; increments the value of i by 1 after each iteration to prevent an infinite loop.
Output :
Enter a number : 123
Sum of digit=6
Output :
Enter a number : 121
Palindrome
Enter a number : 124
not Palindrome
A do-while loop is similar to a while loop, but with one key difference: it always executes the code block at least once before checking the condition.
Its syntax starts with the do keyword, followed by the code block enclosed in curly braces, and ends with the while keyword followed by the condition in parentheses.
Anatomy of a do-while loop :
do {
// code block to be executed
} while (condition);
Condition : Checked after each iteration.
Example :
- i = 0; initializes the loop variable i to 0.
- The code block within the do and while loop will be executed at least once.
- i < 5; is the condition. After executing the code block, the loop will continue as long as i is less than 5.
The for...in loop is used to iterate over the enumerable properties of an object.
Anatomy of for...in loop :
for(variable in object){
// code to be executed
}
Example :
Output :
name => Rokeiyaclass => 12
The for...of loop in JavaScript is designed for iterating over iterable objects, such as arrays, strings, maps, sets, and more. It provides a simpler and more readable way to loop through elements, especially when compared to a traditional for loop or forEach method.
Output :
Apple
Banana
Cherry
Readability: Python's loop constructs are designed for
readability, making
code easy to understand and maintain. The explicit nature of loops enhances
code clarity, especially when dealing with repetitive tasks.
Flexibility: Loops provide flexibility in handling
different data structures
and scenarios. Whether you're iterating over a collection of integers,
strings, or complex objects, Python's loop constructs adapt effortlessly.
Efficiency: Iteration with loops is often more efficient
than manual
iteration, as Python's built-in iteration mechanisms are optimized for
performance. This efficiency is particularly evident when working with large
datasets or complex data structures.
2. Write a program to accept a number and check number is Perfect or not. A number is called perfect if it is equal to the sum of its factor other than the number itself. Example: 6= 1 + 2 + 3
Each type of loop has its use cases and benefits, and choosing the right one depends on the specific requirements of your task. Loops are fundamental to JavaScript programming and are widely used in various applications, from web development to backend server scripting.