CS208 Software programming - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CS208 Software programming

Description:

Use mnemonics to operations and sometimes memory locations ... Semi-Colons (;) used to separate statements. do not use AFTER Begin or BEFORE End. 8/30/09 ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 39
Provided by: academi
Category:

less

Transcript and Presenter's Notes

Title: CS208 Software programming


1
CS208 Software programming

2
Introduction
  • Software
  • Instructions that tell the computer what to do
    (ie. a program)

3
Languages
  • Machine Language
  • zeros and ones
  • Assembly Languages -
  • Use mnemonics to operations and sometimes memory
    locations
  • Each is specific to a particular hardware
    architecture
  • Translated by an Assembler

4
Languages
  • High Level Languages -
  • Use English-like statements to instruct the
    computer what to do.
  • Are more portable (will run on different kinds of
    hardware)
  • Translated by a compiler (produces object code)
  • Linked via linker to other code and library
    routines (produces executable code)

5
Languages
  • High Level Languages
  • Examples
  • -    FORTRAN - scientific/engineering, math
  • -    COBOL - business
  • -    BASIC - education and PCs
  • -    Pascal - education
  • -    C - Application development
  • - C - Application development
  • - Java - Application development
  •  

6
Structured Programming
  • Program is subdivided into modules (one logical
    thought, approx. one page long)
  •  Each page is organized into recognizable
    paragraphs, using indentation to show nesting and
    subordinate clauses.
  •  Embedded comments describe the function of each
    variable and purpose of each module.
  •  Straightforward, easily readable code is
    preferred over slightly more efficient, but
    obtuse, code.

7
Structured Programming
  •  Final program is created via top-down design.
  • Main code with empty modules, slowly refined to
    lowest level
  • Keeps programmer from being overwhelmed by job,
    and allows easy division of work (modules
    assigned to different programmers)

8
Benefits of Structured Programming
  • More readable
  • Easier to maintain
  • More likely to be correct on first run
  • Easier to "prove" by systematic program
    verification

9
Eight-Statement Pascal
  •  program to name the program
  • const to define constants
  • var to declare variables
  • readln to read input
  • writeln to write output
  • assignment () to perform calculations
  • if-then-else to make decisions
  • while-do to loop

10
What a Pascal program looks like
  • program Warning (output)
  • header - required
  • begin required
  • writeln ('DANGER!!')
  • end. required

11
Declarations (Constants/Variables)
  • Primitive (Basic) Data Types
  • Real, Integer, and Char
  •         Integer - whole numbers
  • No commas in numbers
  •         Real - numbers with fractional parts
  • Must have digit before after decimal pt
  •         Char - a single ASCII character
  • enclosed in single quotes

12
Constants
  • Value known prior to start of program AND value
    doesn't change
  • Format const
  • constant-name value

13
Constants
  • Examples
  • const
  • Pi 3.14
  • TaxRate 0.28
  • MaxItems 50
  • BestGrade 'A'

14
Variables
  • allocates memory cell, types it, gives it a name
  •  
  • Format
  • var variable-list type
  •  Examples
  • var
  • Initial char
  • I, Count integer
  • GPA real

15
Variables
  • NOT case-sensitive.
  • good identifiers make code "Self-Documenting"
  • must begin with a Letter, followed by
    letters/digits

16
Executable Statements
  • Between "begin" and "end" (separated by
    semicolons )
  •  Statements can take up more than one line.
  •  Extra blanks ignored - use for readability.
  • Use comments

17
 Assignment Statements
  • assigns a value to an identifier
  •   id 5 (id "gets" 5)
  • types must be compatible
  •  Operators - / DIV MOD translate
    several math formulas. (/ for real nums only, DIV
    MOD for integers)

18
Assignment Statements
  • Swap Example
  • temp a
  • a b
  • b temp
  •  
  • Increment Example a a 1

19
I/O Procedure statements
  • Writeln (output list)
  • procedure call to write output to standard
    output device (w/CR)
  • Writeln writes a blank line with no output on
    it.
  •  
  • Examples Writeln ('Hello')
  • Writeln ('Please enter a number')
  • Writeln ('The answer is', sum)
  •  

20
I/O Procedure statements
  • Readln (input list)
  • procedure call to get input from standard input
    device
  •  
  • Examples Readln (Pay)
  • Readln (Num1, Num2)

21
Semi-Colons ()
  • used to separate statements
  • do not use AFTER Begin or BEFORE End

22
Example
  • Write a program that computes the age of the
    user.

23
(No Transcript)
24
(No Transcript)
25
Example
  • program CalcAge (input, output)
  • Computes and prints age
  • const
  • CurYr 2001
  • var
  • BirthYr, Age integer
  • begin
  • writeln ('Enter year of birth ')
  • readln (BirthYr)
  • Age CurYr - BirthYr
  • writeln ('Your age after this year''s birthday
    is ', Age)
  • end.

26
Boolean Expressions
  • A relational operator tests the relationship
    between two expressions and returns a Boolean
    (TRUE or FALSE) result.
  •  
  • Syntax (ltexprgt ltrelopgt ltexprgt)

27
Boolean Expressions
  • operator example result
  • ------------------------------------------
  • A B TRUE if A is equal to
    B
  • ltgt A ltgt B TRUE if A is not equal
    to B
  • lt A lt B TRUE if A is less
    than B
  • lt A lt B TRUE if A is less than
    or equal to B
  • gt A gt B TRUE if A is greater
    than B
  • gt A gt B TRUE if A is greater
    than or equal to B

28
If Selection Structure
  • Syntax One Alternative
  • if condition then statementT
  • do statement if condition is TRUE
  • Two Alternatives
  • if condition
  • then statementT
  • do if TRUE
  • else statementF
  • do if FALSE

29
If Selection Structure
  • Examples
  • if Age gt 18 then
  • writeln ('Can Vote')
  •  
  • if Divisor ltgt 0.0 then
  • Answer Num / Divisor
  •  
  • if MorD 'M' then if MorD 'M'
  • writeln ('Hi Mom') then writeln ('Hi Mom')
  • else writeln ('Hi Dad')

30
If Selection Structure
  • if Num gt 0
  • then writeln ('Positive')
  • else writeln ('Negative')
  •  
  • if Temp gt 60
  • then writeln ('Tennis Anyone?')
  • else writeln ('Too cold for tennis.')

31
If Selection Structure
  • program CalcAge Computes and prints age
  • Apptype Console
  • const
  • CurYr 2001 LastMon 1
  • var
  • BirthYr, BirthMon, Age integer
  • begin
  • writeln ('Enter year of birth ') readln
    (BirthYr)
  • writeln ('Enter month of birth ') readln
    (BirthMon)
  • Age CurYr - BirthYr
  • if BirthMon gt LastMon then
  • Age Age - 1 Subtract 1 if birthday has not
    yet occurred this year
  • writeln ('Your age is ', Age)
  • writeln(Push return to exit)
  • readln
  • end.

32
If Selection Structure
  • Exercise
  • Write a program that reads in 3 integers and
    outputs the smallest.
  • example
  • 13 8 12
  • The smallest number is 8

33
If Selection Structure
  • program SmallNum
  • Apptype Console
  • var
  • Num1, Num2, Num3, Smallest integer
  • begin
  • Writeln ('Enter three numbers ') Readln
    (Num1, Num2, Num3)
  •  
  • if Num1 lt Num2 then
  • Smallest Num1
  • else Smallest Num2
  •  
  • if Num3 lt Smallest then
  • Smallest Num3
  •  
  • Writeln ('Smallest ', Smallest1)
  • Writeln(Press return to exit) Readln
  • end.

34
Conditional Loop Structure
  • Conditional Loop Structure the WHILE statement
  •   
  • Syntax WHILE (boolean-expression) DO
  • begin
  • statement1
  • statementN
  • end
  •  
  • NOTE begin/end not needed if only one
    statement

35
Conditional Loop Structure
  • Example 1 Display multiples of 5, from 5 to 50.
  •  
  • Count 1
  • WHILE Count lt 10 DO
  • begin
  • Mult 5 Count
  • Writeln (Mult)
  • Count Count 1
  • end

36
Conditional Loop Structure
  • Example 2
  • Error check input, to be sure a positive number
    is entered.
  •  
  • Writeln ('Enter a positive number')
  • Readln (Num)
  • WHILE Num lt 0 DO
  • begin
  • Writeln ('Invalid entry. Try Again.')
  • Readln (Num)
  • end

37
Conditional Loop Structure
  • Student Exercise
  • Write a WHILE loop that will add a series of
    positive integers as they are entered by the
    user. Stop adding and display the total when the
    user enters a negative number.
  • Example
  • 10
  • 20
  • -1
  • Total is 30

38
Conditional Loop Structure
  • Sum 0
  • Num 0
  • WHILE Num gt 0 DO
  • begin
  • Sum Sum Num
  • Writeln ('Enter a positive number (negative to
    exit)')
  • Readln (Num)
  • end
  • Writeln ('The total is ', sum)
Write a Comment
User Comments (0)
About PowerShow.com