6
4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB .NET Programming

4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Embed Size (px)

Citation preview

Page 1: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

4c – Logical Operations

Lingma AchesonDepartment of Computer and Information Science, IUPUI

CSCI N331 VB .NET Programming

Page 2: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

2

Logical Operators• AndAlso

– Connects two or more conditions– All the conditions have to be true for the compound

condition to be true– E.g.

‘Grade will be A if both conditions are satisfied If intFinalScore >= 85 AndAlso strExtra = “Pass” Then

strLetterGrade = “A” End If ‘The loan amount will be 25000 if the person is a student And income is lower

‘than 10000. ‘Otherwise (either a student or low income), the amount will be 10000 If strIdentity = “student” AndAlso intIncome < 10000Then

intLoanAmount = 25000 Else

intLoanAmount = 10000 End If

Page 3: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

3

Logical Operators• OrElse

– Connects two or more conditions– At least one condition has to be true for the

compound condition to be true (also true if more than one condition is true).

– E.g. ‘Grade will be A if it meets at least one of the conditions.

‘The grade will be A if either the final score is 90+ or the project score is 95+‘The score will still be A if both conditions are met. If intFinalScore >= 90 OrElse intProject >= 95 Then

strLetterGrade = “A” End If

Page 4: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

4

Logical Operators• Xor

– Connects two or more conditions– Exclusive OR: Only one condition can be true for the

compound condition to be true.– E.g.

‘Grade will be D only if it meets one of the conditions.‘The grade will be D if either the final score is <60 or the project score is < 60‘If both are < 60, the grade will be F. If intFinalScore < 60 Xor intProject < 60 Then

strLetterGrade = “D”Else

strLetterGrade = “F” End If

Page 5: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

5

Logical Operators• Xor

‘An example determining discount ‘Either a senior citizen or a VIP member can get 10% discount

If strStatus = “senior” Xor strMember = “VIP” Then intDiscount = 10

‘The above If condition will be false if the person is a senior VIP or neither, thus‘it will false into the below ElseIf branchElseIf strMember = “VIP” Then ‘If a senior VIP, will get 20% discount

intDiscount = 20 End If

Page 6: 4c – Logical Operations Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

6

Logical Operators• Not

– Negate the condition. If the condition evaluates to true, negate the condition will make it false, and vise versa.

E.g. If Not strStatus = “kids” Then ‘ if status is not kids, get 10%

intDiscount = 10 ‘ discount

Else ‘ If status is kids, get 20% discount

intDiscount = 20

End If