Ch6. Art of Programming
Testing techniques for while loops
1. Does the program produce correct results with a set of known inputs? 2. Does the program produce correct results if the sentinal value is the first and only input
enables us to read data easily from a text file
Scanner class
is when we dont know how many times the loop will execute; that is when the loop beings, we dont know how many iterations of the loop will be required.
While loop
In this operation we initialize a total baribale to 0>Each time we input a new value we add the value to the total.When we reach the end of the input, the current value of total is the total for all the input.
accumalation operation
we initialize a total variable to 0 before starting the loop. In the loop body, we add each input value to the total. When the loop completes the current total is the total for all processed input values.
accumulation programming technique
A semicolon that immediately follows the condition indicates
an empty loop body
if you know the number of times a loop will be running you can use
count controlled loop
we intitilize a count varibaale to 0 vefore starting the loop. in the body of the loop we increment the count varibale for each input value that meets out criteria. when the loop completes the count variable contains the number of i tems met out criteria
counting programming technique
type of loop is used for validating input
do while
loop style where the ocndition is tested at the end of the loop
do/while loop
it also posssible to construct a while loop whose conditions never evaluates to false. It results in a
endless or infinite loop
We rely on this to tell us when we have processed all of the data
event
processign of items continues until the end of input is signaled either by a sentinel value or by reaching the end of the file
event controlled looping
when you process each input value until a signal or event tells us that there is no more input
event controlled looping
to avoid generating exceptions when the user types characters other than the data type expected we use
has Next method
the method in the scanner class that reads the info in a file
hasNext
each execution of the loop body is called
iteration
is ran when a loop condition is true
loop body
typically used in a for lop. we set its initial value to 0 in the initialization statement, increment or decrement its value in the loop update statement and check its value in the loop condition
loop counting variable
to make sure the condition have to be false at some point of time you have to include a...
loop update statement
useful when performing multiple operations inside of loops
nested loops
after performing any initialization we attempt to read the first item we call this.. because we feed the condition of the while loop for the first iteration
priming read
is a special input value that signals the end of the items to be processed.
sentinal value
the signal for the end of the input may be a special value that the user enters
sentinel value
if the loop of a condiiton is false the first time it is tested
the body of the for loop is skipped
which loop is designed for the count controlled loop,
the for loop is designed for the count controlled loop that is when the number of iterations is determined before the loop begins.
the important thing to understand about a nested loop
the inner or nested loop executes compelty for each single iteration of the outer loop
can increment or decrement the loop variable by any value
the loop update statement
when you read the next input data
update read because we update the data item in preparation for feeding its value into the condition of the while loop for the next iteration;.
to find the average
we combine accumulation and counting
when the has Next method returns false we know
we have reached the end of the file
Scanning a file we check
whether ther is more data in the file and then read the data
when you want to calculate the sum of a set of values
you use accumulation
it is possible that the whiel loop body is never executed. in that case it is called..
zero iterations