60 likes | 174 Views
4c – Logical Operations. CSCI N331 VB .NET Programming. Lingma Acheson. Department of Computer and Information Science, IUPUI. Logical Operators. AndAlso Connects two or more conditions All the conditions have to be true for the compound condition to be true E.g.
E N D
4c – Logical Operations CSCI N331 VB .NET Programming Lingma Acheson Department of Computer and Information Science, IUPUI
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 AndAlsostrExtra = “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”AndAlsointIncome < 10000Then intLoanAmount = 25000 Else intLoanAmount = 10000 End If
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 OrElseintProject >= 95 Then strLetterGrade = “A” End If
Logical Operators • Xor • Connects two or more conditions • Exclusive OR:Onlyone 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 XorintProject < 60 Then strLetterGrade = “D” Else strLetterGrade = “F” End If
Logical Operators • Xor ‘An example determining discount ‘Either a senior citizen or a VIP member can get 10% discount If strStatus = “senior” XorstrMember = “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 branch ElseIfstrMember = “VIP” Then ‘If a senior VIP, will get 20% discount intDiscount = 20 End If
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 NotstrStatus = “kids” Then ‘ if status is not kids, get 10% intDiscount = 10 ‘ discount Else ‘ If status is kids, get 20% discount intDiscount = 20 End If