批处理 运算符_批处理文件运算符(如果不存在),转到和循环

批处理 运算符

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.

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

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


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


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



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.
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.

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

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


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


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



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.

翻译自: https://www.thecrazyprogrammer.com/2015/06/batch-file-operators-if-else-goto-and-for-loop.html

批处理 运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值