热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

批处理运算符Wehavelearnedaboutvariablesandhowtocreatethevariablesinbatchfilesandhowtomanipulatet

批处理 运算符

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 offecho 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 offecho enter two numbersset /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 offecho Enter n valueset /p n=set i=1:startecho %i%if %i%==%n% goto endset /a i=%i%+1goto start:endecho end of programpause

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

批处理 运算符



推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文介绍如何使用 Python 编写程序,检查给定列表中的元素是否形成交替峰值模式。我们将探讨两种不同的方法来实现这一目标,并提供详细的代码示例。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
author-avatar
如哽在喉_495
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有