News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Leant To Code - Comparisons or OR oeprator

Started by kevin, April 24, 2023, 10:50:49 PM

Previous topic - Next topic

kevin

 This post is reply to a question on social media, but the question has since been removed..  




from what I can gleem the question is to check for if a free coupon is used and set the price to 0 but only on a Friday.

from the top of the code there's a variable called "DiscountCode"  which is contain a word (it's a string in other words)  

To check is a variable is equal to something we use a comparison.  In This case we want to know if the word in the variable is same as our "FREEFRIDAY"   so we use an equality test.  Which in most programming language is represented as the ==    

So  DiscountCode == "FREEFIRDAY"  is comparing if the two sides of the comparison are a perfect match.. In other words is checks if the word stored in the DiscountCode variable is "FREEFRIDAY"  (without the quotes)

 If they are equal (or not)  the language will substitute a numeric representation of the result of this comparison in it's place.   These are often called booleans,  but you can just think of them as either a TRUE or FALSE.    In most languages TRUE is a value of 1 and a false is a 0  


 The "IF" statement block is used to selectively  execute the lines with it when the comparison is TRUE  

 Imagine this mock up code

 DiscountCode = "HELLO"

 Price = 100  
 
 IF (DiscountCode=="FREEFRIDAY")
          Price = 0
 Endif

 Now  what would Price be ??


The computer/compiler would run the code from top -> down.

1)   it sets DiscountCode$ variable to the word "HELLO".      

2)   it sets Price variable to a value 100.      

3)   Next see's the start of IF block.   It runs the block after the if expecting that code to resolve down to a TRUE (1) or FALSE (0).  

  The code after the IF is doing a comparison between DiscountCode$ and "FREEFRIDAY" .  Since we know DiscountCode contains the word "HELLO" and the HELLO and FREEFRIDAY don't match the comparison returns a FALSE back to the IF block.

  The IF block then reacts based the  condition for a TRUE or FALSE.

 IF the condition was TRUE,  the compiler would enter the IF block and run all the code inside the braces {  Price=0  }  But it wasn't it returned a FALSE, so in this case the computer will jump past that block to run any code following the closing }  of that IF block


Let's make another example.  So what if we want price to different every day of the week.  Knowing that we can selective run code inside a IF block and if we had a DAY variable containing the day of the week, we could create a group if Statements to catch all possible paths..  


Mock up Example  

  Day$ = "Wednesday"
  Price =0

   if (Day$ ="Monday")
         Price = 100
   endif

   if (Day$ ="Tuesday")
         Price = 200
   endif
 

   if (Day$ ="Wednesday")
         Price = 300
   endif
 
   if (Day$ ="Thursday")
         Price = 400
   endif

   if (Day$ ="Friday")
         Price = 500
   endif

   
   Now if we were to run this code what would it PRICE be set  to  at the end ??  
 
   The answer is  300      

   What would price be if we changed Day$ = "Wednesday"  to    Day$ = "Friday"  at the top code code ??

   The answer is  500      

   What would price be if we changed  Day$ = "Friday"  to    Day$ = "Saturday"  at the top code code ??

   The answer is  0  , since it's default value is set at the top of the program and is never changed  as all of the IF blocks fail (skipping the code within them)  as there's no if block that catches with Day is Saturday.


   Now what if wanted the Price to set to 1000 on both Saturday and Sunday   ?  What code would we add to catch those situations ??  

   Well, we could just add two new IF blocks with comparisons for those days, perhaps a bit like this


  if (Day =="Saturday")
         Price = 1000
 Endif

   if (Day =="Sunday")
         Price = 1000
 Endif


 That would work,  but since we want the same behavior for both Saturday and Sunday,  we could combined those comparisons into one IF block.   But to do this; we'd have to use a OR operator (This symbol ||).

 In this case the easiest way to think of OR operator is that it takes the LEFT and RIGHT results (those Booleans values TRUE and FALSE) and will return a TRUE if either or both sides of the OR are TRUE(1).   Only ever returning a FALSE(0) when the left and right conditionals are false.  

 so   TRUE ||  TRUE   equals TRUE
 so   FALSE ||  TRUE   equals TRUE
 so   TRUE ||  FALSE   equals TRUE
 so   FALSE ||  FALSE   equals FALSE
 
 This is an important concept to grasp as we'll need it in order to stack comparisons together.

 The following code will catch both days  
 if ( (Day =="Saturday")  ||   (Day =="Sunday")  )
        Price = 1000
 Endif

 So how does this work ??   Well when the computer/compiler sees that statement after the IF block is breaks it down in order of precendence.  In the example I've placed brackets around the two comparisons to show clearly that we what those done FIRST.

 So lets imagine how the compiler might break down ( (Day$ =="Saturday")  ||   (Day$ =="Sunday")  )  into a single final TRUE or FALSE

 Well, first it'll find the inner most pair of brackets..  There are two of these in this expression,  so
we'll imagine if scans from the LEFT to RIGHT and meaning it'll finds the (Day$="Satudary")  check first.  
 
 To evaluate this part is compares Day with "Saturday",  it then removes this code substituting the code with result TRUE or FALSE (whatever the answer was from the equality comparison)   next it'd solve the comparison (Day$ ="Sunday")  and substitute either a TRUE or FALSE in it's place


  If we set day to be "Sunday"

  So after solving the comparisons, this initial line

  ( (Day$ ="Saturday")  ||   (Day$ ="Sunday")  )

  Well becomes something more like this as the compiler is working on it

  (  FALSE  ||  TRUE  )
 
   But we're still not finished.  Compiler would continued searching for brackets, this time it finds the expression with the solved comparisons on either side of the OR (||) operator

   So it'll solve the OR using the table above..   If either side of the OR is TRUE the or statement gets removed and the result returned back into the expression.  

  Making this expression solve down to    
  (  TRUE  )

  Subsituting this in the original IF block
 if (  TRUE  )
        Price = 1000
 Endif

  So this IF block would evaluate to be TRUE and the Price would be set to 1000, but only when Day is equal to either Saturday or Sunday..  

  I know i'm not answering your question directly, rather i'm trying to give you some insight to the concepts here.   Once you know them you'll be able to answer your original question and any variation thereof..  
 

  Related Articles
   Comparisions