CMP 131 Introduction to Computer Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CMP 131 Introduction to Computer Programming

Description:

New material for today/this week/next week: Selection. Making decisions. ... Car : Cadillac'; CONST. Score : integer; VAR. X, Y, Z : real; Score, Num : integer; ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 20
Provided by: violettaca
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: CMP 131 Introduction to Computer Programming


1
CMP 131Introduction to Computer Programming
  • Violetta Cavalli-Sforza
  • Week 6, Lecture 1 (Monday)

2
TODAY
  • Midterm exam will be Monday April 23
  • Still waiting for programs from homework 2
  • Go over remainder homework 2, as part of review
    for midterm exam
  • Formatting with write, writeln
  • New material for today/this week/next week
  • Selection. Making decisions.
  • Different types of choices Yes/No choice.
    Either/or choice. Multiple choices.
  • Boolean expressions
  • Conditional statements in some detail
  • Nesting conditional statements

3
Homework 2 Review
4
Exercises 1.4, 2 12 points
  • Valid
  • b) Payroll
  • c) Room222
  • e) A
  • f) A1
  • k) ListOfEmployees
  • Invalid
  • a) 7up
  • d) Name List
  • g) 1A
  • h) TimePlace
  • i) CONST
  • j) XY
  • l) Lima, Ohio

5
Exercises 1.4, 4 3 points
  • The three main sections of a Pascal program are
  • Program heading
  • Declaration section
  • Executable section / program body
  • You tell me what is in each part.

6
Exercises 1.4, 6 6 points
  1. CONST Company General MotorsVAR Salary
    real
  2. VAR Age 25
  3. VAR Days integer Ch charCONST
    Name John Smith

Swap order
7
  1. CONST Car Cadillac
  2. CONST Score integer
  3. VAR X, Y, Z real Score, Num
    integer

This is okay!
8
Exercises 1.4, 7 2 points
  • Semicolon is a statement separator
  • Semicolon is not needed before an END because END
    is not a statement
  • A semicolon is also not needed before an ELSE
    (well see this later)
  • Stmt stmt stmt ? separator (correct)
  • Stmt stmt stmt ? terminator (accepted)

9
Exercises 1.5, 16 6 points
  • What type of data is appropriate for each of the
    following
  • Your age integer
  • Your grade point average (GPA) real
  • Your name string (not char)
  • A test score integer or real
  • The average test score real
  • Your grade char, integer, real, string

10
Part c)
  • Following the example of calculating the mean
    that we used in class during Week 3 of the
    course, develop the problem statement, analysis,
    and design for a program that calculates the
    maximum of a set of numbers. You don't know how
    many numbers you will be given. You can assume
    that all numbers will be greater than 0.
  • Use example from Week 3.1, Slide 29.

11
Maximum of N
  • PROCESSING
  • Initialize Old to 0Prompt for New numberRead
    Newwhile New is not equal to -99999 do
  • IF New gt Old THEN Old New Prompt for
    New Read New
  • Write the value of New New stores the
    maximum value

called a sentinel value because it guards the loop
This is also an example of indefinite looping
You DO NOT know how many times you go around the
loop until you finish.
12
Minimum of N (very similar to Maximum)
  • PROCESSING
  • Initialize Old to MaxIntPrompt for New
    numberRead Newwhile New is not equal to -99999
    do
  • IF New lt Old THEN Old New Prompt for
    New Read New
  • Write the value of New New stores the
    minimum value

called a sentinel value because it guards the loop
This is also an example of indefinite looping
You DO NOT know how many times you go around the
loop until you finish.
13
New Material
14
Types of Statements
  • Input/Output for user-program communication
  • Assignment to store (intermediate) results
  • Control Structures to determine which other
    statements are executed and when
  • conditional or selection statements content is
    executed once, if at all
  • looping, repetition, or iteration statements
    content is executed several times

15
What about computations?
  • Computations are performed through expressions.
    Examples
  • 3 X
  • a lt b
  • If you dont use them in other statements you
    might as well not perform them
  • Assign them to variables with an assignment
    statement
  • Ouput them via output statements
  • Use them in control structures to determine what
    statements are executed.

16
Looping/Iteration/Repetition Statements
  • Three terms for the same idea Performing an
    action or a set of actions a number of times
  • Fixed or definite repetition
  • You know how many repetitions before you enter
    the loop
  • Also counter-controlled repetition
  • Pascal uses FOR loop
  • Example printing out a rectangle
  • Variable or indefinite repetition
  • You dont know how many repetitions you will do
    before you enter the loop
  • Sentinel-controlled loops
  • Pascal uses two types of loops
  • WHILE loop
  • Pretest loop condition is tested at the top of
    the loop and therefore before ever entering the
    loop
  • Body of loop executed 0 or more times
  • REPEAT loop
  • Postest loop condition is tested at the top of
    the loop
  • Body of loop executed at least one time in order
    to reach the condition
  • Examples of all of these in slides from Week
    3.1.
  • We will look at these in more detail later

17
Conditional / Selection Statements
  • The basic idea, at the level of the algorithm, is
    that there is a choice to be made
  • Three types of choices
  • Yes/no choice. If yes, do something, otherwise
    do nothing
  • Either/or choice. A 2-way choice. Do something
    different in each case.
  • Multiple choice. An N-way chice. Do something
    different in each case. It can be expressed as a
    series of 2-way choices.
  • Like most programming languages, Pascal provides
    a way of expressing these choices.

18
Combining Statements
  • The various types of statements can be combined
    in many ways.
  • In particular, control statements can be nested
    inside each other. Nesting is a visual/physical
    relationship but also a control relationship.
  • One loop can be performed inside another
  • A conditional statement can control whether a
    loop is executed
  • A conditional statement can be executed several
    times inside a loop
  • When several statements are controlled by another
    statement we often use BEGIN and END to create
    compound statements

19
Example Drawing Rectangles
  • FOR I 1 to HeightBEGIN FOR J 1 TO
    Width write() writelnEND

This FOR loop controls only the input/output
statement write().
This FOR loop controls a compound statement
enclosed by the BEGIN and END.
The inner FOR statement is nested inside the
outer FOR statement.
Write a Comment
User Comments (0)
About PowerShow.com