热门标签 | 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

批处理 运算符



推荐阅读
  • 本指南从零开始介绍Scala编程语言的基础知识,重点讲解了Scala解释器REPL(读取-求值-打印-循环)的使用方法。REPL是Scala开发中的重要工具,能够帮助初学者快速理解和实践Scala的基本语法和特性。通过详细的示例和练习,读者将能够熟练掌握Scala的基础概念和编程技巧。 ... [详细]
  • PHP预处理常量详解:如何定义与使用常量 ... [详细]
  • POJ 2482 星空中的星星:利用线段树与扫描线算法解决
    在《POJ 2482 星空中的星星》问题中,通过运用线段树和扫描线算法,可以高效地解决星星在窗口内的计数问题。该方法不仅能够快速处理大规模数据,还能确保时间复杂度的最优性,适用于各种复杂的星空模拟场景。 ... [详细]
  • 如何撰写适应变化的高效代码:策略与实践
    编写高质量且适应变化的代码是每位程序员的追求。优质代码的关键在于其可维护性和可扩展性。本文将从面向对象编程的角度出发,探讨实现这一目标的具体策略与实践方法,帮助开发者提升代码效率和灵活性。 ... [详细]
  • 在 Kubernetes 中,Pod 的调度通常由集群的自动调度策略决定,这些策略主要关注资源充足性和负载均衡。然而,在某些场景下,用户可能需要更精细地控制 Pod 的调度行为,例如将特定的服务(如 GitLab)部署到特定节点上,以提高性能或满足特定需求。本文深入解析了 Kubernetes 的亲和性调度机制,并探讨了多种优化策略,帮助用户实现更高效、更灵活的资源管理。 ... [详细]
  • 在《Linux高性能服务器编程》一书中,第3.2节深入探讨了TCP报头的结构与功能。TCP报头是每个TCP数据段中不可或缺的部分,它不仅包含了源端口和目的端口的信息,还负责管理TCP连接的状态和控制。本节内容详尽地解析了TCP报头的各项字段及其作用,为读者提供了深入理解TCP协议的基础。 ... [详细]
  • 在对WordPress Duplicator插件0.4.4版本的安全评估中,发现其存在跨站脚本(XSS)攻击漏洞。此漏洞可能被利用进行恶意操作,建议用户及时更新至最新版本以确保系统安全。测试方法仅限于安全研究和教学目的,使用时需自行承担风险。漏洞编号:HTB23162。 ... [详细]
  • 本文介绍了如何利用Shell脚本高效地部署MHA(MySQL High Availability)高可用集群。通过详细的脚本编写和配置示例,展示了自动化部署过程中的关键步骤和注意事项。该方法不仅简化了集群的部署流程,还提高了系统的稳定性和可用性。 ... [详细]
  • 为了确保iOS应用能够安全地访问网站数据,本文介绍了如何在Nginx服务器上轻松配置CertBot以实现SSL证书的自动化管理。通过这一过程,可以确保应用始终使用HTTPS协议,从而提升数据传输的安全性和可靠性。文章详细阐述了配置步骤和常见问题的解决方法,帮助读者快速上手并成功部署SSL证书。 ... [详细]
  • Java Socket 关键参数详解与优化建议
    Java Socket 的 API 虽然被广泛使用,但其关键参数的用途却鲜为人知。本文详细解析了 Java Socket 中的重要参数,如 backlog 参数,它用于控制服务器等待连接请求的队列长度。此外,还探讨了其他参数如 SO_TIMEOUT、SO_REUSEADDR 等的配置方法及其对性能的影响,并提供了优化建议,帮助开发者提升网络通信的稳定性和效率。 ... [详细]
  • 在《ChartData类详解》一文中,我们将深入探讨 MPAndroidChart 中的 ChartData 类。本文将详细介绍如何设置图表颜色(Setting Colors)以及如何格式化数据值(Formatting Data Values),通过 ValueFormatter 的使用来提升图表的可读性和美观度。此外,我们还将介绍一些高级配置选项,帮助开发者更好地定制和优化图表展示效果。 ... [详细]
  • 深入解析Android 4.4中的Fence机制及其应用
    在Android 4.4中,Fence机制是处理缓冲区交换和同步问题的关键技术。该机制广泛应用于生产者-消费者模式中,确保了不同组件之间高效、安全的数据传输。通过深入解析Fence机制的工作原理和应用场景,本文探讨了其在系统性能优化和资源管理中的重要作用。 ... [详细]
  • 在Cisco IOS XR系统中,存在提供服务的服务器和使用这些服务的客户端。本文深入探讨了进程与线程状态转换机制,分析了其在系统性能优化中的关键作用,并提出了改进措施,以提高系统的响应速度和资源利用率。通过详细研究状态转换的各个环节,本文为开发人员和系统管理员提供了实用的指导,旨在提升整体系统效率和稳定性。 ... [详细]
  • 深入解析 SQL 数据库查询技术
    本文深入探讨了SQL数据库查询技术,重点讲解了单表查询的各种方法。首先,介绍了如何从表中选择特定的列,包括查询指定列、查询所有列以及计算值的查询。此外,还详细解释了如何使用列别名来修改查询结果的列标题,并介绍了更名运算的应用场景和实现方式。通过这些内容,读者可以更好地理解和掌握SQL查询的基本技巧和高级用法。 ... [详细]
  • 深入理解排序算法:集合 1(编程语言中的高效排序工具) ... [详细]
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社区 版权所有