Byron's CSC212 Web Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
Byron's CSC212 Web Site  
 
 
 
Class Notes

Wednesday October 18 , 2000
 
Notes on Boolean Data Types,  Control Loops,  and Incremantal Programming.  

 
  Class Notes  -- Wednesday October 18 , 2000

   
   CSC212 - October 18, 2000
   =========================
  
   Lecture Topic: THE BOOLEAN DATA TYPE
  
           Name            Boolean
           Values          True    False
           Operators       &&      (And)
                           ||      (Or)
                           !       (Not)
  
   AND:  &&
           <boolean> && <boolean> -> <boolean>
  
           Returns TRUE if both inputs are true,
           Returns FALSE if not.
  
    Ex:    boolean  x  =   True
           boolean  y  =   False
           boolean  z  =   x    &&   y
                           true && false
                           -------------
                               False
  
           boolean  w  =    x   &&   x
                            true && true
                           -------------
                               True
  
    Ex:    int a = IO.read_int();
           int b = IO.read_int();
           int c = IO.read_int();
  
           boolean x = (a < c) && (a < b);
  
           Assume the input stream is 7, 4, 10
           what will be the values of x = False?
  
  
   OR:  ||
           <boolean> || <boolean> -> <boolean>
  
           Returns True if at least one input is True,
           Returns False otherwise.
    
  
   NOT:  !
  
           !<boolean> -> <boolean>
  
           Returns "the other boolean" (True/False)
  
  
   WAY (3)
  
           // Read 3 ints
  
           int a = IO.read_int();
           int b = IO.read_int();
           int c = IO.read_int();
  
           // Perform Comparisons and Display Results
  
           if ((a < b) && (b < c))
          { 
           print(a,b,c)
          }
           else if ((a < c) && (c < b))
          {
           print(a,c,b)
          {
           else if ... four more cases.
  
           static private print (int x,int y,int z)
          {
           IO.println(x + " " + y + " " + z);
          }
  
  
   DEF: ITERATION is the  act of repeating a construct, or 
        a slight variation on a construct, again and again 
        in some controlled fashion.
  
    Remark:  these are three main mechanisms of ITERATION - 
  
           1)  While
           2)  For
           3)  Recursion
  
   DEF:  A WHILE STATEMENT is a repetition construct in 
         which the thing eing repeated is repeated while 
         some condition is true.
  
       Exerxise: Articulate that better before putting it 
                 on the Web.
  
    Ex.1:  Read and Write four integers.
  
           int i = 1;
           while (i <= 4)
  
           //Counter Control Loop
          {
           int x = IO.read_int();
           IO.println(x);
           i = i + 1;
          }
  
           Incrementation- |
           Input Stream    |  Environment
           --------------------------------------------
           5  8  7  2      | i =  1 -> 2 -> 3 -> 4 -> 5
                           | x -> 5
                           |        -> 8
           Output Stream   |             -> 7
           ----------------|                  -> 2
           5  8  7  2      |
                           |
           --------------------------------------------
  
    Ex.2: Read several non-zero integers and print them.
  
           IO Example -
                    INPUT:  8  -16  -4   3   0
                   OUTPUT:  8  -16  -4   3 
  
           int number = IO.read_int();
           while (!(number == 0))
  
          // Data Control Loop
          {
           IO.println(number);
           number = IO.read_int();
          }
  
           IO Example -
                     INPUT:  8  -16  -4   3   0
                    OUTPUT:  8  -16  -4   3 
  
           Incrementation- |
           Input Stream    |  Environment
           ---------------------------------------------
           8  -16 -4  3  0 | i =  1 -> 2 -> 3 -> 4 -> 5
                           | x -> 8
                           |        -> -16
           Output Stream   |             -> -4
           ----------------|                  -> 3
           8  -16 -4  3  0 |                       -> 0
                           |
           ----------------------------------------------
  
           "Apriorie" = Before Hand Knowledge
  
   DEF:  A COUNTER CONTROL LOOP is a repetition 
         construct which is characterized by "apriori" 
         knowledge of how many times the instruction block 
         is to be run.
  
     Remark: Connter Control Loops are controlled by a 
             counter which governs the behavior of the 
             loop.
  
  
   DEF:  A DATA CONTROL LOOP is a repetition comstruct 
         which is governed by "meta-data" accompanying 
         the data being processed.
  
     Remark:  Meta Data = Data about the data.
  
   DEF: INCREMENTAL PROGRAMMING is a programming
        methodology wherein one developes a program 
        by writing a sequence of programs with the 
        following properties.
  
      1) The first program in the sequence performs an 
         essentially trivial task, but one which is 
         fundamental to completion of the ultimate task.
  
      2) The last program in the sequence performs the 
         task of ultimate interest.
  
      3) Each successor program performs a task only 
         marginally more elaborate than its predicessor.
  
    Ex:  P0, P1, P2, P3, ... Pn
  
         _______________________________________________
        |               Whatever Directory              |
        |_______________________________________________|
                 |       |       |       |       |
               (V.0)   (V.1)   (V.2)   (V.3)...(V.n) 
              ___|__  ___|__  ___|__  ___|__  ___|__
             |What- ||What- ||What- ||What- ||What- |
             |  ever||  ever||  ever||  ever||  ever|
             |______||______||______||______||______|
  
  
    Problem: Read the integers and display the difference
             between the largest and the smallest values.
  
            Ex:    8   15    9   ->   7 
                 +50   -6   10   ->  56
  
    The sequence of programs will be:
  
     P0: Read & Echo three integers.
     P1: Same as P0, plus determine & display the max
          value.
     P2: Same as P1, plus determine & display the minimum
          value.
     P3: Same as P2, plus compute & display the difference
          between the max & min values.
     Pn: Same as P3, but delete all extra output.
   =======================================================
   123456789112345678921234567893123456789412345678951234567