22 Jun



Syntax

JS

  • loops can be implemented using the for, while, and do-while statements.
// for loopfor (initialization; condition; iteration) {
    // code to execute in each iteration
}// while loopwhile (condition) {
    // code to execute while the condition is true
}// do-while loopdo {
    // code to execute at least once, then continues while the condition is true
} while (condition);


Python

  • loops are typically implemented using the for and while statements.
# for loop
for variable in sequence:
    # code to execute in each iteration

# while loop
while condition:
    # code to execute while the condition is true
  • primarily used to iterate over collections, such as lists or strings.



Javascript
Python
Syntax
  • loops can be implemented using the for, while, and do-while statements.

// for loop
for (initialization; condition; iteration) {
    // code to execute in each iteration
}

// while loop
while (condition) {
    // code to execute while the condition is true
}

// do-while loop
do {
    // code to execute at least once, then continues while the condition is true
} while (condition);

  • loops are typically implemented using the for and while statements.


# for loop
for variable in sequence:
    # code to execute in each iteration

# while loop
while condition:
    # code to execute while the condition is true
  • primarily used to iterate over collections, such as lists or strings.
Range-based For Loop
  • JavaScript does not have a built-in range function, 
  • but you can achieve similar functionality using other techniques, such as creating a custom range function or using the Array.from() method.
  • Python has a built-in range() function that generates a sequence of numbers that can be used in a for loop.
for i in range(start, end):
    # code to execute for each iteration
Loop Control Statements
  • JavaScript provides break and continue statements to control loop execution.


// break statement
for (let i = 0; i < 10; i++) {
    if (i === 5) {
        break; // exit the loop when i equals 5
    }
}

// continue statement
for (let i = 0; i < 10; i++) {
    if (i === 5) {
        continue; // skip the current iteration when i equals 5
    }
}


  • Python also provides break and continue statements for loop control.


# break statement
for i in range(10):
    if i == 5:
        break  # exit the loop when i equals 5

# continue statement
for i in range(10):
    if i == 5:
        continue  # skip the current iteration when i equals 5
  • Additionally, Python provides an else clause that can be used with loops, which executes when the loop completes normally (i.e., not interrupted by a break statement).
  • Additionally, Python supports the pass statement, which acts as a placeholder and does nothing.
For...in Loop
  •  thefor..in loop is commonly used to iterate over arrays or to execute a block of code for a specific number of times.


for (let key in object) {
    if (object.hasOwnProperty(key)) {
        // code to be executed
    }
}
  • Javascript also supports for...of loop (array elements):


for (let element of array) {
    // code to be executed
}
  • Python also has a for loop, but it is primarily used to iterate over collections, such as lists or strings.


for item in my_list:
    # code to execute for each item
Iteration over Arrays and Collections
  • you can iterate over arrays using the for loop or the forEach() method.

// for loop
for (let i = 0; i < array.length; i++) {
    // access array elements using array[i]
}

// forEach()
array.forEach(function(element) {
    // code to execute for each element
});

  • you can iterate over arrays and collections using the for loop.


# for loop
for element in array:
    # access array elements using element

Iteration over Objects and Dictionaries
  • you can iterate over object properties using the for...in loop or the Object.keys() method.


// for...in loop
for (let key in object) {
    // access object properties using object[key]
}

// Object.keys()
Object.keys(object).forEach(function(key) {
    // access object properties using object[key]
});

  • you can iterate over dictionaries using the for loop.


# for loop
for key, value in dictionary.items():
    # access dictionary keys using key
    # access dictionary values using value

Nesting Loops
  • JavaScript allows loops to be nested inside one another.
  •  Python also supports nested loops.
Infinite Loops
  • In JavaScript, you can create an infinite loop using constructs like while (true) or for (;;) and break out of the loop using a conditional if statement with break.
In Python, you can create an infinite loop using while True and break out of the loop using a conditional if statement with break.



Comments
* The email will not be published on the website.