Chapter 5 Flashcards
List the various types of while loops.
- Counter-Controlled while loop
- Sentinel-controlled while loop
- Flag-controlled while loop
- EOF (end of file) controlled while loop
What is an Infinite loop?
A loop whose execution continues forever.
Define Counter-controlled while loop.
Used when you know exactly how many times you need the loop to execute.
Define Sentinel-controlled while loop.
Uses a sentinel: a special value that can be used to indicate the end of processing. Used to control the loop.
Define Flag-controlled while loop.
Used Boolean variable to control the loop.
Define EOF (End of File) controlled while loop:
Loops while there is still data to be read in from the input file.
What is a for-loop?
A specialized form of the While loop. It’s primary purpose is to simplify the writing of counter-controlled loops. Used for counting.
(Pre-test loop)
Code for While loop
While (expression)
Statement
Example:
While (Price != -1)
{
Statement
}
Code for For Loops.
for (initial statement; loop condition; update statement)
{
statement
}
Code for Do While loops.
do { statement (aka code) } while (expression);
(Post-test loop)
What is a post test loop?
Loop always executes at least once
What is a pre-test loop?
Tests loop condition before executing loop instructions.
It is possible that the loop instructions will never execute.
When to use a While loop.
The example we used calculated tax and had the option to type “-1” and end the loop.
While(price != -1)
When to use a For loop.
Used for counting.
Example used in notes:
“create a program that will print out all of the even numbers within a range that the user specifies”
When to use a do while loop
Couldn’t find anything. Look at program on N drive.