Batch File Operators, If Else, Goto and For Loop

We have learned about variables and how to create the variables in batch files and how to manipulate them to perform different computational tasks. And also we are aware of the SET command and its flags to perform arithmetic calculations and take the inputs from the user. In this tutorial you will learn about batch file operators, if else, goto and for loop.

Batch File Operators

Immediately after this part the most important concept comes is the use of operators. Commands in batch programs also supports all the operators that other scripting languages support. So, I’m just giving an image that lists all the operators and their levels of precedence below. It also serves to have a quick review at all the operators with their description.

Batch File Operators, If Else, Goto and For Loop

Form your own expressions for every operator using some variables and evaluate the results. It’s very easy, so I leave it as an exercise for you.

Assuming that now you are comfortable with operators and variables, we move on to the next topic.

A computer program is defined to be a block of code that takes some inputs to the user, performs the computation and returns the result of computation to the user. Conditional statements are very essential in any computer program. Conditional statements allow us to do different things in our program based on the conditions. Without these conditional statements, our code would do the same thing every time it is given an input. Conditionals are the best statements that we can write in a program. They allow the program to vary itself for a particular case.

Bath File IF Else

The primary decision making statements used in batch programs are, ‘IF’ ‘ELSE’ and ‘IF NOT’ versions. The syntax for the IF statement is similar to that of all the programming languages and it executes a block only if the guard condition is true in case it is false, the else block is executed. Similarly, control enters an IF NOT block only if the guard condition is false.

Program #1: Print two numbers are equal or not 

@echo off
echo enter two numbers
set /p a=
set /p b=
if %a%==%b% (
echo equal
)
if not %a%==%b% (
echo not equal
)
Pause

The output for the above program is as follows. I have given the numbers as 1 and 2 so the output obviously would be not equal.

Output
enter two numbers
1
2
not equal
Press any key to continue . . .

We can also write the above program using the else clause as follows

Program #1 revision

@echo off
echo enter two numbers
set /p a=
set /p b=
if %a%==%b% (
echo equal
) else (
echo not equal
)
Pause

Batch File Goto

GOTO is a command used to branch from a particular point in the program unconditionally. GOTO statements also allow us to simulate the loops without the use of for statements in the program. Syntax of the goto command is 

goto label

Here the label means any named section of the program. Consider the below programming example to understand the goto command.

Program #2

@echo off
echo Hello
goto skipline
echo This line will never execute
:skipline
echo end of program
pause

Running the above batch program will produce the output as follows.

Output
Hello
end of program
Press any key to continue . . .

When a goto command is encountered, the program jumps to the line where the label name is found and then start the execution of instruction next to the label.

Program #3: Program that prints the natural numbers from 1 to n

@echo off

echo Enter n value
set /p n=
set i=1

:start
echo %i%
if %i%==%n% goto end
set /a i=%i%+1
goto start

:end
echo end of program
pause

Batch File For Loop

for {%variable} in (set) do command 

Bath files have a built in command to loop over a particular block of statements called for command. You can see the syntax of it in the above line. Let me consider an example to understand it in detail.

FOR %%G IN (20,-2,0) DO ECHO %%G

The first parameter is to be defined using a single character, for example the letter G ( you can use any letter you wish).

In each iteration of a FOR loop, the IN ( ….) clause is evaluated and %%G set to a different value. If this clause results in a single value then %%G is set equal to that value and the command is performed.

Program #4: Program to print numbers from n to 1

@echo off
set /p n=
FOR /L %%G IN (%n%,-1,0) do echo %%G
echo end of program
pause

Try out yourself
1. Practice and find all the flags in For loops of batch files.
2. Try to design the for loop using if and goto commands.

Leave a Comment

Your email address will not be published. Required fields are marked *