Batch Script - Operators
Hello there, future programmers! Today, we're diving into the exciting world of Batch Script operators. Don't worry if you've never written a line of code before – I'll be your friendly guide through this adventure. By the end of this lesson, you'll be manipulating data like a pro!
Arithmetic Operators
Let's start with something familiar: math! Arithmetic operators in Batch Script work just like the ones you learned in school. They help us perform calculations within our scripts.
Here's a table of the arithmetic operators we'll be using:
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 |
- | Subtraction | 7 - 2 |
* | Multiplication | 4 * 6 |
/ | Division | 8 / 2 |
% | Modulus (Remainder) | 9 % 4 |
Now, let's see these operators in action with some code examples:
@echo off
set /a result=5+3
echo 5 + 3 = %result%
set /a result=7-2
echo 7 - 2 = %result%
set /a result=4*6
echo 4 * 6 = %result%
set /a result=8/2
echo 8 / 2 = %result%
set /a result=9%%4
echo 9 %% 4 = %result%
When you run this script, you'll see the results of each calculation. The set /a
command tells Batch that we're doing arithmetic. Notice how we use %%
for the modulus operator in the script – that's because a single %
has a special meaning in Batch, so we need to escape it with another %
.
Relational Operators
Next up, we have relational operators. These are like the comparisons you make every day: Is this bigger than that? Are these two things equal? Let's look at our options:
Operator | Description | Example |
---|---|---|
EQU | Equal to | 5 EQU 5 |
NEQ | Not equal to | 4 NEQ 5 |
LSS | Less than | 3 LSS 4 |
LEQ | Less than or equal to | 3 LEQ 3 |
GTR | Greater than | 5 GTR 4 |
GEQ | Greater than or equal to | 5 GEQ 5 |
Here's a script that puts these operators to work:
@echo off
set num1=5
set num2=3
if %num1% EQU %num2% (
echo The numbers are equal
) else if %num1% GTR %num2% (
echo %num1% is greater than %num2%
) else (
echo %num1% is less than %num2%
)
This script compares num1
and num2
, then tells us their relationship. Run it and see what happens!
Logical Operators
Logical operators help us combine conditions. Think of them as the "and," "or," and "not" in your everyday language.
Operator | Description | Example |
---|---|---|
AND | Logical AND | condition1 AND condition2 |
OR | Logical OR | condition1 OR condition2 |
NOT | Logical NOT | NOT condition |
Let's use these in a script:
@echo off
set age=25
set hasLicense=true
if %age% GEQ 18 (
if "%hasLicense%"=="true" (
echo You can drive a car
) else (
echo You're old enough, but you need a license
)
) else (
echo You're too young to drive
)
This script checks if someone is old enough to drive AND has a license. It's like the conditions you'd check before handing over your car keys!
Assignment Operators
Assignment operators are how we give values to variables. In Batch, we mainly use the set
command for this.
@echo off
set name=John
echo My name is %name%
set /a number=10
set /a number+=5
echo Number is now %number%
Here, we assign the name "John" to the variable name
, and then we use /a
to do arithmetic assignment with number
.
Bitwise Operators
Lastly, we have bitwise operators. These work on the binary representations of numbers. They're a bit advanced, but let's take a peek:
Operator | Description | Example |
---|---|---|
& | Bitwise AND | 5 & 3 |
| | Bitwise OR | 5 | 3 |
^ | Bitwise XOR | 5 ^ 3 |
Here's a simple example:
@echo off
set /a result=5&3
echo 5 & 3 = %result%
set /a result=5|3
echo 5 | 3 = %result%
set /a result=5^3
echo 5 ^ 3 = %result%
These operations are working on the binary (base-2) representations of 5 (101) and 3 (011).
And there you have it! We've covered all the main operators in Batch Script. Remember, the key to mastering these is practice. Try writing your own scripts, experiment with different operators, and don't be afraid to make mistakes – that's how we learn!
In my years of teaching, I've found that the students who excel are those who aren't afraid to play around with code. So, go ahead, be curious, and have fun with Batch Scripting. Before you know it, you'll be writing complex scripts and impressing everyone with your coding skills!
Credits: Image by storyset