FbsBffEfCEefrAX-~~~| G7VZC 847VBCC 9VBCC 886647V G7VC G774VC 5G74VCC 56G75VC G754VB 4G79V~~C 9474749846F55W~ 64646F55ZX

For.` Most runtime in programs is spent in loops. The for-loop iterates over numbers. It is commonly used. It is ideal for processing known ranges.`For each.` In Java no foreach keyword is used. Instead we use the for-keyword to iterate over each element in a collection. We do not need an index to do this.`Example.` This program uses a simple for-loop. It declares the iteration variable "i" inside the condition of the loop. It starts at 0, and continues until it is equal to 5. `First part: `The first part of the for-loop condition is where the loop iteration variable is declared and initialized.`Second part: `This is the terminating condition. In this loop, we terminate when the variable reaches 5.`Third part: `The third and final part of the for-loop is the iteration statement. This is applied after each pass through the loop.`Recommendation.` A for-loop is best when the starting and ending numbers are known. If the end index is unknown, consider a while-loop. Use a break when the end is reached.`For each.` This is a simple syntax form. If we loop over a collection, we use a colon, not an index variable. This enumerates each element in the collection (array, ArrayList). `Arrays `array-java`Info: `This is called a foreach-loop statement. The Java language does not support a "foreach" keyword. Please use the "for" keyword.`Break.` A for-loop can be stopped at any time, based on any condition. We use the "break" statement. It takes no arguments and terminates the nearest enclosing loop. `Break `break-java`More complex logic is needed to fully break out of a nested loop. A flag boolean, or the use of methods, is needed.`However: `This for-loop example scans each element in the values array. It stops (breaks) when a negative one element is found.`Continue.` This keyword stops the current loop iteration and moves to the next one. Further statements are not executed. In a loop with an iteration condition, the next iteration begins. `Continue `continue-java`Caution: `A continue statement can sometimes lead to an infinite loop. Be sure the iteration variable is incremented.`We use "continue" to skip over Strings in an array that start with the letter "b." We filter Strings.`Decrement loop.` Often we use decrementing for-loops to iterate backwards through a series of numbers. The >= operator means we include zero in the loop body. `Nested for-loops.` All kinds of loops can be nested. When we use a break or continue statement with a nested loop, only the innermost loop is affected. `However: `A return statement will exit all loops in the current method. Sometimes flag variables of boolean type are needed.`Reuse iteration variable.` Sometimes an iteration variable needs to be reused outside of a for-loop. We can use any local variable in a new for-loop. `The local variable, like x in this program, remains reachable after the loop itself terminates.`Method, for-each.` A method can be called in the for-loop. This method is evaluated once and then the results of it are accessed in the loop iteration variable. `This program shows that the method getElements is only called once. It is not called three times.`So: `When calling a variable or method in a for-each loop, we can see that the result is cached in a local and not evaluated more than once.`Overflow behavior.` Loops in Java can wrap around. This can happen in loops where we increment or decrement. The int type overflows and becomes negative, then reaches the target. `Caution: `This mistake can cause a serious performance problem. Be sure to validate your looping logic. A for-each loop also helps.`String loop.` This program uses a for-loop over a String. We start at index 0 and process until the length() is reached. With charAt we get each character at an index. `Strings `string-java`Performance, loop jamming.` In loop jamming many loops are combined into one. Consider this program—three arrays, all of the same length, must be looped over. `Version 1: `The first part loops over the three arrays separately and sums their elements.`Version 2: `The second part loops over the arrays in a single, jammed loop. This version is faster.`Result: `The single loop is nearly twice as fast as the three loops put together. And it has the same result on every iteration.`Performance, loop unrolling.` Sometimes loops are unnecessary. In loop unrolling, we change a loop to a list of statements. We can loop over groups of statements at once. `In this example we simply remove the entire loop and place the statements that need to be executed.`Unwinding: `Loop unrolling is sometimes called loop unwinding. It can improve, or reduce, performance—we must benchmark.`ArrayList.` A for-loop can be used on an ArrayList. We can iterate through the indexes, bounded by size(), or directly through the elements with simpler syntax. `ArrayList, For `arraylist-java`Loops are key to optimizations.` Often we eliminate steps from the loop body by changing the loop declaration. Loops affect control flow, causing it to repeat.`With loops,` programs gain complexity and power. Statements no longer occur in the order of program's text lines. Control flow branches, repeats, changes as time passes.

XYZ Y=YDY; { XY=YAYfmain(YM[] args) {Z XXY{Y]through 0, 1, 2, 3YR4. XXZY|Z (YsiYz0; i < 5; i++) {Y'YO.out.Y_ln(i); XX} X} } Z 0 1 2 3 4Z Y=YDY; { XY=YAYfmain(YM[] args) { XXYM[] YdsYzYwYM[3]; XXYds[0]YzZ"Dot"Z; XXYds[1]YzZ"Net"Z; XXYds[2]YzZ"Perls"ZYSXXZY|Z (YM Yd : Yds) {Y'YO.out.Y_ln(Yd); XX} X} } Z Dot Net PerlsZ Y=YDY; { XY=YAYfmain(YM[] args) { XXY}[] YdsYz{ 1, 2, 3, -1 };Z XXY2Y]over YT indexes, but break on negative one. XXZYo(YsiYz0; i < Yds.lYZ; i++) {Y'YrYds[i]Yi-1) {Y'XZbreakZ;Y'}Y'YO.out.Y_ln(Yds[i]); XX} X} } Z 1 2 3Z Y=YDY; { XY=YAYfmain(YM[] args) { XXYM[] YdsYz{ Z"cat"Z, Z"bear"Z, Z"dog"Z, Z"bird"Z };Z XXY{Y]over all YMs. XXZYo(YM Yd : Yds) {Z Y'Y{Skip YMs starting with letter b.Y'ZYrYd.startsWith(Z"b"Z)) {Y'XZcontinueZ;Y'} Y'YO.out.Y_ln(Yd); XX} X} } Z cat dogZ Y=YDY; { XY=YAYfmain(YM[] args) {Z XXY{Y]from fiveYlzero, decrementing. XXZY|Z (YsiYz5; i >= 0; i--) {Y'YO.out.Y_ln(i); XX} X} } Z 5 4 3 2 1 0Z Y=YDY; { XY=YAYfmain(YM[] args) {Z XXY{Use nested Y|-loops. XXZY|Z (YsiYz0; i < 3; i++) {Y'ZY|Z (YsyYz0; y < 3; y++) {Y'XYO.out.Y_ln(iYyZ","ZYyy);Y'} XX} X} } Z 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2Z Y=YDY; { XY=YAYfmain(YM[] args) { XXZY}Z xYz0;Z XXY{Parts of the Y|-loop can be omitted. XXY2Here we use no variable declarationYkthe Y|-statement. XXZY|Z (; x < 3; x++) {Y'YO.out.Y_ln(x); XX} XXYO.out.Y_ln(Z"x is still reachable!"Z); XXYO.out.Y_ln(x); X} } Z 0 1 2 x is still reachable! 3Z Y=YDY; { XYAYscountYSXYAY}[] ZgetElementsZ() {Z XXY{Set YT Y6s based on a YAfield. XXZY}[] YTYzYwY}[3]; XXYT[0]Yzcount++; XXYT[1]Yzcount++; XXYT[2]Yzcount++; XXYJ YT; X} XY=YAYfmain(YM[] args) {Z XXY{The mY[ is called onceYRnot many timesYkthe Y|-loop. XXZY|Z (YsYd : ZgetElementsZ()) {Y'YO.out.Y_ln(Yd); XX} X} } Z 0 1 2Z Y=YDY; { XY=YAYfmain(YM[] args) { XXlong iterationsYz0;Z XXY{YW iterations from 100Yl200 decrementing. XXZY|Z (ZY}Z uYz100; u <= 200; u--) {Y'iterations++; XX} XXYO.out.Y_ln(Z"Iterations from 100Yl200: "ZYyiterations); X} } Z Iterations from 100Yl200: 2147483749Z Y=YDY; { XY=YAYfmain(YM[] args) { XXYM YdYzZ"art"Z;Z XXY{Y]from 0YllYZ() of the YL. XXZY|Z (YsiYz0; i < Yd.lYZ(); i++) {ZY'Y{Yqletters with charAt mY[.Y'Zchar letterYzYd.ZcharAtZ(i);Y'YO.out.Y_ln(letter); XX} X} } Z a r tZ Y=YDY; { XY=YAYfmain(YM[] args) { XXY}[] YT1Yz{ 10, 20, 30 }; XXY}[] YT2Yz{ 20, 10, 30 }; XXY}[] YT3Yz{ 40, 40, 10 }YSXXlong t1YzYO.currentTimeMillis();Z XXY{Version 1: loop over each YT separately. XXZYo(YsiYz0; i < 10000000; i++) { Y'YssumYz0;Y'ZY|Z (YsxYz0; x < YT1.lYZ; x++) {Y'Xsum Z+=Z YT1[x];Y'}Y'ZY|Z (YsxYz0; x < YT2.lYZ; x++) {Y'Xsum Z+=Z YT2[x];Y'}Y'ZY|Z (YsxYz0; x < YT3.lYZ; x++) {Y'Xsum Z+=Z YT3[x];Y'}Y'Yrsum != 210) {Y'XYO.out.Y_ln(false);Y'} XX} XXlong t2YzYO.currentTimeMillis();Z XXY{Version 2: jam loops together. XXZYo(YsiYz0; i < 10000000; i++) {Y'YssumYz0;Y'ZY|Z (YsxYz0; x < YT1.lYZ; x++) {Y'Xsum Z+=Z YT1[x];Y'Xsum Z+=Z YT2[x];Y'Xsum Z+=Z YT3[x];Y'}Y'Yrsum != 210) {Y'XYO.out.Y_ln(false);Y'} XX} XXlong t3YzYO.currentTimeMillis();Z XXY2Times. XXZYO.out.Y_ln(t2 - t1); XXYO.out.Y_ln(t3 - t2); X} } ZResultsZ Z109 msZ, 3 Y|-loops Z 48 msZ, 1 Y|-loop (jammed)Z Y=YDY; { XY=YAYfmain(YM[] args) { XXY}[] YT1YzYwY}[5]YSXXlong t1YzYO.currentTimeMillis();Z XXY{Version 1: assign Y6sYka loop. XXZYo(YsiYz0; i < 10000000; i++) {Y'Yo(YsxYz0; x < YT1.lYZ; x++) {Y'XYT1[x] Z=Z x;Y'} XX} XXlong t2YzYO.currentTimeMillis();Z XXY{Version 2: unroll the loopYRuse a Yt of statements. XXZYo(YsiYz0; i < 10000000; i++) {Y'YT1[0] Z=Z 0;Y'YT1[1]Yz1;Y'YT1[2]Yz2;Y'YT1[3]Yz3;Y'YT1[4]Yz4; XX} XXlong t3YzYO.currentTimeMillis();Z XXY2Times. XXZYO.out.Y_ln(t2 - t1); XXYO.out.Y_ln(t3 - t2); X} } ZResultsZ Z56 msZ, Y|-loop Z17 msZ, unrolled statementsZ

Xfor-loopefor, alternate syntaxUbreakXcontinue6decrements in for-loop`nested for-loops9reuses iteration variableemethod, for-each loop7has loop overflow error`for-loop, string4applies loop jamming-unrolls loops