Download presentation
Presentation is loading. Please wait.
Published byWillis Claud Barber Modified over 8 years ago
1
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT
2
Copyright © Don Kussee 1410-Ch5 #1032 Programming concepts There are 5 programming concepts –Input InputBox( ), TextBox –Output MsgBox( ), Label –Computing - + - * ^ / \ MOD –Selection If - Then, Select Case –Loop
3
Copyright © Don Kussee 1410-Ch5 #1033 Selection Code Allows for the use of different code -- depending on some condition. Most programs include different paths for changeable conditions Two programming methods are used –If (test condition) Then –Select Case
4
Copyright © Don Kussee 1410-Ch5 #1034 If (test) Then code Else code End If True False VB code If( ) VB code Code before If section Code after If section
5
Copyright © Don Kussee 1410-Ch5 #1035 If two code choices are required Based on one test condition, if test is true then one section of code, if test is false then use a different code section If ( age < 18) Then MsgBox(“Child”) ‘ If test is true Else MsgBox(“Adult”)‘ If test is false End If
6
Copyright © Don Kussee 1410-Ch5 #1036 If (test) Then...Else… End If If (age > 6) Then Beep MsgBox(“Go to school”) ‘TEST TRUE other VB code Else MsgBox( “No school yet”) ‘TEST FALSE other VB code End If
7
Copyright © Don Kussee 1410-Ch5 #1037 Scope of If Then...Else… End If Note that the code is indented to assist the human to read the code (Computer ignores) If (test) Then VB Code ‘ Four space indent more VB code End If ‘ End of the If - Outdent VB Code ‘ Code runs if test is True or False
8
Copyright © Don Kussee 1410-Ch5 #1038 If one code choice is required One test condition, if test true then one section of code, if false then no additional code is run. If ( hours > 40 ) Then ‘ If test is true MsgBox(“overtime = ”, hours - 40) End If VB Code ‘ jumps to here if test is false ‘ Also runs here if true
9
Copyright © Don Kussee 1410-Ch5 #1039 If more than two code choices are required Multiple test conditions, for each test a true runs a section of code, a false leads to the next test condition - Order critical If ( Test1) Then ElseIf ( Test2 ) Then ElseIf ( Test3 ) Then End If
10
Copyright © Don Kussee 1410-Ch5 #10310 If (t) Then…ElseIf(t)…Else... If (age = 6) Then MsgBox( “1 st grade”) ‘ Note indenting ElseIf (age = 7) Then ‘ElseIf one word MsgBox( “2 nd grade”) ElseIf (age = 8) Then MsgBox( “3 rd grade”) Else ‘Only if all tests are false MsgBox( “Not in grades 1, 2 or 3”) End If
11
Copyright © Don Kussee 1410-Ch5 #10311 If(t)Then…ElseIf(t)…End If VB code If() VB code true false Without else
12
Copyright © Don Kussee 1410-Ch5 #10312 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false Without else
13
Copyright © Don Kussee 1410-Ch5 #10313 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false Without else
14
Copyright © Don Kussee 1410-Ch5 #10314 If(t)Then…ElseIf(t)…End If VB code If() VB code true false (Else)
15
Copyright © Don Kussee 1410-Ch5 #10315 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false (Else)
16
Copyright © Don Kussee 1410-Ch5 #10316 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false (Else)
17
Copyright © Don Kussee 1410-Ch5 #10317 Unlimited number of ElseIf (tests) allowed Else is also allow, but not required –runs only if all If(test) & ElseIf (test) are false Code stops when the next ElseIf is hit. Program continues after End If
18
Copyright © Don Kussee 1410-Ch5 #10318 If (test) Then code If( ) VB code True False
19
Copyright © Don Kussee 1410-Ch5 #10319 One choice - only one line of code - Caution If (score >60 ) Then MsgBox( “Pass”) Just one statement, not a block of code Caution - easy to forget & add a line that won’t be subject to the if (test) Recommend always include an End If to indicate the end of the If - End If If (score >99) Then MsgBox( “Pass”) End If
20
Copyright © Don Kussee 1410-Ch5 #10320 Comparison or Relational operators - used for tests = equal to > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to
21
Copyright © Don Kussee 1410-Ch5 #10321 Numerical comparisons Based upon the algebraic values of the numbers The sign as well as the magnitude is important. ? 5 > 6 ? 5 > -6 ? -5 > -6
22
If (t) Then code Else code End If True False VB code If( ) VB code
23
Copyright © Don Kussee 1410-Ch5 #10323 Nested or Embedded If( ) Within each If (test is true) VB code –Additional If … Then can be built –Different from ElseIf structure –Different from Combined test conditions (And) If (In school district = yes) If (age = 6) If (vaccinations = done) If ( class size < max class size)
24
Nested If Then Else Statements
25
Copyright © Don Kussee 1410-Ch5 #10325 Nested If Then Else Statements If a given condition must be tested only when a previous condition has been tested true & 1. alternative actions are required for one or more of the conditions then nest. 2. one or more statements are to be executed before or after the second If-Then-Else logic structure, then a nested If-Then-Else is required.
26
Nested If Then Else Statements
33
Copyright © Don Kussee 1410-Ch5 #10333 Nested If structure If (test condition 1) Then Statement code block 1 ElseIf (test condition 2) Then If( test condition 3) Then Statement code block 2 If (test condition 4) Then Statement code block 4 If (test condition 5) Then Statement code block 5 ElseIf (test condition 6) Then Statement code block 6 End If ElseIf (test condition 7) Then Statement code block 7 End If
34
Copyright © Don Kussee 1410-Ch5 #10334 Nested If syntax Indent the nested ifs to make them easy to read and understand Caution - the computer ignores indenting Else, ElseIf, End If - belong to the next upward unattached If.
35
Copyright © Don Kussee 1410-Ch5 #10335 Nested If structure If (test condition 1) Then Statement code block 1 ElseIf (test condition 2) Then If( test condition 3) Statement code block 2 If (test condition 4) Statement code block 4 If (test condition 5) Statement code block 5 ElseIf (test condition 6) Then Statement code block 6 End If ElseIf (test condition 7) Then Statement code block 7 End If
36
Copyright © Don Kussee 1410-Ch5 #10336 Examples of If (t)…ElseIf(t) Grades:>90 A, >80 B, >70 C, >60 D, <60 E Speeding Fines:> 30 miles over limit $500, >20 $200, >10 $100, <10 warning Commission:>$100.2%,>$200.3,>500.4 Bussing: 1 Buss, in between… bussed when snow Or < 20 o F Golf: Walk 9 holes Or >80 o F Or <60 o F Or rain threat Or client Or feels rich Or feels tired Or hurried
37
Copyright © Don Kussee 1410-Ch5 #10337 Option Button (Radio Button) Control opt Input control, allows one of several choices Programmer defines choices One choice is required More than one choice not allowed Unlimited number of buttons allowed Mouse or Tab key will move to next button Like the old push-button car radios
38
Copyright © Don Kussee 1410-Ch5 #10338 Option Button Properties Caption - Text appears by button Style - 0 = Text, 1 = Text & Graphic Value = True or False Option buttons are group controls - All option buttons effect each other, click on one - un-clicks all other buttons - Usually an array of Option Buttons
39
Copyright © Don Kussee 1410-Ch5 #10339 Frame Control fra Separate controls into groups of Objects. Control that groups Option Buttons so that interactions are isolated to that group Default button placement is on the form Frame must be placed first, then the buttons placed on the frame. Buttons can not be moved onto a frame. Cut & paste works. Events are seldom used
40
Copyright © Don Kussee 1410-Ch5 #10340 Frame Control Properties Caption - Text at top left of frame. Enabled - Frames & buttons are grayed Visible - Frame & buttons not displayed Layers of Form & controls Container control
41
Copyright © Don Kussee 1410-Ch5 #10341 Two frames to Group Option Buttons
42
Copyright © Don Kussee 1410-Ch5 #10342 Check Box Control chk Input control, allows many choices Programmer defines choices No choice is required More than one choice is allowed Unlimited number of buttons allowed Mouse or Tab key will move to next button
43
Copyright © Don Kussee 1410-Ch5 #10343 Check Box Properties Caption - Text appears by button Style - 0 = Text, 1 = Text & Graphic Value = True or False or Disabled Check box are not group controls Often an array of Option Buttons
44
Copyright © Don Kussee 1410-Ch5 #10344 Income tax program TaskObjectEvent Task Object Event Income txt Number of dependents chk Use programs tax computation opt Click Compute about owed cmd Click Amount of tax prepayment msb OK Display difference lbl
45
Copyright © Don Kussee 1410-Ch5 #10345 Select Case Statement One test with multiple possible outputs Similar to If … ElseIf … ElseIf … End If Unlimited number of outcomes Case Else available if all others False Selected code runs until next Case reached Program continues after the End Select Numeric or String can be test expression
46
Copyright © Don Kussee 1410-Ch5 #10346 Select Case syntax Select Case test_expression ‘note indenting Case possible_result 1 Code statements for condition 1 Case possible_result 2 Code statements for condition 2 Case Else Code statements for Else condition End Select
47
Copyright © Don Kussee 1410-Ch5 #10347 Select Case possible results Case Is = “Feb” ‘exact string match Case Is = 4 ‘ exact number match Case “Jan”, “Mar”,”May”,”Jul”,”Aug” Case Is 10 to 21, 22, “Sop” ‘range of values Case Is >= 23 ‘comparison operator Case Is <> 6 ‘comparison operator Case Else - only if all other tests are False
48
Copyright © Don Kussee 1410-Ch5 #10348 Select Case Vs If ElseIf One Value tested Many result conditions Easier to read Easier to write Order not important Different values tested Few result conditions Harder to read Harder to write More prone to contain errors Order important
49
Copyright © Don Kussee 1410-Ch5 #10349 Select Case Selected code –Runs until the next Case Statement –Can include multiple code statements –Can include additional Select Case structures –Can include If… ElseIf … End If code Statement order not critical in Select Case Statement order critical in If…ElseIf…End
50
Copyright © Don Kussee 1410-Ch5 #10350 Exit Sub Statement Allows the Sub-routine to be exited early. Considered poor programming (subs should have only one entrance and one exit point) Often connected to error conditions, with some correction ability (MsgBox ( )) or the ability to cancel the program.
51
Copyright © Don Kussee 1410-Ch5 #10351 Chapter 5 keywords Check Box Control Option button controls Condition Outcome Statement block Decision tree Frame control If…Else… End If If…ElseIf… End If Select Case Nested/Embedded If’s Exit
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.