Loops in Java
There may be a situation when we need to execute a block of code several number of times. In general, statements are executed sequentially; The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
While and Do .. While
The while statement executes a block of code until a specific condition is met. In the while loop, a condition is check and if true, the code block is executed. If the condition in a while loop fails from the beginning, the code block is never executed. To ensure that the code block gets executed at least once, we can use a do .. while loop which will execute the code block first and then check the condition. As with while loops in C, Python and other languages, if the condition never evaluates to false, the loop will go on infinitely.
int i = 0; while (i < 5) { System.out.println( i); i++ ; }
Will print out 0 1 2 3 4 on each line.
int i = 0; while (i < 5) { System.out.println( i); }
Will print out 1 infinitely
For Loop
The for loop repeats a block of code until a certain condition is met. It is typically used to execute a block of code for certain number of times. This loop is preferred when we are sure about the number of iterations of the script. The parameters of for loop three meanings. Th first is initialization, used to initialize the counter variables and evaluated once unconditionally before the first execution of code block. Next is the condition which is the beginning of each iteration. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends. Lastly, he increment updates the loop counter with a new value. It is evaluate at the end of each iteration. Unlike PHP or Python, Java can have an infinite loop in a for statement since the initialization, Boolean-expression, and step used in for statement are optional.
for (int i = 0; i < 5; i++) { System.out.println( i); }
Will output 0 1 2 3 4 on separate lines
An example of an infinite loop
for ( ; ; ) { System.out.println("Hello"); } /pre>
Will output Hello infinitely.
Enhanced For Loop
Unlike PHP, Java doesn't have a special loop for iterating through arrays. In PHP, there is a foreach loop but in Java, we continue to use th for loop. Since this loop is simplified in comparison to the standard for loop, we need to declare only two things when initializing a loop; the handle for an element we’re currently iterating over and the source array/collection we’re iterating. The for loop declaration is read as "For each element in items, assign the element to the item variable and run the body of the loop."
int[] myArray = { 0,1,2,3,4 }; for (int num : myArray) { System.out.println num); }
Will output 0 1 2 3 4 on separate lines.
Loops are very important in any programming whether it is C, Java, Python or any other language. Loops allows us to reuse blocks of code and gives us the ability to control the number of times blocks of code are executed. Like it if else statements in programming, it allows programmers to control what our program does given certain criteria.