
Either we know exactly how many times the loop will be executed or we have a better idea than "until n becomes m". In Java, there are several ways to control the flow of the code:įor loops are typically used when the number of iterations is "fixed" in some way. Loops were a concept used long before computer programming was even a thing, but the first person to use a software loop was Ada Lovelace, commonly known by her maiden name - Byron, while calculating Bernoulli numbers, back in the 19th century. low-level programming languages usually achieve that affect with a lot of go-to commands. The For-Each loop is usually used as a substitute for the standard for-statement when a loop counter is not really needed, and every element in the collection. Every programming language supports some form of flow control, if not explicitly via ifs and fors or similar statements - then it implicitly gives us the tools to create such constructs, i.e. I can easily print these 20 Fibonacci sequences out in the for loop, but I just want to do the calculations to fill the Fib array up and do the printing out in an enhanced loop. That's what "flow control" means - guiding the execution of our program, instead of letting it execute line-by-line regardless of any internal or external factors. Java - how do I print my array using enhanced loops, So as my program runs, it does some calculations for the Fibonacci sequence.


There aren't many things we could do with code that can only execute line-by-line. With the release of version 1.5, Java introduced a new type of for loop known as enhanced or numerical for loop. If you still need the index, fall back to a regular for loop.

Conditional statements and loops are a very important tool in programming. The enhanced for loop was created as a shortcut for when you don't need the index of the element.
