+ 4
for loop
When using the "for" loop, does the word "in" come along with it, if so why? Will the loop not work without the word "in"?
4 Answers
+ 3
When you loop associative arrays or objects.
var arr = {"a": 1, "b": 2};
for(key in arr) {
console.log(arr[key]);
}
+ 2
You must have the 'in'
http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
colors = ["red", "green", "blue", "purple"]
for i in range(len(colors)):
print(colors[i])
+ 2
I believe that it depends on the programming language one is using.For example here is the same code written in two languages (ruby and java):
-ruby:
for x in 0..10
puts "Hello"
end
-java:
for(int x=0;x<10;x++){
System.out.println("Hello");
}
So it really depends on the language.
+ 2
Thank you all for your answers.