Object Oriented Programming Quiz 2 (100)
for loop
A pre test loop. Allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code for(initialization; test; update)
While Loop
A pretest loop, which means that it will test the value of the condition prior to executing the loop. When this condition is true, the statements will execute repeatedly.
Curly Braces
Required to enclose block statement while loops.
Continue
Cause the currently executing iteration of a loop to terminate and the next iteration will begin. Will cause the evaluation of the condition in while and for loops
Caution
Do not use floating-point values for equality checking in a loop control Floating-point values are approximations, using them could result in imprecise counter values and inaccurate results.
Initialization
For loops initialize a counter variable that will be tested by the test section of the loop and updated by the update section. The initialization section can initialize multiple variables.
Rule of Thumb
Loop Counters are Integers
Infinite Loops
Loops that do not end
Do while loop
Post-test loop, which means it will execute the loop prior to testing the condition. Goes through it once before testing.
Input validation
The process of ensuring that user input is valid.
To Use
The while loop: Pre-test loop Use it where you do not want the statements to execute if the condition is false in the beginning. The do-while loop: Post-test loop Use it where you want the statements to execute at least one time. The for loop: Pretest loop Use it where there is some type of counting variable that can be evaluated.
Break
Used to abnormally terminate a loop. If used wrongly, bypasses the normal mechanisms and makes the code hard to read and maintain.
Update
Used to increment or decrement the counter variable(s) declared in the initialization section of the for loop. The update section of the loop executes last in the loop. The update section may update multiple variables.
Sentinel Value
You may use an input value to signify the end of the loop. This makes it predetermined.
Caution 2
You should avoid updating the control variable of a for loop within the body of the loop. The update section should be used to update the control variable. Updating the control variable in the for loop body leads to hard to maintain code and difficult debugging.
Multiple
for(int i = 5, int j = 0; i < 10 || j < 20; i++, j+=2) { statement(s); }
Repetition Structures
while do-while for