1 / 46

Chapter 5: Decisions and Operators in Visual Basic

Learn about relational and logical operators, if blocks, select case blocks, conditions, and how to use them in Visual Basic programming.

ghindman
Download Presentation

Chapter 5: Decisions and Operators in Visual Basic

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

  2. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks

  3. Condition • A condition is a Boolean expression that can be either true or false • Conditions can be formed by using the six Relational operatorsand the three Logical operators

  4. Relational Operators

  5. Examples 1<=1 True 1 < 1 False 2 < 5 True 3 <> 3 False 0 < 3.5 True 5 <= 3 False

  6. Examples • Determine whether each of the following conditions is true or false • assume that: • a = 4, b = 3, c = “hello” , d = “bye” • (a+b) < 2 * a True • (Len(c) – b) = (a / 2) True

  7. Using Relational Operators on Strings • Computers use a special coding system to compare character strings called ANSI • With ANSI each character has a number associated with it, this number is called ANSI value of the character. • The ANSI values of characters are given in following table. (Appendix A) • You do not need to memorize the table • Just know the numbers, small and capital letters A= 65 a=97 sp=32 0=48

  8. 8

  9. Using Relational Operators on Strings • The string str1 is said to be less than the string str2 if str1 precedes str2alphabetically when using the ANSI (or ASCII) table • Two strings are compared working from left to right, character by character, to determine which one should precede the other.

  10. Example of Comparing Character Strings “Hope” < “Hopeful” “Chase” < “Chaz” True “ Cat” < “Cat” True “Pay” < “Pay “ True “Jones” <> “James” True True

  11. Example… • Determine whether the following condition is true or false • (assume that c = “hello” and d = “bye”) • c < (“good” & d) False

  12. Logical Operators • The result of a logical operator is also True or False • The three Logical Operators are: Not And Or

  13. Not • Not: Negates a single expression • Example: Suppose answ = “Y” Not (answ = “y”) is True

  14. And • Takes two expressions, returns True only if both expressions are evaluated as being true • Example: Suppose answ = “Y” • (answ = “Y”) And (answ = “y”) is False

  15. Or • Takes two expressions, returns true if either one of the expressions is evaluated as being true. • Example: Suppose answ = “Y” • (answ = “Y”) Or (answ = “y”) is True

  16. Note: • Not ( X And Y ) = Not X ORNot Y • Not ( X Or Y ) = Not X And Not Y Chapter 5 - Visual Basic Schneider 16

  17. Precedence • First, all Arithmetic operations are carried out • Then all expressions involving >, <, and = are evaluated to true or false • The logical operations are next applied in the order Not, then And, and Finally Or.

  18. Examples…

  19. Evaluate the following to True or False Assume a=4, b=3 • Print a*3-2=5-b False • Print a=b+3<=a+b True • Print not (a<2) or b<4 = a+b True • Print Not(a<b)=(a>=b+2) False Be careful about the priorities

  20. Notes • A Condition such as 2<n<5 (not a syntax error) but should never be used, because visual basic will not evaluate it as intended. • The correct condition is (2<n) And (n<5) • The following conditions are equivalent: • a<>b Not (a=b) • a>b Not (a<=b) • Not (n<m) (n>=m)

  21. Types of Decision Structures • If Block Statement • Single alternative: If...Then • Compound alternative: If Then...Else • Select Case Statement

  22. Single Alternative Decision • An action is taken if the condition is true, otherwise the control goes to the next statement. • Syntax:If condition ThenactionEnd If • If condition is true, action is executed. Otherwise, action is skipped

  23. Example

  24. Example

  25. Compound Alternative Decision • Syntax IfconditionThen action1 Else action 2 End If

  26. Example

  27. Example

  28. Example

  29. Example If cond1 Then If cond2 Then action(s) End If End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block

  30. Compound Alternative Decision Syntax Ifcondition1Then action1 ElseIfcondition2Then action 2 ElseIfcondittion3 Then action3 Else action4 End If This block statement searches for the first True condition, carries out its action, and then skips to the statement following end if. If none of the conditions is true, then else’s action is carried out. In general, an IF block can contain any number of ElseIf clauses.

  31. Example

  32. Single Line If Statement Syntax IfconditionThenaction There is no End If If the condition is true, the action will be executed If the condition is false, the (else) action will be taken

  33. Example

  34. Example

  35. The Select Case Block Similar to If statement Used instead of compound If statements Action is selected from a list of alternatives Avoids confusion of deeply nested If blocks

  36. Select Case Block (Syntax) Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select

  37. Example

  38. Example

  39. Example

  40. Example

  41. Example

  42. Example

  43. what is the output of the following code a = 3 b = 4 Select Case a = b Case 3 Print "A" Case 4 Print "B" Case 1 Print "C" Case 0 Print "D" End Select Answer: D

  44. what is the output of the following code a = "hello" Select Case a Case "apple" To "orange" Print "A" Case "Hello" Print "B" Case Is <= "z" Print "C" Case "hello" Print "D" End Select Answer: A

  45. References Chapter 5 - Visual Basic Schneider

More Related