Thursday, July 21, 2011

Please help me with the AND and OR Logic please?

Lab 15 AND and OR Logic USING AND LOGIC When you write Visual Basic programs, you can use the AND operator (And) to make multiple comparisons in a single decision statement. Remember when using AND logic that all expressions must evaluate to true for the entire expression to be true. The Visual Basic code that follows illustrates a decision statement that uses the AND operator (And) to implement AND logic: Dim medicalPlan As String = "Y" Dim dentalPlan As String = "Y" If medicalPlan = "Y" And dentalPlan = "Y" Then System.Console.WriteLine("Employee has medical insurance" & " and also has dental insurance.") Else System.Console.WriteLine("Employee may have medical insurance " & "or may have dental insurance, but does " & "not have both medical and dental " & "insurance.") End If In this example, the variables named medicalPlan and dentalPlan have both been initialized to the string constant "Y". When the expression medicalPlan = " Y" is evaluated, the result is true. When the expression dentalPlan = "Y" is evaluated, the result is also true. Because both expressions evaluate to true, the entire expression medicalPlan = " Y" And dentalPlan = "Y" evaluates to true. Because the entire expression is true, the output generated is "Employee has medical insurance and also has dental insurance. If you initialize either of the variables medicalPlan or dentalPlan with a value other than "Y", then the expression medicalPlan = "Y" And dentalPlan = "Y" evaluates to false, and the output generated is "Employee may have medical insurance or may have dental insurance, but does not have both medical and dental insurance." USING OR LOGIC You can use OR logic when you want to make multiple comparisons in a single decision statement. Of course, you must remember when using OR logic that only one expression must evaluate to true for the entire expression to be true. The Visual Basic code that follows illustrates a decision statement that uses the OR operator (Or) to implement OR logic: Dim medicalPlan As String = "Y" Dim dentalPlan As String = "N" If medicalPlan = "Y" Or dentalPlan = "Y" System.Console.WriteLine("Employee has medical insurance" & " or dental insurance or both ") Else System.Console.WriteLine("Employee does not have medical" & " insurance and also does not have dental insurance.") End If In this example, the variable named medicalPlan is initialized to the string constant "Y", and the variable named dentalPlan is initialized to the string constant "N". When the expression medicalPlan = "Y" is evaluated, the result is true. When the expression dentalPlan = "Y" is evaluated, the result is false. The expression medicalPlan = "Y" Or dentalPlan = "Y" evaluates to true because when using OR logic, only one of the expressions must evaluate to true for the entire expression to be true. Because the entire expression is true, the output generated is "Employee has medical insurance or dental insurance or both." If you initialize both of the variables medicalPlan and dentalPlan to the string constant "N", then the expression medicalPlan = "Y" Or dentalPlan = "Y" evaluates to false, and the output generated is "Employee does not have medical insurance and also does not have dental insurance." In this exercise, you use what you have learned about OR logic to study a complete Visual Basic program that uses OR logic in a decision statement. This program was written for a marketing research firm that wants to determine if a customer prefers Coke or Pepsi over some other drink. Take a few minutes to study the code that follows, and then answer Questions 1-4. 'CokeOrPepsi.vb - This program determines if a customer ' prefers to 'drink Coke or Pepsi or some other drink. Module CokeOrPepsi Sub Main() Dim customerName As String ' Customer's name Dim drink As String = "" ' Customer's favorite drink customerName = InputBox$("Enter customer's name: ") drink = InputBox$("Enter customer's drink preference: ") If drink = "Coke" Or drink = "Pepsi" Then System.Console.WriteLine("Customer Name: " & customerName) System.Console.WriteLine("Drink: " & drink) Else System.Console.WriteLine(customerName & " does not prefer Coke or Pepsi.") End If End Sub End Module 1. What is the exact output when this program executes if the customer's name is Sally Preston and the drink is Coke? Answer: Customer Name: Sally Preston Drink: Coke 2. What is the exact output when this program executes if the customer's name is Sally Preston and the drink is Pepsi? Answer: Customer Name: Sally Preston Drink: Pepsi 3. What is the exact output from this program when If drink = "Coke" Or drink = "Pepsi" is changed to If drink = "Coke" And drink = "Pepsi" and the customer's name is still Sally Preston and the drink is still Coke? Answer: Sally Preston does not prefer Coke or Pepsi. (Why?) 4. What is the e
--------------------
What on earth is the point of taking a course and posting the entire question on Answers so that someone else can anwer it for you? Save your parents' or the taxpayer's money and drop the course. You obviously have no interest in learning.
Source

No comments:

Post a Comment