Agenda & Readng COMPSCI 8 SC Applcatons Programmng Programmng Fundamentals Control Flow Agenda: Decsonmakng statements: Smple If, Ifelse, nested felse, Select Case s Whle, DoWhle/Untl, For, For Each, Nested s Others Wth Usng Ext Recommended Readng: Control Flow n Vsual Basc http://msdn.mcrosoft.com/enus/lbrary/ca8tdhcs(vs.8).aspx Advanced Programmng Usng Vsual Basc.NET Appendx B: Vsual Basc Revew of Introductory of VB.NET Concepts Mcrosoft Vsual Basc 5 Step by Step Chapter 6 and 7 HandsOn Lab: Lab6 COMPSCI 8 7 6 VB DecsonMakng Statements DecsonMakng statements evaluate condtons and execute statements based on that evaluaton VB.NET ncludes two decsonmakng statements: If statement Evaluates an expresson Executes one or more statements f expresson s Can execute another statement or group of statements f expresson s Select case statement Evaluates a varable for multple values Executes a statement or group of statements, dependng on contents of the varable beng evaluated Condton True Flow chart of a typcal decson structure Condtonal COMPSCI 8 7 6 VB Code Smple If Smple If Evaluates an expresson Executes one or more statements f expresson s If condton Then If sales > 5 Then getsbonus = True commssonrate =. daysoff = daysoff + Note: You can use the snglelne form for short, smple tests. In the snglelne form. If > Then = + It s possble to have multple statements executed as the result of an If...Then decson. All statements must be on the same lne and be separated by colons. If > Then = + : = + : k = k + COMPSCI 8 7 6 VB etc.
Ifelse Ifelse Evaluates an expresson Executes one or more statements f expresson s Executes a second statement or set of statements f expresson s If condton Then If sales > 5 Then getsbonus = True getsbonus = etc. Nested felse Nested felse Ifelse or f statement can be used as a subpart of another felse or f statement. The else clause matches the most recent f clause n the same block. Note: nested statement can be very trcky to code. If ( > ) Then If ( > k) Then Console.WrteLne("A") Console.WrteLne("B") If ( > ) Then If ( > k) Then Console.WrteLne("A") Console.WrteLne("B") If sales > 5 Then getsbonus = True getsbonus = COMPSCI 8 7 6 VB 5 COMPSCI 8 7 6 VB 6 If condton Then Statement(s) If condton Then Select Case Statements(s)... Acts lke a multpleway f statement Transfers control to one of several statements, dependng on the value of an expresson testexpresson Must be an elementary data types, such as Boolean, Integer, Strng expressonlst Multple expresson clauses are separated by commas. Each clause can take one of the followng forms: expresson To expresson [ Is ] comparsonoperator expresson expresson Select case Select Case number Case To 5 Case 6, 7, 8 Case Is > 8 Case End Select Select Case testexpresson Case expressonlst statement... Case statementn End Select COMPSCI 8 7 6 VB 7 C C C True True True Statement(s) Statement(s) Statement(s) Wrtng s s Repeated executon of one or more statements untl a termnatng condton occurs Types of loops: whle Do Whle/Untl For For Each (cover n Arrays) COMPSCI 8 7 6 VB 8
Whle The Boolean expresson s checked before the loop body s executed When, the loop body s not executed at all Before the executon of each followng teraton of the loop body, the Boolean expresson s checked agan If, the loop body s executed agan If, the loop statement ends Whle condton End Whle Counters Dm counter As Integer = Whle counter < counter += End Whle Varables called counters are frequently used to control loops Counters are nvarably ntalzed before the loop begns (e.g. Dm count As Integer = ) They are also usually modfed wthn the body of the loop (e.g. count += ) The counter n the body of the loop must eventually make the test expresson Otherwse, the loop wll contnuously loop forever called an nfnte loop Dm counter As Integer = Whle counter < counter = End Whle COMPSCI 8 7 6 VB 9 COMPSCI 8 7 6 VB Do Whle The Do... constructon allows you to test a condton at ether the begnnng or the end of a loop structure. You can also specfy whether to repeat the loop whle the condton remans True or untl t becomes True. Do Whle loop executes whle the expresson s TRUE Do Untl loop executes untl the expresson s FALSE Do whle Untl condton Pretest Vs Posttest s The precedng Do Whle loops were wrtten n ther pretest syntax The expresson s always tested before the body of the loop s executed Posttest loop tests the termnatng condton at the end of the loop The nsde the body must be done once rrespectve of the expresson used Do whle Untl condton COMPSCI 8 7 6 VB COMPSCI 8 7 6 VB
For For counter ntalzaton and ncrementng code as a part of the For statement Uses pretest logc t evaluates the termnatng expresson at the begnnng of the loop Step ndcates the ncrement for count at the end of each teraton; t s optonal and defaults to f not specfed The step value can be negatve, n whch case the loop count downwards For CounterVarable = StartValue To EndValue [Step step] [CounterVarable] Incr counter Intalze the control varable Examples 'case = Do Whle ( < ) = + 'case = Do Whle ( < ) = + 'case = Do = + Whle ( < ) Case 5 'case For = To 'case 5 = Do Whle ( < ) Evaluatng the condton tmes Infnte Executng the body tmes Infnte Output COMPSCI 8 7 6 VB COMPSCI 8 7 6 VB When to use the Do Whle Use the Do Whle loop when you wsh the loop to repeat as long as the test expresson s Do Untl Use the Do Untl loop when you wsh the loop to repeat untl the test expresson s For The For loop s prmarly used when the number of requred teratons s known Posttest loops are deal when you always want the loop to terate at least once Nested s can be nested It Can be constructed usng any combnaton of Do Whle, Do Untl, or For loops When nested, the nner loop terates from begnnng to the end for each sngle teraton of the outer loop There s no lmt n Java to how many levels you can nest loops. It s usually not more than three levels. Example Multplcaton table: ROW =, COLUMN = For = To ROW For = To COLUMN product = If (product < ) Then Console.Wrte(" " & product) Console.Wrte(" " & product) 5... 5 6 8 6 9 output 6 8 6 9 COMPSCI 8 7 6 VB 5 COMPSCI 8 7 6 VB 6
Nested s Example ROW= For = To ROW For = To ROW Console.Wrte("") Outer condton <=... nner condton <= <= <= <= <= output Nested s Example ROW= For = To ROW For = To Console.Wrte("") Outer condton <= < <=... nner condton <= <= <= <= <= output COMPSCI 8 7 6 VB 7 COMPSCI 8 7 6 VB 8 Usng The usng statement establshes a statement block wthn whch you make use of a resource. You can acqure the resource wth the Usng statement. When you ext the Usng block, Vsual Basc automatcally dsposes of the resource so that t s avalable for other code to use. Usng { resourcelst resourceexpresson } statements End Usng Usng nf As New System.Drawng.Font("Aral",.F, _ System.Drawng.FontStyle.Bold) txtsrc.font = nf txtsrc.text = "Ths s pont Aral bold" End Usng Wth End Wth The wth End wth statement allows you to specfy an obect reference once and then run a seres of statements that access ts members. Ths can smplfy your code and mprove performance Wth obect End Wth Wth txtsrc.heght =.Text = "Hello, World".ForeColor = System.Drawng.Color.Green.Font = New System.Drawng.Font(.Font, System.Drawng.FontStyle.Bold) End Wth COMPSCI 8 7 6 VB 9 COMPSCI 8 7 6 VB
Ext Ext (con t) outer nner + outp ut You can use the Ext Statement to ext drectly from a control structure Ext Select Ext Whle Ext Do Ext For Snce these overrde the normal loop termnaton mechansm, they should be used sparngly For = To If ( Mod ) = Then Ext For It s used to termnate the nnermost control structure Const row As Integer = For As Integer = To row For As Integer = To row If ( + ) >= row Then Ext For Console.Wrte("") < < < < < < < = break = break COMPSCI 8 7 6 VB COMPSCI 8 7 6 VB