For many programmers the for loop is a necessity for most programs out there. It is one of the first statements any programmer learns and usually never forgets. This article is to show how this code works, and to get a general understating of it.
In short the for loop takes a value, sometimes an integer, and then iterates it over a range of values until a certain condition is met, usually exceeding a numerical value. It is important to note my code is in the Java programming language. Please see the example below:
class For {
public static void main(String[] args){
for(int i=1; i<6; i++){
System.out.println(“Current Value: “ + i);
}
}
}
As you can see the integer i has the value 1 to start off. The next statement compares i to the value 6, and if it is less than 6, it continues through the for loop. At the end if i is less than 6, it adds 1 to i which is what i++ means. And finally it prints out what is in “ “, Current Value: i. After it prints i, it re-enters the for loop, until i>6, in which case it exits the for loop and proceeds to the next line in your code. So if we were to run this For Loop it would output the following text:
Current Value: 1
Current Value: 2
Current Value: 3
Current Value: 4
Current Value: 5
Now you can see and understand how a basic function of a programming language works. There are many statements and loops that can be used and I will go over more of these in future articles. Stay tuned!