The decision makers or conditional statements help us to add different comparison and equality checks or rules in our software and application.
Most of the comparison and logical operators will be useless without conditional statements.
Conditional statement are for programming language, what our government, law enforcement agencies, traffic rules, etc. are for us. These set the rules, control the flow and make all decisions for our program.
Let’s take some real world rules as examples to better understand this.
If temperature is greater than or above 40˚ than don't go out.
If a vehicle is moving above 120 km/hour than put over-speeding fine on it.
If it is raining out side than you should take umbrella with you.
Let’s break these in parts now.
{If} temperature is {greater than or above} 40˚ {than} don't go out{.}
{If} a vehicle is {moving above} 120 km/hour {than} put over speeding fine on it{.}
{If} it {is raining} out side {than} you should take umbrella with you{.}
So in nutshell each rule or law can be divided into these parts.
And I have used ‘IF THAN’ type of rules/statements for a reason. Because there is one very powerful and basic conditional statement in almost all programming languages. And that is “IF” statement. Let’s try to convert one of above statements to a program. First we will convert our comparison operator.
If temperature >= 40 Than
Do not go out
.
Now let’s just put our comparison statement in simple braces.
If (temperature >= 40) Than
Do not go out
.
So it is still very much human readable. But, to convert it to a machine readable format, we will have to use a programming language. Let’s use PHP for now.
In PHP “if” keyword must be in small letters. “Than” must be replaced with “:”. And if we want to print “Do not go out” on a notice board, we will have to use “echo” function of PHP. Dot “.” or end of statement must be replaced with “endif”. So our above conditional statement will be like this in PHP.
if (temperature >= 40):
echo "Do not go out";
endif;
There is another way of writing “IF” statement in PHP which exactly same in C++, Java, JavaScript and other Friends of C languages. To convert our above conditional statement to that we will have to change “:” to a left “{” curly braket and “endif;” to a “}” a right “}” curly braket. Let’ do this.
// PHP
if (temperature >= 40) {
echo "Do not go out";
}
// Code will look almost similar in C++. Just change "echo" to "cout()"
if (temperature >= 40) {
cout("Do not go out");
}
// For Java, just change "cout" to System.out.print()
if (temperature >= 40) {
System.out.print("Do not go out");
}
So, you should be able to understand importance of conditional statements in programming languages. And how easy it is to convert a real life condition or rule to a program statement.
// Different conditional statements
if (temperature >= 40) {
echo "Do not go out";
}
// If Else Conditional statement.
// It is useful when you want to
// make a choice between two
if (temperature >= 40) {
echo "Do not go out";
} else {
echo "You can go out";
}
// Ternary Operation or Conditional Statement
echo (temperature >= 40) ?
"Do not go out" :
"You can go out";
// Switch Statement
// It is most effective for String
// Or fixed numbers comparison
switch (temperature) {
case "40":
echo "Do not go out";
break;
default:
echo "You can go out";
break;
}
There are so many different conditional statements available in many different programming languages. You just need to pick one and convert you condition or rule as per guidelines provided by programming language of your choice.
Let’s move on to next chapter of this course. Please click on “Next” to continue.
Maynard Sirmons
Bookmarked!, I really like your website!
Zeeshan Elahi Author
Thank you. Really appreciated.