for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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"?

23rd Apr 2018, 8:50 PM
arbaaz
4 Answers
+ 3
When you loop associative arrays or objects. var arr = {"a": 1, "b": 2}; for(key in arr) { console.log(arr[key]); }
23rd Apr 2018, 9:13 PM
Toni Isotalo
Toni Isotalo - avatar
+ 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])
23rd Apr 2018, 10:04 PM
Emma
+ 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.
24th Apr 2018, 8:18 PM
Uni
Uni - avatar
+ 2
Thank you all for your answers.
24th Apr 2018, 9:46 PM
arbaaz