Display all even elements. Best Practice. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 2

Display all even elements. Best Practice.

If I would like to print every second element in an array what is the best practice? An if statement in a foreach or a for loop with i+=2? Does the compiler optimize if i use foreach? Is there a better way maybe?

6th Feb 2018, 2:11 AM
Roland Kopka (KOB)
Roland Kopka (KOB) - avatar
1 Antwort
+ 6
You may use foreach for this purpose. Check out the comparison among those two, https://www.dotnetperls.com/for-foreach
6th Feb 2018, 6:06 AM
Dev
Dev - avatar