Download - Sentinel Values and Running Totals
Sentinel ValuesSentinel Valuesand and
Running TotalsRunning Totals
Sentient – to be awareSentient – to be aware Ex: Humans are “sentient” beings. We are aware!Ex: Humans are “sentient” beings. We are aware!
Sentinel Value: Sentinel Value: A special value that cannot be mistaken as a regular A special value that cannot be mistaken as a regular
value. Ex: -1value. Ex: -1 A value that signals when the end of a list of values A value that signals when the end of a list of values
can be reached.can be reached. In regards to loops, it is a value that causes a loop to In regards to loops, it is a value that causes a loop to
terminate.terminate.
In the movie “The Matrix” there were In the movie “The Matrix” there were machines called “Sentinels” that attacked machines called “Sentinels” that attacked human spaceships, thereby causing the human spaceships, thereby causing the humans to terminate.humans to terminate.
Ex. of Sentinel Value:Ex. of Sentinel Value:
//In this example, entering -1 causes the loop to end.//In this example, entering -1 causes the loop to end.
while (intNumber != -1)while (intNumber != -1){{
System.out.println(“Hello!”);System.out.println(“Hello!”);System.out.print(“To end this loop enter -1: ”);System.out.print(“To end this loop enter -1: ”);intNumber = keyboard.nextInt();intNumber = keyboard.nextInt();
}}
Running Total:Running Total: A sum of numbers that accumulates with each A sum of numbers that accumulates with each
iteration of a loop.iteration of a loop. It is called “running” because the numbers are It is called “running” because the numbers are
gathered and summed during the “running” of a loop.gathered and summed during the “running” of a loop.
Accumulator:Accumulator: The variable used to keep the running total.The variable used to keep the running total.
Decumulator:Decumulator: The variable used to keep the balance when The variable used to keep the balance when
subtracting from a total.subtracting from a total.
Ex. of a running total:Ex. of a running total:
int intCounter = 1;int intCounter = 1;
while (intCounter < 5)while (intCounter < 5)
{{
intTotal = intTotal + 5 //This is the accumulatorintTotal = intTotal + 5 //This is the accumulator
intCounter ++;intCounter ++;
}}
Deciding Which Loop To UseDeciding Which Loop To Use
““while” loop:while” loop: A pretest loop.A pretest loop. Ideal if you do NOT want the loop to iterate in the Ideal if you do NOT want the loop to iterate in the
beginning if a condition is FALSE.beginning if a condition is FALSE. Also ideal if you want a sentinel value to terminate the Also ideal if you want a sentinel value to terminate the
loop.loop.
““do-while” loop:do-while” loop: A posttest loop.A posttest loop. Ideal if you want the loop to iterate at least once.Ideal if you want the loop to iterate at least once.
Deciding Which Loop To UseDeciding Which Loop To Use
““for” loop:for” loop: A pretest loop.A pretest loop. Has built in expressions for initializing, testing, Has built in expressions for initializing, testing,
and updating.and updating. Ideal for when the EXACT number of Ideal for when the EXACT number of
iterations is known.iterations is known.