1 / 8

Sentinel Values and Running Totals

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.

noel-jones
Télécharger la présentation

Sentinel Values and Running Totals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sentinel Valuesand Running Totals

  2. 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. • In regards to loops, it is a value that causes a loop to terminate.

  3. In the movie “The Matrix” there were machines called “Sentinels” that attacked human spaceships, thereby causing the humans to terminate.

  4. Ex. of Sentinel Value: //In this example, entering -1 causes the loop to end. while (intNumber != -1) { System.out.println(“Hello!”); System.out.print(“To end this loop enter -1: ”); intNumber = keyboard.nextInt(); }

  5. Running Total: • A sum of numbers that accumulates with each iteration of a loop. • It is called “running” because the numbers are gathered and summed during the “running” of a loop. • Accumulator: • The variable used to keep the running total. • Decumulator: • The variable used to keep the balance when subtracting from a total.

  6. Ex. of a running total: int intCounter = 1; while (intCounter < 5) { intTotal = intTotal + 5 //This is the accumulator intCounter ++; }

  7. Deciding Which Loop To Use • “while” loop: • A pretest loop. • Ideal if you do NOT want the loop to iterate in the beginning if a condition is FALSE. • Also ideal if you want a sentinel value to terminate the loop. • “do-while” loop: • A posttest loop. • Ideal if you want the loop to iterate at least once.

  8. Deciding Which Loop To Use • “for” loop: • A pretest loop. • Has built in expressions for initializing, testing, and updating. • Ideal for when the EXACT number of iterations is known.

More Related