click below
click below
Normal Size Small Size show me how
Chapter 6
Looping
Term | Definition |
---|---|
Loop | A structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated. If it is true, a block of statements called the loop body executes and the Boolean expression is evaluated agian |
Loop Body | Can be a single statement or a block of statements between the curly braces of a loop |
Iteration | One execution of any loop |
while Loop | Can be used to execute a body of statements as long as the Boolean expression that controls entry into the loop continues to be true, Consists of the keyword while followed by Boolean expression in parentheses, followed by body of loop |
Definite Loop(Counted Loop) | A loop that executes a specific number of times |
Indefinite Loop | A loop in which the number of times the loop executes might not be determined until the programming is running. Called indefinite loop because you don't know how many times it will eventually loop |
Loop Control Variable | A variable whose value determines whether loop execution continues. To write a definite loop, you initialize a loop control variable |
Infinite Loop | A loop that never ends |
1. Three Separate Actions to Prevent a while Loop from Executing Infinitely | 1. A loop control variable is initialized to a starting value... 2. The loop control variable is tested in the while statement... |
2. Three Separate Actions to Prevent a while Loop from Executing Infinitely | 3. The loop control variable is altered within the body of the loop. The variable must be altered so that the test expression can eventually evaluate to false and the loop can end |
Empty Body | A body with no statements in it |
Counter-Controlled Loop | A definite loop is a counter-controlled loop because the loop control variable is changed by counting. It is very common to alter the value of a loop control variable by adding 1 to it, or incrementing the variable |
Event-Controlled Loop | When the value of a loop control variable is not altered by arithmetic, but instead is altered by user input. That is, an event occurs that determines whether the loop continues |
Validating Data | The process of ensuring that a value falls within a specified range. Programmers commonly use indefinite loops when validating input data |
Priming Read(Priming Input) | The first input statement prior to a loop that might contain a value that prevents any execution of the loop |
Counting | Incrementing a variable in a loop to keep track of the number occurrences of some event |
Accumulating | The process of repeatedly increasing a value by some amount |
Add and Assign Operator | The += is the add and assign operator; it adds and assigns in one operation. Ex: count += 1; |
Other Shortcut Operators | You can also use the subtract and assign(-=), the multiply and assign operator(*=), the divide and assign operator(/=), and the remainder and assign operator(%=). Each of these performs the operation and assign the result in one step |
Prefix Increment Operator(Prefix ++) | To use the prefix ++, you type two plus signs before the variable name. The statement someValue = 6; followed by ++someValue; results in someValue holding 7 - one more than if held before you applied the ++ |
Postfix Increment Operator(Postfix ++) | To use a postfix ++, you type two plus signs just after a variable name. The statement anotherValue = 56; anotherValue++; results in anotherValue containing 57 |
1. Difference Between Postfix and Prefix Increment Operators | When a prefix or postfix operator is used as part of larger expressions, it does make a difference which operators you use because they function differently in terms of what they return. |
2. Difference Between Postfix and Prefix Increment Operators | When a prefix operator is used in an expression, the value after the calculation is used, but when a postfix operator is used in an expression, the value used is the one before the calculation takes place. |
3. Difference Between Postfix and Prefix Increment Operators | When you use the prefix ++, the result is calculated, and then its value is used. Ex:... b = 4;... c = ++b;... The result is that both b and c hold the value 5 because b is increased to 5 and then the value of the expression is assigned to c. |
4. Difference Between Postfix and Prefix Increment Operators | When you use the postfix ++, the value of the expression before the increase is stored. Ex:... b = 4;... c = b++;... The result is still that b is 5, but c is only 4. Although b is increased, its original value is assigned to c. |
5. Difference Between Postfix and Prefix Increment Operators | In other words, if b = 4, the value of b++ is also 4, but after the statement is completed, the value of b is 5 |
1. for Loop | A special loop that is used when a definite number of loop iterations is required; it provides a convenient way to create a counter-controlled loop. You being a for loop with the keyword for followed by a set of parentheses. |
2. for Loop | Within the parentheses are three sections separated by two semicolons, The three sections are usually used for the following... 1. Initializing the loop control variable.. 2. Testing the loop control variable.. 3. Updating the loop control variable. |
3. for Loop | Ex:... for(val = 1; val < 11; ++val).. {.. System.out.println(val);.. }.... val = 1;.. while(val < 11).. {.. System.out.println(val);.. ++val;.. } |
Pretest Loop | Loops in which the loop control variable is tested before the loop body executes. Both the while loops and for loops are pretest loops |
Posttest Loop | Loops in which the loop control variable is tested after the loop body executes. The do...while loop is a posttest loop |
do...while Loop | In a do...while loop, the loop body executes before the loop-controlling question is asked even one time. Ex:... do.. {.. total += numberValue;... } while(total < 200);.. |
1. Improving Loop Performance | You can improve loop performance by doing the following... 1. Making sure the loop does not include unnecessary operations or statements... 2. Considering the order of evaluation for short-circuit operators... 3. Making a comparison to 0... |
2. Improving Loop Performance | 4. Employing loop fusion... 5. Using prefix incrementing rather than postfix incrementing |
1. Avoiding Unnecessary Operations | You can make loops more efficient by not using unnecessary operations or statements, either within a loop's tested expression or within the loop body. Ex: while(x < a + b)... //loop body.. |
2. Avoiding Unnecessary Operations | If this loop executes 1000 times, then the expression a + b is calculated 1000 times. Instead, if you use the following code, the results are the same, but the arithmetic is performed only once:... int sum = a + b... while(x < sum)... //loop body |
Considering the Order of Evaluation of Short-Circuit Operators | The expression in each part of an AND or an OR expression use short-circuit evaluation. If you believe that the first Boolean expression is more like to be true than the second one, you can eliminate testing the second one on more occasions |
Comparing to Zero | Comparing a value to 0 is faster than comparing to other values, no matter which comparison operator you use - greater than, less than, equal to, and so on. |
do-nothing Loop | A loop that performs no actions other than looping |
Loop Fusion | The technique of combining two loops into one. If saving a few milliseconds ends up making code harder to understand, you almost always should err in favor of slower but more readable programs |
Using Prefix Incrementig Rather than Postfix Incrementing | Probably the most common action after the second semicolon in a for statement is to increment the loop control variable. The result is identical whether you use x++ or ++x. However, using the prefix increment operator produces a faster loop |