Why For?


#1

Hey guys, I'm in AP Comp Sci right now and this lesson has duly solidified my base, but why do we use the For loop in arrays rather than say a while loop.
Follow up: why dont we learn about while loops on this course.

Thanks in Advance.


#2

While we cannot speak for the author's intentions, it seems the focus is on for so that it can be reviewed and then extended to introduce the for each loop, which does not use a counter. Within the limited scope of the course, while might be a distraction until the learner has mastered for and for each. It's a thought, anyway.


#3

Although you can use both, the For loop is the one most often used to traverse through arrays. The second most common is the For Each loop. That being said, do which way makes most since in the code.

  • Java Developer.

#4

@stucky3 @mtf
Great explanation, thank you both.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.