Iterating in Java: MATLAB and Python to Java Tutorial

The general for loop structure for Java adheres to the following form: for (INITIAL-STATEMENT; CONTINUING-CONDITION; ITERATIVE-STATEMENT). The INITIAL-STATEMENT section is evaluated once before the loop begins and is usually where a counter variable is declared and initialized. The CONTINUING-CONDITION section typically contains at least one boolean statement usually regarding the counter variable and is evaluated before each iteration begins. The ITERATIVE-STATEMENT section is evaluated at the end of each iteration and often contains a statement iterating the counter variable.

Though the for loop is merely a modified form of the while loop, by convention and ease they are used to achieve different purposes. A for loop is used when the exact number of iterations in known, while a while loop is used when the number of iterations is dependent upon satisfying some conditional. For instance, a program simulating a person eating until he is full would probably use a while loop, because the person must continue eating until the condition of being full is satisfied. However, if another person decided that she would only eat ten chicken nuggets, the program would use a for loop.

Get pdf download Iterating in Java: MATLAB and Python to Java Tutorial

Related Tutorial

Tags: , , , , , , , ,

Comments

Leave a Reply