PowershellIf-Elseif-Else条件语句

PowershellIf-Elseif-Else条件语句

2023年8月2日发(作者:)

PowershellIf-Elseif-Else条件语句Powershell is a really powerful scripting language. It provides a lot of modern useful programming language features. Italso have basic features like conditional statements like If . In this tutorial we will look syntax and use cases of If .Powershell是⼀种⾮常强⼤的脚本语⾔。 它提供了许多现代有⽤的编程语⾔功能。 它还具有基本功能,例如条件语句(如If 。 在本教程中,我们将研究If的语法和⽤例。If is used to check if specified condition/s are met and if the condition is met do something if not check other conditions orexit. We will look different usage types below.如果⽤于检查是否满⾜指定的条件,以及是否满⾜条件,请执⾏其他操作(如果不检查其他条件)或退出。 我们将在下⾯介绍不同的⽤法类型。句法 (Syntax)Generic syntax of the if statement is like below. Other than first if line each line optional. There may be more than oneelseif lineif语句的通⽤语法如下所⽰。 除了第⼀if⾏每⾏可选。 可能有不⽌⼀个elseif线If (condition) {Block Command}elseIf (condition) {Block Command}...else {Block Command}单⼀条件 (Single Condition)We will start with simplest usage type of If. We will only check a single condition. According to conditions status which islogical values true or false specified script will run or not. Now we will do an example.我们将从If的最简单⽤法类型开始。 我们将只检查⼀个条件。 根据条件状态(逻辑值true或false指定的脚本将运⾏或不运⾏。 现在我们将举⼀个例⼦。In this example we will check if variable $a is bigger than 5 . If $a is bigger than 5 then we will print a message saying that $avalue is bigger than 5.在此⽰例中,我们将检查变量$a是否⼤于5。 如果$ a⼤于5,则我们将输出⼀条消息,指出$ a的值⼤于5。$a=10if ($a -gt 5){Write-Host("$a is bigger then 5")}Now lets examine what this code mean.现在让我们检查⼀下这段代码的含义。$a=10 creates a numeric variable and assigns the value 10$a=10创建⼀个数字变量并将值分配为10if () checks the statement between brackets and is the condition is true the code between curly brackets { Write-Host ("$ais bigger then 5")} is executed if not nothing will be executedif ()检查⽅括号之间的语句, if ()条件为true,则执⾏⼤括号之间的代码{ Write-Host ("$a is bigger then 5")}如果不执⾏任何操作,将执⾏$a -gt 5 is the most important part -gt means greater then so if we try to read this statement variable a is greater statement will be used as condition for if. In this example $a is 10 and this statement is true.$a -gt 5是最重要的部分-gt意味着更⼤,因此,如果我们尝试读取此语句,则变量a更⼤。 该语句将⽤作if的条件。 在此⽰例中,$ a为10,该语句为true。了解更多Python布尔变量类型多种条件(Multiple Conditions)Previous example we have check single condition. But the real world consist of complex problems and we have to usecomplex and multiple conditions in decision making process. If can be used to provide multiple conditions by defining themin else lines.在前⾯的⽰例中,我们检查了单个条件。 但是现实世界包含复杂的问题,我们在决策过程中必须使⽤复杂的多种条件。 通过在else⾏中定义If可以提供多个条件。In this example we will check variable $a whether it is bigger than 10 or not.在此⽰例中,我们将检查变量$a是否⼤于10。$a=5if ($a -gt 10){Write-Host("$a is bigger then 10")}elseif ($a -le 10){Write-Host("$a is lower then 10")}The if statement execution will start with first if line but the variable data is 5 and not bigger than 10. So this line will 语句的执⾏将从第⼀条if⾏开始,但是变量数据为5并且不⼤于10。因此将跳过此⾏。The if will resume with elseif statement and as we see that the ($a -le 10) condition will return 将使⽤elseif语句恢复,并且我们看到($a -le 10)条件将返回true。Because of the elseif condition is true the code block of elseif will be executed.因为elseif条件为true,否则将执⾏elseif的代码块。We can add more elseif conditions than one and all of them will have same syntax but different conditions. Here an otherexample which have more than one elseif我们可以添加更多的elseif条件,⽽不是⼀个,并且所有条件将具有相同的语法,但条件不同。 这⾥,其具有多于⼀个的其它⽰例elseif$a=5if ($a -gt 10){Write-Host("$a is bigger then 10")}elseif ($a -le 10){Write-Host("$a is lower or equal to 10 and bigger than 0")}elseif ($a -lt 0){Write-Host("$a is lower then 0")}其他 (Else)Up to now we have defined our conditions explicitly by using if or elseif statements. What is we do not define the conditionbut if the previous conditions do not match we want to execute a code block? We call this else statement. Else is put as thelast statement of the whole if block. If none of the previous conditions are met the last else code block is executed.到⽬前为⽌,我们已经通过使⽤if或elseif语句明确定义了我们的条件。 我们没有定义条件是什么,但是如果先前条件不匹配,我们要执⾏代码块? 我们称此为else声明。 Else则作为整个if块的最后⼀个语句。 如果先前的条件都不满⾜,则执⾏最后的else代码块。了解更多什么是⽂件系统块和超级块?In the example if the variable $a is not positive integer we will print a warning message to the console.在⽰例中,如果变量$a不是正整数,我们将向控制台输出警告消息。$a=-4if ($a -gt 10){Write-Host("$a is bigger then 10")}elseif ($a -ge 0){Write-Host("$a is lower or equal to 10 and bigger than 0")}else{Write-Host("$a is not positive integer")}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690916399a462815.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信