Professor Mustard Programming
 

Programming 101 - Unit 02 - Flow Control

02z) Review
 
To keep these concepts fresh in your mind, answer the following questions, and then check your answers at the bottom. For best results as a memory tool, answer these questions a few hours after reading the original material.

1) Identify the logic error in this code:

if (age >= 16)
{
  Console.WriteLine("Old enough to drive");
}
Console.WriteLine("Not old enough to drive");



2) Given the following expression...

if (age < 8 || age > 10)

...which of these expressions will produce the same result?

a) if (age <= 7 || age > 10)
b) if (age < 8 && age > 10)
c) if (!(age >= 8) || !(age <= 10))
d) if (age != 8 && age != 9 && age != 10)


3) Will the following loop ever complete?

int num = 0;
while (num != 11)
{
  ++num;
  if (num % 2 == 0)
    ++num;
}



4) How might you calculate the average of all the elements in an int[] array (I'm assuming here that, mathetically speaking, you already know how to calculate an average of several numbers)?


5) When might you use...
a) foreach instead of for?
b) switch instead of if?
c) while instead of for?



Answers
1) The logic error is that the code will output "Not old enough to drive", even if the user enters 16 or above as their age. That second WriteLine() should be in an "else" block, like so:

if (age >= 16)
{
  Console.WriteLine("Old enough to drive");
}
else
{
  Console.WriteLine("Not old enough to drive");
}



2) a), c) and d) mean the same thing.
a) is a match, because age < 8 and age <=7 mean the same thing.
b) isn't a match, because && does the opposite of ||. In fact, this one will never evaluate to true, because a number can't possibly be less than 8 and greater than 10 at the same time.
c) is a match, but it's a stinker to figure that out, because "negative logic" is a difficult thing to follow. Just consider the first half, "!(age >= 8)" (the original first half was "age < 8") . "greater than or equals" does the opposite of "less than"... but the ! operator is used to reverse that, and we end up with a double negative (in effect, we're saying that doesn't do the opposite of the original expression). So the two opposites cancel out, and we're left with the same result that the original expression would produce. The other half works the same way.
d) is a match, because if you look at the original expression, we're really just excluding the numbers 8 through 10... this expression simply mentions them individually, rather than as a range of numbers.


3) Yep. You only get the extra increment if the number is even, so you won't be skipping any odd numbers (such as 11, which is the exit condition).


4) You could calculate the average of all the elements in an int array like so:

int sum = 0;
for(int i = 0; i < numberArray.Length; ++i)
{
  sum += numberArray[i];
}
double average = sum / (double)numberArray.Length;



5) Answers are as follows (and bear in mind, these are general rules of thumb):
a) Both foreach and for are commonly used to iterate through collections. However, foreach is a faster operation than for, because it's specifically designed to iterate through a collection (with a for loop, you're just using an index variable to access the collection's elements, in what happens to be a sequential order; a for loop isn't actually tied into the collection the way foreach is). So, in theory at least, you should always use foreach to iterate through a collection... unless you either need to assign to the elements in the array with "=" (which you can't do in a foreach), or if you need that index variable (which you don't have in a foreach) for something special.
b) Again, switch is faster than if, because it's designed to work in a much more specific way. Generally speaking, you should use switch instead of if whenever you get the chance... namely, when you are testing for a limited number of possible values in a single variable.
c) Use while instead of for when there isn't a counter variable involved in deciding how many times you want to loop... usually, because you're waiting for a very specific set of circumstances, such as specific user input, or a bool variable becoming true.