These are one of the very critical components of a program and they can define the flow of the program. As the name suggests, a decision maker decides a certain action based upon a certain condition.
Simply put, if you wanted to buy an ice cream you would go to an ice-cream shop, but for a coffee you would go to a coffee shop. In this case, the condition was whether you wanted ice cream or coffee. The action was based upon the result of the condition: you went to that specific shop.
These decision makers, also called conditions, are defined in a different manner in different scripting languages, but the result of each of the conditions decides the future flow of the program.
Generally, in a condition, two or more values are compared and either a true or a false is returned. Depending on the value returned, a specific set of instructions are executed.
Consider the following example:
Condition:
if (2 is greater than 3), then
Proceed to perform Option 1
else
Proceed to perform Option 2
As we see in the preceding example, a condition is evaluated and if 2 is greater than 3, then the flow of program will be performed based upon Option 1, and in case of a false (which means 2 is not greater than 3), Option 2 would be chosen.
If we want a bit more complexity, we can add multiple decision-making statements or conditions to granulize the flow of a program.
Let us take an example:
if (Car is of red color), then
if (Car is Automatic), then
if (Car is a sedan), then
Option 1 (Purchase the car)
else (Option 2, ask for a sedan car from dealer)
else (Option 3, ask for an Automatic car from dealer)
else (Option 4, ask for a red car from dealer)
As we can see in this complex condition, we can easily decide the flow of a program based upon additional checks. In this case, I only want to buy a Car that is red, Automatic, and a sedan. If any of those conditions are not met, then I ask the dealer to meet that specific condition.
Another thing to notice in the preceding example is that the conditions are nested within each other, hence they are shown as nested with spaces deciding the sub-conditions from its parent condition. This is usually depicted within brackets or with simple indentation based upon the scripting language used.
Sometimes, it is necessary to evaluate a value against multiple conditions and perform an action if it matches any of the conditions. This is called a switch case in programming.
Consider an example as follows:
Carcolor="Red" (Here we define a variable if the value of string as Red)
switch (Carcolor)
Case (Red) (Perform Option 1)
Case (Blue) (Perform Option 2)
Case (Green) (Perform Option 3)
Here we see that depending upon the variable's value, a certain type of action can be performed. In this case, option 1 will be performed. If we change the value of the Carcolor variable to Blue, then option 2 will be performed.
An important component of conditions are the comparison operators that we use to compare two values for the result. Some example operators are equal to, greater than, less than, and not equal to. Depending on which comparison operator we use, the results can vary.
Let us take an example:
greaternumber=5
lessernumber=6
if (greaternumber 'greater than' lessernumber)
Perform Option 1
else
Perform Option 2
We declare two variables named greaternumber and lessernumber and compare them in a condition. The conditional operator we use is greater than, which would result in option 1 if the condition is true (greaternumber is greater than lessernumber), or would result in option 2 if the condition is false (greaternumber is not greater than lessernumber).
Additionally, we also have operators that are called logical operators, such as AND, OR, or NOT. We can combine more than one condition by using these logical operators. They have a similar meaning in English, which means that if, for example, we use the AND operator, we want condition 1 AND condition 2 both to be true before we perform an action.
Consider an example: I want to buy a car only when the car is red, automatic, and a sedan:
if (car is 'red') AND (car is 'automatic') AND (car is 'sedan')
Perform action 'buy car'
else
Perform action 'do not buy'
This simply means I would evaluate all the three conditions and only if all of them are true, then I would perform the action buy car. In this case, if any of the conditions do not meet the values, such as the car is blue, then the do not buy action will be performed.