CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
 
Class Notes

Wednesday October 18 , 2000
 

Booleans

And (& &)  

< boolean > & & < boolean > -- < boolean >  

Returns true if both inputs are true,  and returns false if not.  

EXAMPLES:  

  boolean x = true;
  boolean y = false;
  boolean z = x & & y;
  boolean w = x & & x;
 

boolean z is a false statement since x is true and y is false.  

boolean w is a true statement since x is true.  

a -- 7  

b -- 4  

c -- 10  

  int a = IO.read_int ();
  int b = IO.read_int ();
  int c = IO.read_int ();
  boolean x = (a < c) & & (a < b);
 

a < c is a true statement since 7 is less than 10..  

a < b is a false statement since 7 is not less than 4..  

Therefore the entire statement is false.  

Or (| |)  

< boolean > | | < boolean > -- < boolean >  

Returns true if at least one input is true,  and returns false otherwise.  

Not (!   )  

!   < boolean > -- < boolean >  

Returns ``the other boolean.''  

SAMPLE PROGRAM USING BOOLEANS:  

  / /Read three integers.
      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);
     
}  
  static private print (int x,  int y,  int z)
  {
      IO.println (x + ````+ y + ````+ z);
 
}
 

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

There are three main mechanisms of iteration:

  • ``while''
  • ``for''
  • ``recursion''
 

Definition   A WHILE STATEMENT is a repetition construct in which the thing being repeated is repeated while some condition is true.

EXAMPLE:   (Counter control loop)  

  / /Read and write four integers.
      int i = 1;
      while (i < = 4)
      {
        int x = IO.read_int ();
        IO.println (x);
        i = i + 1;
     
}
 

EXAMPLE:   (Data control loop)  

  / /Read several nonzero integers and print them.
      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 apriori knowledge of how many times the instruction block is to be run.

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 wherin on 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 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 ellaborate than its predecessor.