Control Flow Fundamentals by Internet Marketing Services India
So that's one way of doing things. Intractably state equals red. Then, in that case, we'll just print stop instead and that's fine. It says stop there. However, it's a little unnecessary in this case because we know again this is kind of based on assumptions that the traffic stay is either red or green now which means that if it's not green then it's red. The control flow fundamentals by internet marketing services india are beneficial for the system.
If it's not red then it's green. So realistically we only need to perform one test at a time. In this case, we're actually performing both tests regardless of the outcome of the previous test. So even if my traffic light is green I still perform the second test because it's part of the difference. If statements. So again I can simplify this because I know it's either going to be red or green and just add an ELSE case to this if statements. In this case, I'm just going to print. Stop. So this is a this is OK for this example because I know that if my traffic stay is not green. So if this test fails then it must be red. So I know it's a prince stopped.
Similarly, if I had these roles reverse and I had stopped if I had read up here. And I would sprint stop and I know it's not read. Then I know it has to be green so I would print go. So as you can see if I were to just go ahead and run this again the same result I get go being printed because my trash play stays green OK. And similarly, if it was red then I get stop being a prince at the not those couple of differences between this format and what we saw previously with the two if statements from internet marketing services india are not required.
So the first difference is the fact that this L's case is only going to be executed. If my first test fails. OK, so this means if my traffic by the state is indeed green or if my snarky state is red which it was previously this fails and so this does not get executed therefore I perform this execute this code. But if the shuffle I say is green then I'm going to execute this because this is true.
So around run this piece of code and I simply ignore this stuff because this worked out to be true. This is basically saying if everything above the else calls fails then we'll execute this. You can think about the statement as being kind of like a failsafe. The second difference is the fact that we're actually not testing anything here. The Else case isn't providing isn't performing any more tests so most will only ever perform one test in this case. OK. However, we know that traffic lights generally have at least three states.
So there's going to be a yellow light as well. Well, in that case, all we have right now the model is just going to print stop. But that's not exactly what we want. We want something different maybe slow down or speed up being printed. So what we have to do is perform an additional test as told by internet marketing services india to see. Now AK My Chartley state could be green or yellow or red K if it's not green or yellow then it must be red.
Will Prince stop so we can do this in a few different ways. I think the simplest way would be just to add an L S which is Shortall else. If my traffic light State equals yellow. Well, in this case, I'm just going to Prince let's be cautious let's just to slow down for now. So in this particular case, we've made stuff a little more complex with first performing this test to see if it's green.
K if this test fails then we'll perform this test. If this test fails then we'll fall back on our else case. Additionally, if we wanted to be really safe what we can do is say L is traffic state equals red. Then will print stop. This would actually be the best way of doing things because if for whatever reason GraphicLy state was something else or maybe there was a typo then we could have the else case be in print something like an unknown action.
I don't really know what to do if my short stay is for example blue All right so it's generally a good idea to have an ALS case particularly when working with internet marketing services india because strings are very typo prone which means if we make a typo somewhere and we really needed that string to be exactly the same as some other string then we're not going to get the behavior that we want.
So let's test to see what happens with all of all possible values. Start off with green and I'm going to zoom out and that's a zoom out there. So we'll go ahead and run this one as you can see. Go is printed out. Now, what if this is yellow. What happens then. What we get slowed down Prince that's to be expected. What happens if it's red will get stop being printed and what happens if it's some you know garbage string something that is different from those we get our unknown action.
So this is exactly the behavior that we loop's behavior that we want which is one to change it back to green. So this statement reads kind of like the following will perform this test first. If this test pulses will execute this will print go and then we'll ignore everything else and basically execute any code on the knee. If this first test fails then we'll move to our second test.
If internet marketing services india test passes execute and ignore this if this test fails will move to the stud. And so on and so forth. And if the third test fails of all of these tests fail them we're going to perform this action which is just going to be Prince unknown action. All right. That's your basic if else if at all statements. But we can make things more complex still. So he mentioned earlier how to do that we might be able to test multiple cases in one single statement and we can do that through the use of our and then operators.