Different between for loop and foreach loop. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Different between for loop and foreach loop.

how do work ? define that.

20th Apr 2018, 8:36 AM
Ayush Bious Raj
Ayush Bious Raj - avatar
3 Answers
+ 3
C++ for (init; check; inc) ... equals init; while (check) { ... inc; } A foreach loop is more specialised than the for loop and it goes through each element of a data structure.
20th Apr 2018, 8:51 AM
Timon Paßlick
+ 2
for loops just increment ot decrement a counter by a set amount each iteration. a foreach loop takes a data array or object and on each loop gives you access to the key value pair for each element in the structure. you can do that with a for loop but a foreach makes it easier. I for (var i = 0; I < array.length; i++) { //array[i].key returns value for that key in an array of objects }
20th Apr 2018, 9:47 AM
Adam
Adam - avatar
+ 1
In Python, a for loop is kind of like a foreach loop in other languages: it iterates over an object
20th Apr 2018, 10:22 AM
Amaras A
Amaras A - avatar