Controlling Program Flow
Topics Conditional Operators Logical Operators If Select Case Loops
Operators manipulate data by combining or computing results.
Math and String Operators Precedence Default – Left to Right ( ) ^ * / MOD \ + - &
6 Conditional Operators = Equal to <> Not equal (less than or greater than) < Less than > Greater than >= Greater than or equal to <= Less than or equal to Like Is
You can compare strings “S” < “s” “Kev” < “Kevin” IMPORTANT: The computer ultimately gives comparisons a TRUE or FALSE answer. Console.write (1<2) Console.write (1=2)
Like Operator Used for seeing if strings are similar Uses Wildcards: * ? # [characters] [!not these characters] Console.WriteLine (“Kevin” like “Ke*”) Console.WriteLine (“Beet” like “B??t”) Console.WriteLine (“Employee 7” like “Employee #”)
Logical Operators
Not …Statements If Not((varA = varB) And (varC < varD)) then End if NEGATES a condition Use Sparingly!
And …Statements If (varA = varB) And (varC < varD) then End if If True and True then Condition 1 AND Condition 2
Or …Statements If (varA = varB) Or (varC < varD) then End if If True Or True then Condition 1 AND Condition 2
Xor …Statements If (varA = varB) Xor (varC < varD) then End if If True Xor True then Condition 1 AND Condition 2
Truth Table Condition Result AND TRUE FALSE OR XOR
Such a simple device… Computers only know: 1 or 0 ON or OFF Yes or No True or False We combine these choices to set the course of our computer program.
If then If (condition is true) then do this End if
If then Else If (condition is true) then do this Else do something else End If
If then ElseIf If (condition is true) then do this ElseIf (condition is true) then do something else Else do yet something else End If
Select Case Simpler, more logical than using several elses Select Case varAge Case Is <5 statements Case 5 Case 6 to 11 Case 12 to 13, 23 to 46, Is 65 Case Else End Select
For Next Loop Used for counting through a certain number of iterations (geek-speak for times). For Counter As Integer = 1 to 10 ‘statements Next You can dimension variables in the loop, as above.
Nesting Loops For outerCounter = 1 to 10 statements For innerCounter = 1 to 10 Next innerCounter Next outerCounter
Step value For counter = 0 to 10 step 2 ‘statements Next Counter steps = 0, 2, 4, 6, 8, 10
Step Values: negative For counter = 10 to 0 Step -1 ‘statements Next
Do Loops Execute WHILE a condition is true, or UNTIL it is true Do While strAnswer=“yes” statements Loop
Do Loops Do While Loop condition Until
Do Loops Do While condition statements Loop Do statements Loop While condition Do Until condition statements Loop Do statements Loop Until condition
An early exit… Exit Sub Exit Function Exit For Exit Do End (ends program) Use sparingly - It is most clear to have one exit for a program segment