Questions tagged [for-loop]

A for loop is a control structure used by many programming languages to iterate over a range. It is a way of repeating statements a number of times until the loop ends. Depending on the language this may be over a range of integers, iterators, etc.

A for loop is an entry control loop. Entry control means that before the execution enters the loop, the loop's condition is verified. If the condition is false the loop body will not execute.

The basic syntax is as follows:

for ( initialization of iteration variable ; condition ; increment/decrement )
{
     ...
}

For loop on Wikipedia

See also: , , , and .

71492 questions
3965
votes
46 answers

Loop through an array in JavaScript

In Java, you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can I do the same in JavaScript?
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87
3553
votes
48 answers

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; How do I loop through all of p's elements (p1, p2, p3...) and get their keys and values?
Tanmoy
  • 44,392
  • 16
  • 45
  • 55
2977
votes
3 answers

Why is printing "B" dramatically slower than printing "#"?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000;…
Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63
2118
votes
20 answers

How do I iterate over a range of numbers defined by variables in Bash?

How do I iterate over a range of numbers in Bash when the range is given by a variable? I know I can do this (called "sequence expression" in the Bash documentation): for i in {1..5}; do echo $i; done Which gives: 1 2 3 4 5 Yet, how can I…
eschercycle
  • 22,067
  • 4
  • 21
  • 13
2062
votes
28 answers

Why is using "for...in" for array iteration a bad idea?

I've been told not to use for...in with arrays in JavaScript. Why not?
lYriCAlsSH
  • 57,436
  • 10
  • 26
  • 20
1239
votes
8 answers

How do I iterate through two lists in parallel?

I have two iterables, and I want to go over them in pairs: foo = [1, 2, 3] bar = [4, 5, 6] for (f, b) in iterate_together(foo, bar): print("f:", f, " | b:", b) That should result in: f: 1 | b: 4 f: 2 | b: 5 f: 3 | b: 6 One way to do it…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
1163
votes
20 answers

Difference between ( for... in ) and ( for... of ) statements?

I know what is a for... in loop (it iterates over the keys), but I have heard about for... of for the first time (it iterates over values). I am confused about for... of loop. var arr = [3, 5, 7]; arr.foo = "hello"; for (var i in arr) { …
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
1146
votes
20 answers

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
934
votes
9 answers

A for-loop to iterate over an enum in Java

I have an enum in Java for the cardinal and intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of…
Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
800
votes
6 answers

How to loop over files in directory and change path and add suffix to filename

I need to write a script that starts my program with different arguments. I start my program with: ./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]. Here is the pseudocode for what I want to do: for each filename in /Data do for int i = 0, i =…
Dobrobobr
  • 8,216
  • 4
  • 17
  • 18
727
votes
25 answers

Why does python use 'else' after for and while loops?

I understand how this construct works: for i in range(10): print(i) if i == 9: print("Too big - I'm giving up!") break else: print("Completed successfully") But I don't understand why else is used as the keyword here,…
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
640
votes
12 answers

Get loop counter/index using for…of syntax in JavaScript

Caution: question still applies to for…of loops.> Don't use for…in to iterate over an Array, use it to iterate over the properties of an object. That said, this I understand that the basic for…in syntax in JavaScript looks like this: for (var…
hobbes3
  • 28,078
  • 24
  • 87
  • 116
548
votes
8 answers

Java 8 Iterable.forEach() vs foreach loop

Which of the following is better practice in Java 8? Java 8: joins.forEach(join -> mIrc.join(mSession, join)); Java 7: for (String join : joins) { mIrc.join(mSession, join); } I have lots of for loops that could be "simplified" with lambdas,…
nebkat
  • 8,445
  • 9
  • 41
  • 60
454
votes
8 answers

C++ Loop through Map

I want to iterate through each element in the map without knowing any of its string-int values or keys. What I have so far: void output(map table) { map::iterator it; for (it = table.begin(); it…
NoName
  • 9,824
  • 5
  • 32
  • 52
440
votes
17 answers

Iterate all files in a directory using a 'for' loop

How can I iterate over each file in a directory using a for loop? And how could I tell if a certain entry is a directory or if it's just a file?
Vhaerun
  • 12,806
  • 16
  • 39
  • 38
1
2 3
99 100