1 / 26

Controlling Program Flow

Learn about conditional and logical operators, if statements, select case, loops, and operators to manipulate data. Understand precedence, default flow, and usage of different operators in programming. Master control flow in your programs efficiently.

tsmallwood
Download Presentation

Controlling Program Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Controlling Program Flow

  2. Topics • Conditional Operators • Logical Operators • If • Select Case • Loops

  3. Operators Operators manipulate data by combining or computing results.

  4. Math and String Operators • Precedence • Default – Left to Right • ( ) • ^ • * / • MOD • \ • + - • &

  5. 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

  6. 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)

  7. 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 #”)

  8. Logical Operators

  9. Not If Not((varA = varB) And (varC < varD)) then …Statements End if NEGATES a condition Use Sparingly!

  10. And If (varA = varB) And (varC < varD) then …Statements End if If True and True then Condition 1 AND Condition 2

  11. Or If (varA = varB) Or (varC < varD) then …Statements End if If True Or True then Condition 1 AND Condition 2

  12. Xor If (varA = varB) Xor (varC < varD) then …Statements End if If True Xor True then Condition 1 AND Condition 2

  13. Truth Table

  14. 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.

  15. If then • If (condition is true) then do thisEnd if • IF (condition is true) then do this

  16. If then Else If (condition is true) then do this Else do something else End If

  17. 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

  18. Select Case • Simpler, more logical than using several elses Select Case varAge Case Is <5 statements Case 5 statements Case 6 to 11 statements Case 12 to 13, 23 to 46, Is 65 statements Case Else statements End Select

  19. 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.

  20. Nesting Loops For outerCounter = 1 to 10 statements For innerCounter = 1 to 10 statements Next innerCounter statements Next outerCounter

  21. Step value For counter = 0 to 10 step 2 ‘statements Next Counter steps = 0, 2, 4, 6, 8, 10

  22. Step Values: negative For counter = 10 to 0 Step -1 ‘statements Next

  23. Do Loops • Execute WHILE a condition is true, or UNTIL it is true Do While strAnswer=“yes” statements Loop

  24. Do Loops • Do • While • Loop • condition • Do • Until • Loop • condition

  25. Do While conditionstatements Loop Do statements Loop While condition Do Loops Do Until conditionstatements Loop Do statements Loop Until condition

  26. 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

More Related