P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Wednesday October 18 , 2000
 
Synopsis
In this class we covered counter control and meta data loops and how to translate them.   We're also introduced to the boolean data type and decision making with it.
  The boolean Data Type  

  Name:   boolean
  Values:   true,  false
  Operators:   & & (and)
  | | (or)
  !   (not)
and & &
ttline {1} {< boolean > & & < boolean > - > < boolean >} returns true if both inputs are true,  false if not.  

Ex:
  boolean x = true;
  boolean y = false;
  boolean z = x & & y;  / /z evaluates to false
 
  boolean w = x & & x;  / /w evaluates to true
 

Ex:
  int a = IO.read_int ();  / /a - > 7
  int b = IO.read_int ();  / /b - > 4
  int c = IO.read_int ();  / /c - > 10
  boolean x = (a < c) & & (a < b);  / /a < c - true & a < b - false
 

Assume the input stream is 7 4 10
What will the be the value of x?   false  

OR | | ttline {1} {< boolean > | | < boolean > - > < boolean >} returns true if at least one input is true,  false if not.  

NOT! ttline {1} {!   < boolean > - > < boolean >} returns ``the other boolean''  

Way 3 - the comparison program
  / /Read 3 ints
  int a = IO.read_int ();
  int b = IO.read_int ();
  int c = IO.read_int ();
 
  / /perform comparison and display results
  if ((a < b) & & (b < c))
  {
  IO.println (a,  b,  c);
 
}   else if ((a < b) & & (c < b))
  {
  print (a,  c,  b);
 
}   else ---
  there will be fore more cases!

Definition   ITERATION is the act of repeating a construct,  or a slight variation on a construct again and again in some controlled fashion.

REMARK
There are three main mechanisms of iteration.

  • ``while''
  • ``for'' - most specific
  • recursion
 

Definition   a WHILE statement is a repetition construct in which the item that is to be repeated is done so until a condition is met or true.

 

Ex:
Read and write four integers.  

Counter Control Loop
  int i = 1;
  while (i < = 4)
  {
  int x = IO.read_int ();
  IO.println (x);
  i = i + 1;
 
}
 

Ex:
Read several non zero integers and print them.
IO example:
in:8 - 16 - 4 3 0
out:8 - 16 - 4 3 - end of program since 0 is entered  

Meta data control loop
  int number = IO.read_int ();
  while (!   (number == 0))
  {
  IO.println (number);
  number = IO.read_int ();
 
}
 

Definition   A COUNTER CONTROL LOOP is a repetition construct which is characterized by a priori (beforehand) knowledge of how many times the instruction block is to be run.

 

REMARK
Counter control loops are controlled by a counter which governs the behavior of the loop.  

Definition   a DATA CONTROL LOOP is a repetition construct which is governed by ``metadata'' accompanying the data being processed.

 

Definition   INCREMENTAL PROGRAMMING is a programming methodology wherein one develops a program by writing a sequence of programs with the following properties:

  1. The first program in the sequence performs some 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 predecessor.

 

Ex: P0 P1 P2 P3 ....   Pn
P1 is the first successor of the base program P0 and so on.  

INSERT DIAGRAM HERE!  

Versioning - If P2 is screwed up we can fall back on P1 and proceed from there.  

Problem:
Read three integers and display the difference between the largest and the smallest values.
  Input Result
  ----- ------
  8 15 9 7
  50 - 6 10 56
 

The sequence of programs will be...

  • P0:   Read and echo 3 integers.
  • P1:   Same as P0,  plus determine and display the max value.
  • P2:   Same as P1,  plus compute and display the difference.
  • P3:   Same as P2,  plus compute and display the difference between the max & min values.
  • P4:   Same as P3,  but delete all ``extra'' output.