IWA-HWG Perl Course. Week 2 Quiz

  1. What statement complements the "if" conditional by allowing it do something when the condition is not met?

    The else is an extension of the if control that provides a block for statements to execute when the if's condition evaluates to false.

  2. What loop structure continually executes a program block until it turns false?

    The while control involves a condition and a block of statements. The condition is tested and when found to be true the statements within the block are executed. Once the statements have been executed, the condition is again evaluated starting another iteration of the repetitive behavior. When the condition evaluates to false the block of statements is skipped and one next executes the statement following the while's block.

  3. When using the "elsif" construct, do you also have to use the "else"?

    No. When used with if and elsif the else provides what might be called the "default case," a set of statements to execute when the if and elsif conditions all evaluated to false. Thus, if the program's logic does not require anything to be done in this circumstance, then the else is not needed. (That being said, it can be useful to have a default case even if you believe you have accounted for all of the cases. A good example might be to include a print statement especially in the debugging stage of a program.)

  4. What is the purpose of the naked block control structure?

    It can be used just to provide a separation between a set of instructions with a well-defined purpose/function and serve as an aid to someone maintaining the code. If the problem is with that particular "function" the person knows to focus attention there, and if the problem is not with that "function" the person knows to leave it alone. A naked block also provides a notion of scope, so that if one declared a so-called "lexical variable" using the keyword my is confined to that block and would not be confused with a variable with the same name in another part of the program. (Learning Perl, pp. 138-9)

  5. Incrementing and decrementing a variable by 1 as with a counter are parts of a typical looping structure. Two methods have been demonstrated in the last two weeks, show examples of each.

    One version of incrementing a variable is the following statement
            $i = $i + 1;
        
    A shortcut version of the same incrementation is
            $i++;
        
    There are two variations on the shortcut (the post-increment and the pre-increment) that are different when used in conjunction with an assignment as shown in the example below.
    Increment Screen Capture
    Replace all of the +'s above with -'s and you have decrementing instead of incrementing.

  6. What operator immediately ends the execution of a loop?

    The last statement takes one out of the loop -- or more particularly out of the innermost loop that the last statment was in. (It is the equivalent of the break statement in C/C++.)

  7. What is the binary assignment operator used for?

    A binary operator takes two pieces of data and combines them in some way. An example is addition, such as
            $balance + $deposit;
        
    An assignment operator evaluates an expression on the right-hand-side of an equal sign, it then makes the variable on the left-hand-side equal to the value obtained on the right-hand-side.
            $newbalance = $balance + $deposit;
        
    It often turns out that the same variable is used on the left-hand-side and right-hand-side of the assignment operator.
            $balance = $balance + $deposit;
        
    where one is using the old value of a variable to determine a new value for the same variable. This situation occurs so frequently, there is a shortcut version of it known as a "binary assignment operator", such as
            $balance += $deposit;
        
    Most of the binary opertors have a corresponding binary assignment operator version. (Learning Perl, p. 27)