QMCS 230: Today in Class - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

QMCS 230: Today in Class

Description:

Used to apply a set of operations a fixed number of times ... Example: program that averages a bunch of positive numbers. Input: collects one number at a time ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 10
Provided by: ricks6
Category:
Tags: qmcs | class | definition | of | today

less

Transcript and Presenter's Notes

Title: QMCS 230: Today in Class


1
QMCS 230 Today in Class
  • More Loops
  • Do While a trailing decision loop
  • For loop for doing a sequence
  • Accumulation, Sentinel values
  • Tomorrow lab using a count controlled loop

2
Recap Parts of a Program
  • The Envelope
  • Class definition, Main definition
  • Well just use them as patterns for now
  • Variable declarations
  • First thing in Main
  • First part of setting up your program to run
  • Some variables are initialized, not necessarily
    all of them
  • Each has a comment explaining its purpose
  • Executable Statements
  • BLANK LINE separates them from variable
    declarations
  • These are things that run when your program
    runs
  • Clusters of 2-5 with a comment at the start of
    each cluster
  • Indentation of Ifs and Loops

3
Parts of a loop
  • Statements OutsideThe Test condition
  • Statements Inside
  • More Statements Outside

4
Do While
  • A Trailing Decision Loop
  • do
  • statements to execute
  • while (condition-remains-true)
  • Loop statements are performed once
  • Then repeated as long as the condition remains
    true

5
Leading vs Trailing decision
  • Error messages and input loops
  • Read input
  • If error, repeat
  • Print error message
  • Prompt for another input
  • Repeat
  • Prompt for input, giving rules
  • Read input
  • Until input is valid

6
The For Loop
  • Count controlled loop
  • Used to apply a set of operations a fixed number
    of times
  • Traditionally applied to lists and arrays
  • (we havent studied those yet)
  • for (initial-stmt test-stmt update-stmt)
  • statements to perform
  • Usually the loop header has a variable that
    indicates which iteration you are on

7
Accumulating Results
  • We already did that in the Payroll example
  • The idea repeating an operation several times,
  • Accumulating partial results as you proceed

8
Sentinel Values
  • Example program that averages a bunch of
    positive numbers
  • Input collects one number at a time
  • Once a negative number arrives, average the
    previously collected ones

9
Creative Commons License
  • This work is licensed under the Creative Commons
    Attribution-Share Alike 3.0 United States
    License. To view a copy of this license, visit
    http//creativecommons.org/licenses/by-sa/3.0/us/
    or send a letter to Creative Commons, 171 Second
    Street, Suite 300, San Francisco, California,
    94105, USA.
Write a Comment
User Comments (0)
About PowerShow.com