PASCAL I - Control Structures - PowerPoint

Document Sample
PASCAL I - Control Structures - PowerPoint Powered By Docstoc
					   PASCAL I - Control Structures



Philip Fees
CS241



              CS241 PASCAL I - Control Structures   1
         Week 1 Topics
 Reserved Words
 Syntax Diagrams
 PASCAL Program
 Basic Data Types
 Basic Output




            CS241 PASCAL I - Control Structures   2
          Reserved Words
 See page 29-30
 Identifiers that are part of the syntax of
  the language
 Cannot be used for constant, type,
  variable, function, procedure identifier
  names


              CS241 PASCAL I - Control Structures   3
            Syntax Diagrams
 See page 30-31
 Graphic representation of a language‟s
  grammar
 Identifier:
    [A-Za-z][A-Za-z0-9]*




                 CS241 PASCAL I - Control Structures   4
       PASCAL Program
 See page 32
 What is the minimum PASCAL
  program?




           CS241 PASCAL I - Control Structures   5
         Basic Data Types
 integer (-maxint <= integer <= maxint)
 real (4567.123, 4.567123 x 103,
  4.567123E3)
 char („a‟, „7‟, „ „)
 String constant (const name = „sally‟;)




             CS241 PASCAL I - Control Structures   6
            Basic Output
 write - output without newline terminator
 writeln - output with newline terminator
 writeln(expr1[:n[:n]], expr2[:n[:n]], … ,
  exprN[:n[:n]]);




             CS241 PASCAL I - Control Structures   7
                Exercises
   Exercise #39, page 52




              CS241 PASCAL I - Control Structures   8
          Week 2 Topics
 Arithmetic Operators
 Basic Debugging (tracing and print)
 Basic Input
 Constants
 Standard Functions




             CS241 PASCAL I - Control Structures   9
       Arithmetic Operators
 ( ), *, MOD, DIV, /, +, -
 What is an operator?
 Precedence - evaluation sequence of
  multiple operator expressions
 Typically left to right, but can be inside
  to outside or right to left.


              CS241 PASCAL I - Control Structures   10
                Basic Input
 Variables - name vs. memory location
 Data pointer
    – location of next data item to be read from
      input stream
 read - input next data item
 readln - input next data item, skip to
  next line

                CS241 PASCAL I - Control Structures   11
                         Constants
   Maintenance - descriptive name, one
    location updates
    CONST
          pi = 3.14;
    VAR
          area, radius : real;
    ...
          area := 3.14 * radius * radius;
    vs.
          area := pi * radius * radius;
                        CS241 PASCAL I - Control Structures   12
          Standard Functions
 pp. 92 & 96
 what is a function
    – perform some operation on argument(s)
    – returns value(s) as determined by
      operation
   Example
    area := pi * sqr(radius);

                 CS241 PASCAL I - Control Structures   13
             Week 3 Topics
   Boolean Logic
    – Relational Operators
    – Boolean Expressions
 IF THEN ELSE Statements
 CASE Statements




               CS241 PASCAL I - Control Structures   14
       Boolean Expression
 Relational Operators (=, <, >, <=, >=,
  <>) used to build Boolean Expressions
 Logical Operators (AND, OR, NOT)
  used to build Compound Boolean
  Expressions
 Exercise: pp 177-178 #16-22, #28-33



             CS241 PASCAL I - Control Structures   15
        IF THEN Statements
 A decision-making statement
 Syntax:
    IF <boolean expression> THEN
       <statement>
   Review flow diagram, figure 5.1 pg. 179



                CS241 PASCAL I - Control Structures   16
       Compound Statements
 Treats a set of statements as one
 Good programming practice even for
  single statement. Why?
 Syntax:
    IF <boolean expression> THEN
    BEGIN
       <statement1>;
       <statement2>;
       ...
       <statementN>;
    END;
                       CS241 PASCAL I - Control Structures   17
    IF THEN ELSE Statements
 Action to occur only if boolean
  statement is false
 Review flow diagram, figure 5.2 pg. 187




             CS241 PASCAL I - Control Structures   18
     Nested IF Statements
 When the <statement> part of IF or
  ELSE is an IF THEN ELSE statement
 Nested IF ELSE is considered a single
  statement and doesn‟t require BEGIN
  END
 Review Example 5.13 on page 197
 WARNING: ensure ELSE matches to
  correct IF
            CS241 PASCAL I - Control Structures   19
         CASE Statement
 Abbreviated IF THEN, ELSE IF THEN,
  etc statement
 Review flow diagram, figure 5.4 pg. 209
  and CASE rules on pg. 210
 protect case with IF THEN ELSE or use
  of OTHERWISE


             CS241 PASCAL I - Control Structures   20
                      Exercises
 Modify LAB #2 to improve the display
 New features
    – If the coefficient is 1 don‟t display the 1:
       1x + 2y = 5 becomes x + 2y = 5
    – if the coefficient is 0 don‟t display:
       0x + 5y = 5 becomes 5y = 5
    – display subtraction
       2x + -1y becomes 2x - y = 0
    – give error if division by 0 would occur

                    CS241 PASCAL I - Control Structures   21
          Week 4 Topics
Nested Selection
No additional information available




             CS241 PASCAL I - Control Structures   22
              Week 5 Topics
   Repetition Problem
   Conditional Branch
   Pre-testing (Top-testing) Loops
   Post-testing (Bottom-testing) Loops
   Comparing Loops




                 CS241 PASCAL I - Control Structures   23
            Repetition Problem
   A class of problems that can be solved by
    repeatedly performing some task until some
    condition is no longer valid.
   Example 1: Monopoly, “Go to Jail” until you
    roll doubles, 3 rolls, or pay $50.
   Example 2: N ! = 1 * 2 * 3 * ... (N-1) * N
   Iterations could be written as sequence of
    steps - # of iterations may vary.

                CS241 PASCAL I - Control Structures   24
               Conditional Branch
   Predates looping constructs
    label:
       statement1;
       statement2;
       ...
       statementN;
       if ( boolean expression is true)
             goto label;

   Exercise: Write N! using conditional branch
    logic
                       CS241 PASCAL I - Control Structures   25
            Loop Terminology
   initialization - assign values before evaluation
   evaluate - determine if loop should continue
   increment - modify or increase loop controls
   iteration - one pass over loop (evaluate, body,
    increment)
   loop body - syntax that is executed with each
    iteration of loop


                 CS241 PASCAL I - Control Structures   26
    Pre-testing (Top-testing) Loops
   Evaluates looping condition before executing
    body of loop
   Body of loop executes a minimum of 0 times
   Pascal has FOR and WHILE loops
   FOR loop (pg. 239)
    – based on FORTRAN FOR loop
    – used for fixed increment looping
   WHILE loop (pg. 252)
    – General purpose top-testing loop

                  CS241 PASCAL I - Control Structures   27
                    For Loop
   Syntax:
    FOR <var> := <init> [TO | DOWNTO] <value> DO
   Good practice to have maximum iteration
    value defined as a CONST or VAR (i.e., don‟t
    hard code).
   Loop control variable may or may not be
    defined as VAR
   Exercise: pg. 251 7-10

                CS241 PASCAL I - Control Structures   28
                 While Loop
   Syntax:
    WHILE <Boolean expr> DO
   Exercise: pg. 264 #3, 4, 5




                CS241 PASCAL I - Control Structures   29
     Post-testing (Bottom-testing)
                Loops
   Evaluates looping condition after executing
    body of loop
   Body of loop executes a minimum of 1 times
   Syntax:
    REPEAT
    UNTIL <Boolean expr>
   Exercise: pg. 272 #3, 4, 5.


                 CS241 PASCAL I - Control Structures   30
             Comparing Loops
   Each loop can be rewritten in another loop‟s
    syntax
   Easier to use similar testing loops (i.e. for and
    while)
   Review Example 6.22, and 6.23 on pg 275




                  CS241 PASCAL I - Control Structures   31
                   Exercises
   Write one of the following:
    – pg. 312 # 12 (easier) NOTE: read term from
      keyboard instead of file
    – pg. 310 # 6a (moderate)
    – pg. 311 #8 (harder)




                 CS241 PASCAL I - Control Structures   32
             Week 6 Topics
   Nested Loops




               CS241 PASCAL I - Control Structures   33
                  Examples
   pg. 285
   Example 6.26 pg. 285
   Example 6.30 pg. 291
   Program Problem 6 b pg. 310




               CS241 PASCAL I - Control Structures   34
          Week 7 & 8 Topics
   Subprogramming
   Procedures
   Parameters
   Side Effects




              CS241 PASCAL I - Control Structures   35
             Subprogramming
   Modular - readability, exchange (swap), and
    fix parts
   Subtasks - repetitive execution of sub
    functionality
   Structured - receive values, perform task,
    return result
   Black Box design - Lego building blocks


                CS241 PASCAL I - Control Structures   36
                 Procedures
   Design to perform small discreet task
   Thought of as a small program - test as such
   Program is a collection/series of procedure
    (function) calls
   Discuss procedure format slide (pg. 321)




                CS241 PASCAL I - Control Structures   37
           Procedures (cont.)
   Placed in declaration section of
    program/procedure/function
   use {-------------} to denote procedure
    boundary
   procedure name unique to program (scope)




                CS241 PASCAL I - Control Structures   38
           Procedure Execution
   a procedure is called or invoked by name
   flow of control jumps to first line in procedure
   on completing procedure, flow of control
    returns to first line after procedure call
   walk through exercise 11 pg. 335




                  CS241 PASCAL I - Control Structures   39
                 Parameters
   PROCEDURE <name> ( <parameter list> );
   parameter list format: similar to VAR format
   Discuss parameter notes slide




                 CS241 PASCAL I - Control Structures   40
             Parameter Types
   formal - variables in procedure heading
    (parameters)
   actual - values in procedure call (arguments)
   call by value (value parameters) arguments
    are copies to parameters
   call by reference (variable parameters)
    parameters refer to arguments


                 CS241 PASCAL I - Control Structures   41
                 Side Effects
   Unintended change in a variable
   Typically associated with call by reference
    parameter passing
   Considered “bad” programming. Why?
   Sometimes desirable. When/Example?




                 CS241 PASCAL I - Control Structures   42
                     Exercise
   Walk through program on pg. 349
   Do prime program in class #2, pg. 372
   Lab: #3 pg. 372, #8 pg. 373, prime program
    above.




                CS241 PASCAL I - Control Structures   43