FAQ CS1 Week 4

Muddiest Points from Lecture Material

  1. How do you know what type of control structure to use?

    It really depends on what you need to do. If you need to do one thing after another, you will use a sequential structure. If you need to do different things based on a decision, you will use a selection or conditional structure. Next week we will add another structure as well that helps us to repeat things. Implementation "patterns" can help us understand what's required in a particular situation.

  2. Are these if statements equivalent?
    if ( y == 8 )
    if ( x == 5 )
    System.out.println( "1" );
    else
    System.out.println( "2" );
    System.out.println( "3" );
    System.out.println( "4" );
    and
    if ( y == 8 && x == 5 )
    System.out.println( "1" );
    else
    System.out.println( "2" );
    System.out.println( "3" );
    System.out.println( "4" );

    No, try tracing what happens if y is 7 and x is 4. (For the first if, the output is 3 followed by 4 on the next line. In the second, it is 2 and 3 and 4 all on separate lines!) This is a good example of why both the use of indentation and the use of {}'s are helpful to those of us humans trying to read the code. If written according to our style standards, the two pieces of code would be written as follows:

    if ( y == 8 ) {
       if ( x == 5 ) {
          System.out.println( "1" );
       } else {
          System.out.println( "2" );
       }
    System.out.println( "3" );
    System.out.println( "4" );
    and
    if ( y == 8 && x == 5 ) {
       System.out.println( "1" );
    } else {
       System.out.println( "2" );
    }
    System.out.println( "3" );
    System.out.println( "4" );

  3. I understand that you have to use to "==" in an if statement, but what exactly does "=" in an if statement compare?

    It doesn't compare anything, it's an assignment. Unlike C or C++, Java requires that the expression in an "if" statement be a boolean_expression. It will complain if we try to compile an if statement that incorrectly uses a = instead of ==, UNLESS the assignment is to a boolean variable. For example, see code/CompareWrong.java. If you download it and compile it, you will receive the following error message:

    Compare.java:34: incompatible types
    found : int
    required: boolean
    if ( x = y ) {
          ^
    1 error

  4. Are switch and if the same thing?

    No, but they are different constructs for making selections of the path your program is to follow. And, you can do many of the same things with each. They differ substantially though in syntax and symantics!

  5. I am having trouble understanding switch statements. Can the switch statement can work on multiple cases? If so, for example if we want the same thing to happen for two cases, does the switch statement finish when it first executes a break statement or does it go past the first break statement and continue executing until it reaches the second break statement?

    Yes, the switch statement can work on multiple cases. In ALL cases it stops executing when it reaches the first break statement after the matching case.

  6. I don't understand how to convert between if and switch statements. Please help!


Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
September 30, 2004
http://www.cs.rit.edu/~ncs/Courses/cs1/Muddy4.shtml