# Chapter 6 Looping

Student Learning Outcomes
1.
2. Create while loops
3. Use shortcut arithmetic operators
4. Create for loops
5. Create do...while loops
6. Nesting  loops
7. Improve loop performance

Important Points:
1. Learn about the loop structure. Allow repeated execution of a block of statements. While loop conditions remains true it will execute an operation.
a. While loops - the loop control is the first statement
b.
For loops - concise format in which to execute loops.
c. Do...while loop control is the last statement in the loop.

2. While loops -executes statements as long as value is true. Tests the condition at the beginning of the loop.
a. A loop that executes a specific number of times is a definite loop. It is counter controlled.
b. A loop that does not specify number of executions is called indefinite loop. It is event controlled.
c. To write a definite loop you initialize a loop control variable whose value determines whether loop execution continues.

int val   //this is our loop control variable
final int LIMIT=11;
val=1;   //this is our loop control variable initialized to 1
while (val<LIMIT)
{
System.out.println(val);
val=val +  1;   //this is a counter which reassigns the value of val back to val which will increment val by one for each iteration.
}

d. A loop that never ends is called an infinite loop. To prevent an infinite loop, a named loop control variable is initialized to a starting value. It is tested in the while statement. The loop control variable must change to break out of the loop. ~counter
e. An Empty body is a loop without statements. This causes an infinite loop.
f. Incrementing adds to the loop control variable, decrementing subtracts from the loop control variable.
g. Indefinite loops are often used to validate input data. Validating data is process that data falls within a certain range.
h. Usually a loop that tells the user they have put in the wrong data is the best.

3. Using shortcut Arithmetic Operators

a. Counters are used to keep track of the number of occurrences of some event.
b. Accumulators are used to add totals or to increase the original amount by an additional amount.

balance += INT_RATE is the same as balance = balance + INT_RATE
Count +=1 is the same as count=count+1

c. += means add and assign
d. -= subtract and assign
e. *= multiply and assign operator
f. /= divided and assign
g. %= remainder and assign operator
h. The prefix ++ and postfix ++ works with variables but not with constants. It increases the value by 1.
i. Example:

int value;
value=24;
++value;      // value is 25
value++       // value is 25
value += 1    // value is 25

j. Decrement operators

b=4    c=b--   // 4 is assigned to c, and after assignment b is decreased to 3.
c=--b    // b is decreased to 3 and then 3 is assigned to c.

4. A for loop is used when a definite number of loop iterations is required.
a. Initializing the loop control variable
b. Testing the loop control variable
c. Updating the loop control variable

for(int val=1; val<11; ++val //int val=1 initializing control variable, val<11 testing the control variable, ++val updating the control variable.
System.out.println(val);

d. Initialization of more than one variable by placing commas between separate statements.

for(g=0, h=1; g<6; ++g)

e. Using AND or OR operators for testing and condition:

for(g=0, g<3&&h>1; ++g)

f. Decrementation or performance of some other test:

for(g=5; g>=1; --g)

g. Altering more than one value:

for(g=0; g<10; ++g,++h, sum+=g)

5. Using a do...while loop
- the do while loop checks the condition of the loop at the bottom of the loop. It will always execute once.
a. Do…While loop format:  do {statements}while(test=?);
the loop begins with do, followed by the statements in curly brackets, followed by the condition.
b. Example:

do
total+=numbervalue;      //with a single statement curly braces are not needed but recommended.
while(total<200);

c. When a loop must execute at least once, use a do while.

6. Nested Loops
a. For, while, do loops can be nested.
b. The inner loop must be contained within the outer loop. Loops can never overlap.
c. The variable in the outer loop changes more infrequently.

7. Improve loop performance.
a. Avoid unnecessary operations or statements.
b. Consider the order of evaluation of short-circuit operators.
c. Comparing to 0 (zero) is faster because of the way the compiler works flagging once through the loop.
d. A do-nothing loop is one that performs no actions other than looping.
e. Loop fusion is the technique of combining two loops into one. When the two loop conditions are the same, the methods can be combined as long as the fist method is finished before the second method is executed.