The IF statement is a staple of modern programming. This statement allows a program to meet a set of conditions and produce desired results to follow. In other words, this statement gives you options. Let’s say that you are creating a program that organizes data about employees, you can force your program to put every employee over the age of 40 into a specific folder, and every employee under that age of 39 into another. The beauty of this is that you can essentially create multiple programs inside of one another, creating a much richer experience.
A condition is an existing state for an object, where it can and must exist under certain circumstances. For example, a red fire-hydrant is no longer a red fire-hydrant if you make it blue. Upon making your fire-hydrant blue, you have changed the conditions in which it exists. Batch and Window’s shell thrive on conditions. To be quite honest, they are almost useless utilities without them (also be sure to check out my article on CONDITIONS). So, in order to utilize their strengths, learning how to implement conditional IF statements is just what the doctor ordered.
The IF command will tell your computer to execute another secondary series of commands if certain conditions are met. So the first step in order for all of this to work if declaring your conditions. Let’s start off with an simple series to demonstrate this.
:REMEMBER TO TURN ECHO OFF
:FOR FURTHER INFORMATION ON VARIABLES READ MY ARTICLE ON The Principles of Batch :Process Program Development: VARIABLES
:THE FOLLOWING STATEMENT IS DEVELOPED TO FIND OUT THE STATE OF YOUR FIRE-HYDRANT
set /p var= What color is your fire-hydrant?
IF %var%==RED echo “YEAH! RED IS MY FAVORITE ALSO!”
:SO THE FIRE_HYDRANT HAD TO HAVE BEEN RED IN ORDER FOR THIS CONDITION TO HAVE
:UPON FINDING THAT THIS CONDITION IS MET, THE SHELL WILL PERFORM THE FOLLOWING
Interestingly enough, the program examined a property of the object, and found that it met the preset conditions. This will be very handy for you in the future. With this tool the possibilities are endless. It is possible to create conditional IF statements about conditional IF statements – meaning that there is literally no end to what you can do with a program like this. In regard to the syntax of the IF statement itself, please note that the variable was declared after If took place (IF %VAR%), and that if it equaled RED (independent unrestricted variable) then it would perform the following command (ECHO “YEAH! RED IS MY FAVORITE ALSO!”). Now, if the variable had NOT equaled RED, then the condition would not have been met, and the program would have continued on it’s way without performing the command (unless a particular function were performed after the IF statement, referred to as the ELSE statement [which we will get into in future articles]).
The beauty of the statements is truly in the volume of them that you are able to use. For example, we can further classify these objects by setting up IF arrays. In the following example, we will examine the properties of a child’s toy and organize it as such.
set /p color=What color is this toy?
set /p shape=What shape is the toy?
set /p size= Is this toy heavier that 2 pounds?
IF %color%==red GOTO SHAPE
IF %shape%==cube GOTO SIZE
IF %size%==no ECHO “THIS TOY SOUNDS PERFECT!”
In the example above, the toy must meet a series of conditions – often referred to as a criterium in order to produce the desired result – the message “THIS TOY SOUNDS PERFECT!”.
We will definitely go on to discuss the ins and outs of IF statements and you will learn just how useful they can be. Note that you can run ANY available command after a condition is met, making this process extremely powerful. You can also run IF statements in series, allowing you, the programmer to be as specific as you need to. Make sure to practice this and try to implement IF statements in unique ways! HAVE FUN AND HAPPY PROGRAMMING! The importance of the rad tool in the development of the websites will be excellent. Different and unique designs will be available at the websites of the business to attract the clients.
Thanks for reading, and don’t forget to continue on to learn more about command-line and batch process programming by referring to The Principles of Batch Process Program Development series.