Control Structures: Part 1
Outline 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) 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)
Control Structures Selection Structures If/Then If/Then/Else Single-selection structure If/Then/Else Double-selection structure Select Case Multiple-selection structure
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
If/Then Selection Structure It is a single-entry/single-exit structure Example If studentGrade >= 60 Then MsgBox(“Passed”) End If
If/Then Selection Structure The preceding If/Then selection structure also could be written on a single line as 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 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.
If/Then/Else Selection Structure 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 Example If studentGrade >= 60 Then MsgBox(“Passed”) Else MsgBox(“Failed”) End If
If/Then/Else Selection Structure 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 Illegal Syntax If HoursDecimal <= 40D Then MsgBox(“ Equals to 40”) Else MsgBox(“ Not Equals to 40”) End If VB Editor Correction with Colon Else : MsgBox(“ Not Equals to 40”) Preferred Syntax MsgBox(“Equals to 40”) Else MsgBox(“ Not Equals to 40”) If you type Else with a coding statement on the line, the editor will add a colon between Else and the coding statement
Nested If/Then/Else 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 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. 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
While Repetition Structure Allows the programmer to specify that an action should be repeated, depending on the value of a condition
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
Do While/Loop Repetition Structure This structure behaves like the While repetition structure
DoWhile.vb Program Output Write a program to find the first power of two larger than 1000? 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
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. Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation test evaluates to false.
DoUntil.vb Program Output once again consider a program designed to find the first power of two larger than 1000. DoUntil.vb Program Output The loop ends when the condition becomes true
Assignment Operators Fig. 4.11 Assignment operators.
Assignment.vb Program Output Calculates a power of two using the exponentiation assignment operator. Assignment.vb Program Output
Assignment Operators Although the symbols =, +=, -=, *=, /=, \=, ^= and &= are operators, we do not include them in operator-precedence tables. When an assignment statement is evaluated, the expression to the right of the operator is always evaluated first, then assigned to the variable on the left. Unlike Visual Basic’s other operators, the assignment operators can only occur once in a statement.