Control Structures Part 1 Outline Introduction Control Structures

  • Slides: 22
Download presentation
Control Structures: Part 1

Control Structures: Part 1

Outline Introduction Control Structures If/Then Selection Structure If/Then/Else Selection Structure While Repetition Structure Do

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 • Single-selection structure – If/Then/Else • Double-selection

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

Control Structures • Repetition Structures – While Loop – Do Loops: • • Do

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 student. Grade

If/Then Selection Structure • It is a single-entry/single-exit structure • Example If student. Grade >= 60 Then Msg. Box(“Passed”) End If

If/Then Selection Structure • The preceding If/Then selection structure also could be written on

If/Then Selection Structure • The preceding If/Then selection structure also could be written on a single line as If student. Grade >= 60 Then Msg. Box("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

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 student. Grade >= 60 Then Msg. Box(“Passed”) Else

If/Then/Else Selection Structure • Example If student. Grade >= 60 Then Msg. Box(“Passed”) Else Msg. Box(“Failed”) End If

If/Then/Else Selection Structure • The VB Editor tries to help you write If statements.

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 End. If 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 Hours. Decimal <= 40 D Then Msg.

If/Then/Else Selection Structure • Illegal Syntax If Hours. Decimal <= 40 D Then Msg. Box(“ Equals to 40”) Else Msg. Box(“ Not Equals to 40”) End If • VB Editor Correction with Colon If Hours. Decimal <= 40 D Then Msg. Box(“ Equals to 40”) Else : Msg. Box(“ Not Equals to 40”) End If • Preferred Syntax If Hours. Decimal <= 40 D Then Msg. Box(“Equals to 40”) Else Msg. Box(“ 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

Nested If/Then/Else • Nested If/Then/Else structures test for multiple conditions by placing If/Then/Else structures

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 Else. If keyword.

Nested If/Then/Else For example, the following code will print “A” for exam grades greater

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 Msg. Box(“A”) Else. If grade >= 80 Then Msg. Box(“B”) Else. If grade >= 70 Then Msg. Box(“C”) Else. If grade >= 60 Then Msg. Box(“D”) Else Msg. Box(“F”) End IF

While Repetition Structure • Repetition structure – Allows the programmer to specify that an

While Repetition Structure • 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

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

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

Write a program to find the first power of two larger than 1000? Failure

Write a program to find 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 Until/Loop Repetition Structure • Unlike the While and Do While/Loop repetition structures, the

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.

once again consider a program designed to find the first power of two larger

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

Assignment Operators Fig. 4. 11 Assignment operators.

Assignment Operators Fig. 4. 11 Assignment operators.

Calculates a power of two using the exponentiation assignment operator.

Calculates a power of two using the exponentiation assignment operator.

Assignment Operators • Although the symbols =, +=, -=, *=, /=, =, ^= and

Assignment Operators • Although the symbols =, +=, -=, *=, /=, =, ^= and &= are operators, we do not include them in operatorprecedence 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.