Section 5.11. Nested Control Statements


5.11. Nested Control Statements

We have seen that control statements can be stacked on top of one another (in sequence). In this case study, we examine the only other structured way control statements can be combined, namely, through the nesting of one control statement within another.

Consider the following problem statement:

A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, 10 of the students who completed this course took the licensing exam. The college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of the 10 students. Next to each name is written a "P" if the student passed the exam and an "F" if the student failed the exam.

Your program should analyze the results of the exam as follows:

  1. Input each exam result (i.e., a "P" or an "F"). Display the message "Enter result" on the screen each time the program requests another exam result.

  2. Count the number of passes and failures.

  3. Display a summary of the exam results, indicating the number of students who passed and the number who failed.

  4. If more than eight students passed the exam, print the message "Raise tuition."

After reading the problem statement carefully, we make the following observations:

  1. The program must process exam results for 10 students. A counter-controlled loop can be used because the number of test results is known in advance.

  2. Each exam result is a stringeither a "P" or an "F." Each time the program reads an exam result, the program must determine whether the input is a "P" or an "F." We test for a "P" in our algorithm. If the input is not a "P," we assume it is an "F."

  3. Two counters store the exam resultsone counts the number of students who passed the exam and the other counts the number of students who failed.

  4. After the program has processed all the exam results, it must determine whether more than eight students passed the exam.

The class that solves the preceding problem is shown in Fig. 5.15, and two sample executions appear in Fig. 5.16.

Figure 5.15. Nested control statements: Examination-results problem.

  1  ' Fig. 5.15: Analysis.vb  2  ' Analysis of examination results.  3  Public Class Analysis  4     ' input and analyze exam results  5     Public Sub ProcessExamResults()  6        ' initializing variables in declarations                          7        Dim passes As Integer = 0 ' number of passes                      8        Dim failures As Integer = 0 ' number of failures                  9        Dim student As Integer = 1 ' student counter                     10        Dim result As String ' one exam result (obtains value from user) 11 12        ' process 10 students using counter-controlled loop 13        While student <= 10 14           Console.Write("Enter result (P = pass, F = fail): ") 15           result = Console.ReadLine() 16 17           ' nested control statement                       18           If result = "P" Then                             19              passes += 1 ' increment number of passes      20           Else                                             21              failures += 1 ' increment number of failures  22           End If                                           23 24           student += 1 ' increment student counter 25        End While 26 27         ' display exam results 28        Console.WriteLine( _ 29            "Passed: " & passes & vbCrLf & "Failed: " & failures) 30 31         ' raise tuition if more than 8 students passed 32         If passes > 8 Then 33            Console.WriteLine("Raise tuition") 34         End If 35     End Sub ' ProcessExamResults 36  End Class ' Analysis 

Figure 5.16. Test program for class Analysis (Fig. 5.15).

 1  ' Fig. 5.16: AnalysisTest.vb 2  ' Test program for class Analysis. 3  Module AnalysisTest 4     Sub Main() 5        Dim application As New Analysis() ' create Analysis object         6        application.ProcessExamResults() ' call method to process results  7     End Sub ' Main 8  End Module ' AnalysisTest 

Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): F Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Passed: 9 Failed: 1 Raise tuition Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): F Enter result (P = pass, F = fail): F Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Enter result (P = pass, F = fail): p Passed: 8 Failed: 2 



Lines 710 of Fig. 5.15 declare the variables that method ProcessExamResults of class Analysis uses to process the examination results. Several of these declarations use Visual Basic's ability to incorporate variable initialization into declarations (passes is initialized to 0, failures is initialized to 0 and student is initialized to 1). Looping programs may require initialization at the beginning of each repetitionsuch reinitialization would normally be performed by assignment statements rather than in declarations.

The While statement (lines 1325) loops 10 times. During each iteration, the loop inputs and processes one exam result. Note that the If...Then...Else statement (lines 1822) for processing each result is nested in the While statement. If the result is "P", the If...Then...Else statement increments passes; otherwise, it assumes the result is "F" and increments failures. Line 24 increments student before the loop condition is tested again at line 13. After 10 values have been input, the loop terminates and lines 2829 display the number of passes and the number of failures. The If...Then statement at lines 3234 determines whether more than eight students passed the exam and, if so, outputs the message "Raise tuition".

AnalysisTest Module That Demonstrates Class Analysis

Module AnalysisTest (Fig. 5.16) creates an Analysis object (line 5) and invokes the object's ProcessExamResults method (line 6) to process a set of exam results entered by the user. Fig. 5.16 shows the input and output from two sample executions of the program. During the first sample execution, the condition at line 32 of method ProcessExamResults in Fig. 5.15 is truemore than eight students passed the exam, so the program outputs a message indicating that the tuition should be raised.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net