Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures: Part 1. Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure.

Similar presentations


Presentation on theme: "Control Structures: Part 1. Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure."— Presentation transcript:

1 Control Structures: Part 1

2 Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure Do While / Loop Repetition Structure Do Until / Loop Repetition Structure Assignment Operators Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Outline

3 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 4 (Nested Repetition Structures) Introduction to Windows Application Programming

4 Control Structures Control Structures Selection Structures – If / Then Single-selection structure – If / Then / Else Double-selection structure – Select Case Multiple-selection structure

5 Control Structures Control Structures Repetition Structures – While Loop – Do Loops: Do While / Loop Do / Loop While Do Until / Loop Do / Loop Until – For Loops: For / Next For Each / Next

6 /Selection Structure If / Then Selection Structure It is a single-entry/single-exit structure Example If studentGrade >= 60 Then MsgBox( “ Passed ” ) End If

7 The preceding If/Then selection structure also could be written on a single line as The preceding If/Then selection structure also could be written on a single line as If studentGrade >= 60 Then MsgBox("Passed") If studentGrade >= 60 Then MsgBox("Passed") In the multiple-line format, all statements in the body of the If/Then are executed if the condition is true. In the multiple-line format, all statements in the body of the If/Then are executed if the condition is true. In the single-line format, only the statement after the Then keyword is executed if the condition is true. In the single-line format, only the statement after the Then keyword is executed if the condition is true. Writing the closing End If keywords after a single-line If/Then structure is a syntax error. Writing the closing End If keywords after a single-line If/Then structure is a syntax error. /Selection Structure If / Then Selection Structure

8 The If/Then/Else selection structure allows the programmer to specify that a different action (or sequence of actions) be performed when the condition is true than when the condition is false. If / Then / Else Selection Structure

9 Example If studentGrade >= 60 Then MsgBox( “ Passed ” ) Else MsgBox( “ Failed ” ) End If

10 The VB Editor tries to help you write If statements. When in the code view window, if you type the keyword If and press Enter, the VB editor automatically adds the keywords Then and End If. You can add the Else branch as necessary. The VB editor will try to correct errors. If you type EndIf with no space, the editor will correct this and add the required space. If you type Else with a coding statement on the line, the editor will add a colon between Else and the coding statement – the colon is a statement separator. If / Then / Else Selection Structure

11 Illegal Syntax If HoursDecimal <= 40D Then MsgBox(“ Equals to 40”) Else MsgBox(“ Not Equals to 40”) End If VB Editor Correction with Colon If HoursDecimal <= 40D Then MsgBox(“ Equals to 40”) Else : MsgBox(“ Not Equals to 40”) End If Preferred Syntax If HoursDecimal <= 40D Then MsgBox(“Equals to 40”) Else MsgBox(“ Not Equals to 40”) End If If you type Else with a coding statement on the line, the editor will add a colon between Else and the coding statement If / Then / Else Selection Structure

12 Nested If/Then/Else structures test for multiple conditions by placing If/Then/Else structures inside other If/Then/Else structures. Most Visual Basic programmers prefer to write the nested If/Then/Else structure using the ElseIf keyword. Nested If / Then / Else

13 For example, the following code will print “A” for exam grades greater than or equal to 90, “B” for grades in the range 80–89, “C” for grades in the range 70–79, “D” for grades in the range 60–69 and “F” for all other grades. For example, the following code will print “A” for exam grades greater than or equal to 90, “B” for grades in the range 80–89, “C” for grades in the range 70–79, “D” for grades in the range 60–69 and “F” for all other grades. Nested If/Then/Else If grade >= 90 Then MsgBox(“A”) ElseIf grade >= 80 Then MsgBox(“B”) ElseIf grade >= 70 Then MsgBox(“C”) ElseIf grade >= 60 Then MsgBox(“D”) Else MsgBox(“F”) End IF

14 While Repetition Structure Repetition structure Repetition structure – Allows the programmer to specify that an action should be repeated, depending on the value of a condition

15 Write a program that finds the first power of two larger than 1000? Failure to provide the body of the structure with an action that causes the condition to become false creates an infinite loop

16 Do While/Loop Repetition Structure This structure behaves like the While repetition structure

17 DoWhile.vb Program Output Failure to provide the body of the structure with an action that causes the condition to become false creates an infinite loop Write a program to find the first power of two larger than 1000?

18 Do Until/Loop Repetition Structure Do Until/Loop Repetition Structure Unlike the While and Do While/Loop repetition structures, the Do Until/Loop repetition structure tests a condition for falsity for repetition to continue. Unlike the While and Do While/Loop repetition structures, the Do Until/Loop repetition structure tests a condition for falsity for repetition to continue. Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation test evaluates to false. Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation test evaluates to false.

19 DoUntil.vb Program Output The loop ends when the condition becomes true once again consider a program designed to find the first power of two larger than 1000.

20 Fig. 4.11Assignment operators. Assignment Operators

21 Assignment.vb Program Output Calculates a power of two using the exponentiation assignment operator.

22 Although the symbols =, +=, -=, *=, /=, \=, ^= and &= are operators, we do not include them in operator- precedence tables. Although the symbols =, +=, -=, *=, /=, \=, ^= and &= are operators, we do not include them in operator- precedence tables. When an assignment statement is evaluated, When an assignment statement is evaluated, the expression to the right of the operator is always evaluated first, then assigned to the the expression to the right of the operator is always evaluated first, then assigned to the variable on the left. variable on the left. Unlike Visual Basic’s other operators, the assignment operators can only occur once in a statement. Unlike Visual Basic’s other operators, the assignment operators can only occur once in a statement. Assignment Operators

23 Space Counter Example Property ReadOnly = true

24 Public Class Form1 Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click Dim i, counter As Integer, A As String Dim i, counter As Integer, A As String counter = 0 counter = 0 For i = 0 To txtString.Text.Length - 1 For i = 0 To txtString.Text.Length - 1 A = txtString.Text.Chars(i) A = txtString.Text.Chars(i) If A = " " Then If A = " " Then counter = counter + 1 counter = counter + 1 Else Else txtStringNoSpace.Text = txtStringNoSpace.Text & A txtStringNoSpace.Text = txtStringNoSpace.Text & A End If End If Next i Next i txtSpacesNo.Text = counter txtSpacesNo.Text = counter End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click txtString.Clear() txtSpacesNo.Text = "" txtSpacesNo.Text = "" txtStringNoSpace.Text = "" txtStringNoSpace.Text = "" End Sub End Class Space Counter Example

25 Case Study 1 (Counter-Controlled Repetition) Counter-controlled repetition – Uses counter: Variable that specifies the number of times that a set of statements will execute Example: A class of ten students took a quiz. The grades (integers in the range from 0 to 100) for this quiz are available to you. Determine the class average on the quiz.

26

27 Program Output Enter integer grade: 89 Enter integer grade: 70 Enter integer grade: 73 Enter integer grade: 85 Enter integer grade: 64 Enter integer grade: 92 Enter integer grade: 55 Enter integer grade: 57 Enter integer grade: 93 Enter integer grade: 67 Class average is 74.5

28 Develop a class-averaging program that averages an arbitrary number of grades each time the program is run. In the first class-average example, the number of grades (10) was known in advance. In this example, no indication is given of how many grades are to be input. The program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average? Case Study 2 (Sentinel-Controlled Repetition)

29 One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value or a flag value) to indicate “end of data entry.” The user inputs all grades and then types the sentinel value to indicate that the last grade has been entered. It is crucial to employ a sentinel value that cannot be confused with an acceptable input value. Grades on a quiz are normally nonnegative integers, thus –1 is an acceptable sentinel value for this problem. Case Study 2 (Sentinel-Controlled Repetition)

30 Test division by zero In sentinel-controlled repetition, a value is read before the program reaches the While structure print the value of average in the command window as a fixed- point number.

31 Program Output Enter Integer Grade, -1 to Quit: 97 Enter Integer Grade, -1 to Quit: 88 Enter Integer Grade, -1 to Quit: 72 Enter Integer Grade, -1 to Quit: -1 Class average is 85.67

32 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 examination. 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” 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 of students who failed the exam. 4. If more than 8 students passed the exam, print the message “Raise tuition.” Case Study 3 (Nested-Control Structure)

33 Identifier vbCrLf is the combination of the carriage return and linefeed characters The If / Then / Else structure is a nested control. It is enclosed inside the While.

34 Program Output

35 Case Study 4 (Nested-Repetition Structure) Write a program that draws in the command window a filled square consisting solely of * characters.Write a program that draws in the command window a filled square consisting solely of * characters. The side of the square (i.e., the number of * characters to be printed side by side) should be input by the user and should not exceed 20.The side of the square (i.e., the number of * characters to be printed side by side) should be input by the user and should not exceed 20.

36 Three levels of nesting

37 Program Output

38 – In console applications, execution starts from Main. – In Windows applications, if you want a method like main you must create a method that executes when the form is loaded into memory during program execution. – Like Main, this method is invoked when the program is run. – Double-clicking the form in design view adds a method named FrmASimpleProgram_Load to the class, which is responsible for that Introduction to Windows Application Programming

39 Fig. 4.29Method FrmASimpleProgram_Load containing program code. Introduction to Windows Application Programming

40 Conclusion Normally, statements in a program are executed one after another in the order in which they are written. This is called sequential execution. Normally, statements in a program are executed one after another in the order in which they are written. This is called sequential execution. Various Visual Basic statements enable the programmer to specify that the next statement to be executed might not be the next one in sequence. This is called a transfer of control. Various Visual Basic statements enable the programmer to specify that the next statement to be executed might not be the next one in sequence. This is called a transfer of control. Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures—the sequence structure, the selection structure and the repetition structure. Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures—the sequence structure, the selection structure and the repetition structure.

41 Failure to provide in the body of a While or Do While/Loop structure an action that eventually causes the condition to become false is a logic error. Normally, such a repetition structure never terminates, resulting in an error called an “infinite loop.” Failure to provide the body of a Do Until/Loop structure with an action that eventually causes the condition in the Do Until/Loop to become true creates an infinite loop. Conclusion

42 In Counter-controlled the number of repetitions is known before the loop begins executing. In Counter-controlled the number of repetitions is known before the loop begins executing. In sentinel-controlled repetition, the number of repetitions is not known before the loop begins its execution. In sentinel-controlled repetition, the number of repetitions is not known before the loop begins its execution. Conclusion


Download ppt "Control Structures: Part 1. Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure."

Similar presentations


Ads by Google