1: | What are the main parts of an If-Then statement? |
A1: | The main parts of an If statement are the If keyword, the test expression (which must evaluate to either logic True or False ), and the Then keyword. If the outcome of the test expression is logic True , the If statement block following the Then keyword is executed. Otherwise, the If statement block is skipped . |
2: | What are the main parts of an If-Then-Else statement? |
A2: | The main parts of an If statement are the If keyword, the test expression (which must evaluate to either logic True or False ), the Then keyword, and the Else keyword. In the following statement: If TestExpression Then IfStatementBlock() Else ElseStatementBlock() End if if TestExpression is logic True (that is, nonzero), IfStatementBlock() is executed. If TestExpression is logic False , ElseStatementBlock() is executed. |
3: | What is a nested If ? |
A3: | A nested If occurs when one If statement is part of another If statement. In the following example, the first If tests the age of the person, and the nested If checks the sex of the person: If Age > 21 Then If Sex = 0 then MessageBox.Show("This is an adult female") Else MessageBox.Show("This is an adult male") End If Else MessageBox.Show("This is a minor") End If Based on the combined tests, the program displays the appropriate response. |
4: | When should you use a Select Case statement instead of an If statement? |
A4: | You can use the Select Case statement in place of an If statement any time you want to. However, the Select Case is normally used when there are a number of different actions to be taken, depending on the value of the test expression. An If is normally used when the test expression resolves to a specific value, such as this: If Age > 65 Then GiveSeniorDiscount() End If An If-Then-Else statement is often used when there is a specific value and everything else falls into a default category. Here's an example: If Age > 65 Then GiveSeniorDiscount() ' Special Else GiveRegularDiscount() ' Default End If In this situation, you want to give one discount to senior citizens (that is, the special case), but everyone else gets the regular discount (that is, the default case). A Select Case statement is best used where the test expression means that different actions are to be taken over a wider range of values. Here's an example: Select Case NaughtySpeed Case 60 To 70 Fine = 60 + ((NaughtySpeed 60) * 10) Case 71 To 80 Fine = 70 + ((NaughtySpeed 60) * 20) Case 81 To 90 Fine = 80 + ((NaughtySpeed 60) * 30) Case 91 To 100 Fine = 500 + TakeTheirCarAwayFromThem() Case 101 To 714 Fine = 2000 + PutThemInJail() End Select As you can see, the Select Case statement is well suited to situations where a variety of actions need to be taken based on the test condition (in this case, NaughtySpeed ). |
5: | What are enumerations, and what data types can be used with enumerations? |
A5: | Enumerations are named constants that can be used in a program. A carefully selected list of enumerated constants can help make code more readable. The Enum data type is limited to integral data types, and the default starting value in an Enum list is zero. |
6: | Suppose you are writing a program for a university that has freshmen through seniors, plus master's and doctoral students. You know from your preliminary design that there will be a lot of places in the code where knowing the student's class year will be important. How might you classify these students? |
A6: | An Enum list would help in this situation. You might try this: Enum StudentYear Freshmen = 1 Sophomore = 2 Junior = 3 Senior = 4 Masters = 10 Doctoral = 100 End Enum This declaration must appear outside a procedure. Then you could use something like Dim Age, Sex As Integer, ThisStudent As StudentYear Select Case ThisStudent Case ThisStudent.Freshmen ShowThemTheRopes() Case ThisStudent.Sophomore TimeToSelectAMajor() Case ThisStudent.Junior TellThemToTowTheRope() Case ThisStudent.Senior TimeToGetAJob() Case Else GradSchool() End Select In your code, the Case values determine which tasks to perform based on the Enum values. The Enum values make it easier to understand what each Case means. The code would be more difficult to read if the Case statements were simply labeled with numbers . |
7: | In Exercise 6, why do you make the master's and doctoral students' Enum values so much larger than the other students' values? |
A7: | There are two reasons: so you could add up to 90 new master's student classifications without overlapping the doctoral students and so you could do certain tests on the students' numeric values and know something about who they are. Here's an example: Dim StudentValue As Integer, ThisStudent As StudentYear ' Some code that sets the student value to their Enum If StudentValue / ThisStudent.Masters >= 1 Then ThisIsAGradStudent() Else ThisIsAnUnderGrad() End If Given the way the Enum is declared in this example, you could use a Mod 10 operation on a student's year value to determine whether he or she is an undergraduate or a graduate student. That is, a student year value Mod 10 would produce a value of for either type of graduate student (master's or doctoral). Again, the Enum value makes it easier to understand what the code is doing. |
8: | What is the basic idea behind the use of a short-circuit operator? |
A8: | The basic idea behind the use of a short-circuit operator is efficiency. If you have a complex test expression that uses a logical And or Or , the short-circuit operators AndAlso and OrElse can skip the evaluation of the second test expression. Here's an example: If Class = 4 And Grade = "A" Then Graduate() End If In this case, even if Class is not equal to 4 , the code still performs the test on Grade . However, this is inefficient because if Class is not 4 , there's no reason to even perform the test on Grade because the compound expression is already logic False , and there is no way to reach the Graduate() call. If you write this code: If Class = 4 AndAlso Grade = "A" Then Graduate() End If if Class is not equal to 4 , Visual Basic .NET does not even bother to perform the second test. You short-circuit the second test, and this short-circuit saves time. OrElse works in a similar fashion by saying that if the first test is logic True , there's no reason to even perform the second test. |