sentinel values and running totals

8
Sentinel Values Sentinel Values and and Running Totals Running Totals

Upload: noel-jones

Post on 31-Dec-2015

11 views

Category:

Documents


0 download

DESCRIPTION

Sentinel Values and Running Totals. Sentient – to be aware Ex: Humans are “sentient” beings. We are aware! Sentinel Value: A special value that cannot be mistaken as a regular value. Ex: -1 A value that signals when the end of a list of values can be reached. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Sentinel Values and  Running Totals

Sentinel ValuesSentinel Valuesand and

Running TotalsRunning Totals

Page 2: Sentinel Values and  Running 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.

Page 3: Sentinel Values and  Running Totals

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.

Page 4: Sentinel Values and  Running Totals

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();

}}

Page 5: Sentinel Values and  Running Totals

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.

Page 6: Sentinel Values and  Running Totals

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 ++;

}}

Page 7: Sentinel Values and  Running Totals

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.

Page 8: Sentinel Values and  Running Totals

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.