Categories
python

for loop in python

In this tutorial, we will learn about for loop in python. We can iterate an array using for loop.

Syntax

for seq_val in sequence:
  body

Iterate array

cars = ["Honda", "Toyota", "Hyundai"]
for car in cars:
    print(car)

Output

Honda
Toyota
Hyundai

for loop using range

cars = ["Honda", "Toyota", "Hyundai"]
for i in range(len(cars)):
    print(cars[i])
Honda
Toyota
Hyundai

for loop with custom start value

cars = ["Honda", "Toyota", "Hyundai"]
for i in range(2, len(cars)):
    print(cars[i])

Output

Hyundai

Related Articles

Categories
java

for loop in java

for loop used to iterate an array or used to repeat a set of code for predefined times

Syntax

for(;;){
    // your logic
}

Example

   for (int i = 0; i < 10; i++) {
             System.out.println("Loop count " + i);
         }

Ouput

Loop count 0
Loop count 1
Loop count 2
Loop count 3
Loop count 4
Loop count 5
Loop count 6
Loop count 7
Loop count 8
Loop count 9