Transcript from the "Managing Flow Control" Lesson
>> Let's talk about decisions and conditionals. This is kind of the class, shall I stay or shall I go? If I stay there will be trouble, if I go there will be double. This is a very, very simple module. It just comes down to if something is true or false, then you perform some actions.
[00:00:19] So does true equal true? Does batman.secretidentity == Bruce Wayne? Does client ID equal payload ID or not? And so all of the conditions that we encounter in our code and ironically in life, really comes down to is this condition true or false? Even when you are looking at, I have multiple options to take, I'm at a crossroads and should I go here or should I take door number 1, 2 or 3?
[00:00:58] Well ultimately, your choice of that door is going to depend on you determining the condition that's going to affect your final decision, and if that's true or false, then that affects your action. So one of the most common ways that I use conditions, is in a container component if I'm saving an object, and I'll just say does this have a client ID or does this have an ID?
[00:01:30] If it does, then I update it, else, I will create it. And so here I'm saying, if this doesn't have a client ID, do this else do this. Now another form of this and as we start to move into kind of this brave new world of type script with kind of functional programming in the middle of operations, is that you will see this ternary operator.
[00:01:57] Which is the conditioned exists around client ID equals payload ID. And if that evaluates to true, then the left side of the colon is executed. If it evaluates to false, then the right side is and so this is a very shorthand way to do if else. Now, this is one that I quite like, because we are faced with more than one possible option at any given time.
[00:02:34] This is life, this is what switch cases were invented for. So in this case, imagine a calculator, and you're given two numbers and then an operation parameter. And then based on that operation, you're either going to add it, subtract, multiply, divide it or just return 42 because that is always the answer.
[00:02:55] And so this allows us to go through and say here is kind of a table of possible options, and then based on the cases available, so you're still evaluating to true or false. And so if the operation equals add, then that's what you're going to do. Okay, so that was, I feel like if you ever been standing it like a platform and a train station, the train just slips by, that's kinda what that just felt like.
[00:03:27] So we're done waxing the train, let's get down to business.