Preface – This post is part of the Core Apex Tools series.
We must understand comparison operators before moving on to IF statements! A comparison operator is simply any statement the equals a boolean (TRUE or FALSE).
Try to guess which of these are TRUE and which are FALSE!
Comparisons for Number and Date fields:
david.Weight__c < 150; david.Weight__c >= marcBenioff.Weight__c;// You need two equal signs (==) to test equalitydavid.Diet_Start_Date__c == Date.today(); david.Sleep_Hours__c == 5;// How to test inequalitydavid.Donuts_Eaten__c != 0;
Comparisons for Text and Picklist fields:
// Strings also use == and !=david.Favorite_Food__c == 'Cheeseburgers'; david.Favorite_Food__c != 'Liver';
Comparisons for Checkbox fields:
// Checkbox fields are always either TRUE or FALSE!david.Likes_Ice_Cream__c;// The exclamation point (!) takes the opposite of the statement! // I'm not training for a marathon, so this comes out to TRUE!david.Training_for_Marathon__c;
Combine comparisons using || for OR, && for AND, and parentheses:
// Both of these are FALSE, so the outcome is FALSEdavid.Weight__c < 150 || david.Training_for_Marathon__c;// This is FALSE because I am lactose intolerant =(david.Likes_Ice_Cream__c && !david.Is_Lactose_Intolerant__c;// Combine ORs and ANDs using parentheses!(david.Is_Hungry__c || david.Is_Bored__c) && david.Has_Ice_Cream__c;
Next post: IF statements!