Is there a way to count iterations using a while loop?
<p>Is there a way in which the program can count the number of iterations in which it took for the variable "<code>counter</code>" to reach its limit? I'm writing a basic program that demonstrates the use of a while loop and have been asked to display the number of iterations which have been printed out. </p>

<p>P.S. Sorry if there are any errors in the indentation/formatting of the code, I'm new to Java/programming. Thanks in advance.</p>

<pre><code>public class Main {
    public static void main(String[] args) {
        for (int counter=2; counter&lt;=40; counter+=2) {
            System.out.println(counter);
        }
        System.out.println("For loop complete.");

        int counter = 1;
        while (counter &lt;= 500) {
            System.out.println(counter);
            counter++;
        }
    }
}
</code></pre>