
If execution depends on multiple different conditions (including one inside the other: the inner depends on the outer one)
Until now we focused on one scenario at a time. In the real world and in programing many times we do different things depending on more then one factor. If you stop and ponder over this one, you'll notice that many times throughout the day conditions overlap each other.
For example, in the MBX news channel, they just created a new slide that dynamically shows users an image based on a few parameters that overlap. There are a total of four possible colors the slide can be rendered in: black, dark gray, gray-blue and vivid blue. During the day: if the weather is great, the background image for the sky is blue. In any other case it is gray-blue. At night: if the weather is great the sky background is black and in every other scenario the sky background is rendered dark gray.
This situation occurs frequently in practice, when there are 2 or more categories of conditions to be checked, on the same entities.
In this context, the pseudo-code for the weather program scenario would be:
var time:string = “…”; var weather:string = “…”; var render:string = “…”;
//populate the above strings using logical actions
if(time == “daytime”) {
if(weather == “good”) {
render = “sky-blue”;
}else {
render = “gray-blue”;
}
}else {
if(weather == “good”) {
render = “pitch-black”;
}else {
render = “dark-gray”;
}
}
trace(“rendering MBX slide as” + render);
Nesting of an if-else clause inside other clauses is very intuitive, used in day-to-day actions you do yourself. The most important aspect you should grab and latch onto is always trying to find the relationship between the real world and programming. At the end of the day, programs were created for humans (some might say programmers are sub-human but yet we are human!
As such all programing languages were created tobalance between making things easy for both the computer and humans to understand.As long as you're open to noticing it, programming will become easier and mainly big fun!
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.