CIS162AD -- C Sharp (C#) -- Section 5832
Professor: Dave Hammer dwhammer@cox.net
http://members.cox.net/dwhammer/
Student: Patrick Moss patmoss@patmoss.com
http://www.patmoss.com/cis162ad/
Worksheet #3
Due Thursday, February 23, 2006
I. Definitions
 
   1. boolean variable (p. 93-94)
      Can only take on one of two states, True or False.
      Use C# variable value type "bool".
      
   2. Unicode character (p. 807)
      The character set used by programmers of C#. The
      Unicode character set uses 16 bits to represent a
      character. Thus 2^16 or 65,536 unique characters
      can be represented.
      
   3. switch statement (p. 806)
      The switch statement is considered to be a multiple
      selection structure. It allows you to perform a 
      large number of alternatives based on the value of
      a single variable. This variable or expression must
      evaluate to an integral or string value, such as
      short, int, long, char, and string. It cannot be
      used with double, decimal, or float variables.

   4. break statement (p. 286)
      The "break" statement is used with the switch statement
      to provide an immediate exit from the switch structure.
      You can also place a break statement in the body of a
      loop to provide an immediate exit from the loop.

      Note: There is also a "continue" statement. When reached,
      a new iteration of the nearest enclosing while, do...while,
      for, or foreach, statement is started. The continue statement
      immediately transfers control to the conditional expression
      for an evaluation.

   5. conditional operator (a.k.a. ternary operator) (p. 806, 226-7)
      The ternary operator consists of a question mark and a colon.
      It provides a way to express a simple if...else selection
      statement. 

      For example, grade = examScore > 89 ? 'A' : 'B';
      If the score is greater than 89, the grade is 'A', else the
      grade is 'B'.

   6. logical operator (p. 802, 203-6)
      The logical operators in C# are &&, ||, &, |, and !.
      Expression AND = &&, inclusive OR = ||, including
      "short-circuit evaluation."
      Expression AND = &, inclusive OR = |, not including
      "short-circuit evaluation."
      Expression NOT = !.

   7. The NOT operator (a.k.a. logical negation operator) (p. 802)
      The exclamation symbol (!) is a logical negation operator.
      It is a unary operator that negates its operand. If the
      operand true, it returns false. If the operand is false,
      it returns true.

   8. nested if...else statement (p. 802, 217-222)
      When you place an if...else statement block within another
      if...else statement block, you create a nested if...else
      statement.

II. Questions or short essay
 
   1. What is the difference between the = operator and the
      == operator?
      
      = is the assignment operator (p. 796)
      For example, x = y + 5;

      == is the equality operator (p. 199-200)
      For example, if (x == y) a = 1;

   2. What are the three basic programming constructs? (p. 196)
      General-purpose programming languages provide three
      categories of programming statements, called the basic
      programming constructs, as follows:

      a. simple sequence
         Execute one statement after another, in ascending 
         memory-address sequence.
      b. selection statement, e.g., if (x == y) a = 1;
         Allows you to deviate from the sequential path, 
         based on the result of a test decision.
      c. iteration (or looping)
         Enables you to write instructions that can be repeated.
         The three elements for a loop are: initialize, increment
         /decrement, and test.  
         Note: An alternative to iteration is recursion, which
         utilizes a stack structure to allow execution the same
         instructions repeatedly until a condition is reached.

   3. If you combine && and || in an expression, which has higher
      precedence? How can you control the precedence of operators
      in an expression with mixed logical operators?
      a. && has higher precedence than || (p. 227-8, 793)
      b. We use parentheses to change the precedence order
         in an expression.
         
   4. What is the advantage of using a switch statement rather
      than nested if statements? 

      Nested if statements can be difficult for a human reader
      to understand. An equivalent switch statement structure
      is much easier to read and to understand; it provides 
      better implicit program documentation.

   5. Explain the indentation style that should be used with
      the selection statement.

      We normally indent several spaces for each dependent
      statement within a control structure.  (p. 208, 212, 222-3)
      
      For example:
      
      if (x == y)
        a = 1;
      else
        a = 2;

      switch (expression)
      {
        case value1:
          (statements...)
          break;
        case value2:
          (statements...)
          break;
        default:
          (statements...)
          break;
      }

   6. What is the syntax of the if...else statement? (p. 212)
      
      if (condition)
        { true stmts; }
      else
        { false stmts; }

   7. What are the relational operators? (p. 804, 201-3)
      The relational operators are >, >=, <, <=.
      Relational operators allow you to test variables to determine
      if one is greater or less than another valiable or value.
      And to deviate from the sequential path, based on the result
      of the test decision.

      For example, if (x > y) a = 1;

   8. What does it mean that && and || are short-circuiting?
      (p. 205-6)
      An expression containing && and/or || is only evaluated until
      the true/false result can be determined. Then the remaining
      tests (if any) are not performed.

      This can be important:
      a. When we include an increment/decrement operator for a
         variable then it may not be performed, such as:
         if ((x > y) && < (p < q++)) a = 1;
         Here, if x > y, then the p < q++ test will not be
         performed, and, therefore, q will not be incremented.

      b. When we include I/O-related operands in a compound statement,
         if the first test fails, then the second test should not
         be performed else it may cause a system interrupt exception.
         For example:
         if ((inpRecType == 'A') && (inpPayCode == "OT")) othrs = 10;
         Here, if the record type is not 'A', then we do not want to
         test for an input pay code, and doing so will be nonsense,
         and may likely cause the program to abort execution.

 III. Programming Exercise
      Write a program that allows the user to enter two integers and
      a character. If the character is 'A', add the two integers. If
      the character is 'S', subtract the two integers. If the
      character is 'M', multiply the two integers. Display the results
      of the arithmetic. Use several methods in your program to
      accomplish the separate tasks. Turn in your source code for this
      program.
                                 Return to Top