Python “如果”和“如果”之间的主要区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25703446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the main difference between 'if' and 'else if'?
提问by J.Smith.JJJ
For e.g..
According to some experts,
例如。
据一些专家介绍,
The conditions here are mutually exclusive:
这里的条件是互斥的:
if(n>0):
print "Number is Positive"
if(n<0):
print "Number is Negative"
if(n==0):
print "Number is ZERO"
It would be better to rewrite with elif and else:
最好用 elif 和 else 重写:
if n > 0:
print "Number is Positive"
elif n < 0:
print "Number is Negative"
else:
print "Number is ZERO"
So I just want to ask the question that , Is there any difference between ' if 'and ' elif '. I know the basic difference between ' if ' and ' elif '. But I just want to know , Why some novice programmers prefer ' elif ' over ' if '?
所以我只想问一个问题,'if'和'elif'之间有什么区别。我知道“ if ”和“ elif ”之间的基本区别。但我只想知道,为什么有些新手程序员更喜欢“elif”而不是“if”?
回答by óscar López
The first form if-if-iftests allconditions, whereas the second if-elif-elsetests only as many as needed: if it finds one condition that is True, it stops and doesn't evaluate the rest. In other words: if-elif-elseis used when the conditions are mutually exclusive.
第一种形式if-if-if测试所有条件,而第二种形式if-elif-else只测试需要的数量:如果找到一个条件True,则停止并且不评估其余条件。换句话说:if-elif-else当条件互斥时使用。
Let's write an example. if you want to determine the greatest value between three numbers, we could test to see if one is greater or equal than the others until we find the maximum value - but once that value is found, there is no need to test the others:
让我们写一个例子。如果您想确定三个数字之间的最大值,我们可以测试一个是否大于或等于其他数字,直到找到最大值 - 但是一旦找到该值,就无需测试其他数字:
greatest = None
if a >= b and a >= c:
greatest = a
elif b >= a and b >= c:
greatest = b
else:
greatest = c
print greatest
Alternatively, we could assumeone initial value to be the greatest, and test each of the other values in turn to see if the assumption holds true, updating the assumed value as needed:
或者,我们可以假设一个初始值是最大的,然后依次测试其他每个值以查看假设是否成立,并根据需要更新假设值:
greatest = None
if a > greatest:
greatest = a
if b > greatest:
greatest = b
if c > greatest:
greatest = c
print greatest
As you can see, both if-if-ifand if-elif-elseare useful, depending on what you need to do. In particular, the second of my examples is more useful, because it'd be easy to put the conditional inside a loop - so it doesn't matter how many numbers we have, whereas in the first example we'd need to write many conditions by hand for each additional number.
正如您所看到的,根据您需要做什么,if-if-if和if-elif-else都很有用。特别是,我的第二个例子更有用,因为将条件放在循环中很容易 - 所以我们有多少数字并不重要,而在第一个例子中我们需要写很多为每个额外的数字手动设置条件。
回答by corvid
You can chain ifwith elifand finish with an elseif none of the conditions in the chain were met. When checking through the statements, it will find the first matching one and execute the instructions within that block, then break from the if/elif/else block
如果不满足链中的任何条件,您可以if使用elif和 结束else。检查语句时,它将找到第一个匹配的语句并执行该块中的指令,然后从 if/elif/else 块中中断
n = 6
if n % 2 == 0:
print('divisible by 2')
elif n % 3 == 0:
print('divisible by 3')
else:
print('not divisible by two or three')
this would print
这将打印
divisible by 2
However, say you replace that elifabove with an ifand remove the elseclause...
但是,假设您elif用 an替换上面的内容if并删除该else子句...
divisible by 2
divisible by 3
the elifchains the statements and chooses the first appropriate one.
该elif链条的陈述,并选择适当的第一个。

