What is a for loop?
A for loop is a programming statement that repeats one or more statements. It typically uses a variable to count the iterations of the loop.
Why is a for loop useful?
A for loop is useful because it implements repetition in programs. It prevents redundant code. A for loop makes it easy to access arrays.
How do you use a for Loop?
Here is a sample for loop in the Java programming language:
for (int i=0; i < 10; i++)
First, the for loop initializes the integer variable i to 0. Then it checks the terminating condition. Since i is less than 10, the loop will execute the block of statements. In this case, there is only one statement in the loop block. The loop prints out the current value of i, which is 0. Finally, the loop increments the i variable to 1. The loop repeats, checks if i is less than 10 and so on. Eventually, the variable i will become 10, and the loop will exit. This example prints out the numbers 0 through 9.
For more articles, please subscribe, add or follow me here.