Jump to content
Guest Apogee

What is "For"?

Recommended Posts

Guest Apogee

I saw "For" recently.

I know somewhat of what it means, but i don't understand why you would need to use it. Can somebody elaborate?

Link to comment
Share on other sites

There are 2 types of for loops; The 'standard' for loop which loops through a code block while a condition is true, executing some logic after every loop as well (usually incrementation).

 

example:

for(int i = 0; i < 10; i++) {
//Code block
}

this would execute the code block until the variable i is no longer less than 10; And will increment i by one after each execution of the code block. This type of for loop is commonly used for iterating through arrays of primitive types, grabbing elements by index.

 

another variation of the for loop (often referred to as a for-each loop) is used to pull elements out of a collection. This type of for-loop will only work with collections that implement the Iterable interface, and is essentially a limited iteration by the collections iterator.

for(Object element : collection) {
//Do something with the elementa
}
Edited by FrostBug
  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...