Python 中断语句详细信息
Python break statement details
任何人都可以提供有关此问题的详细信息吗?
for i in range(1,10):
if i == 5:
break
print("number is ", i)
Output:
number is 1
number is 2
number is 3
number is 4
我的问题是:为什么没有来5个??我在这里告诉我 == 5 这意味着当 i 值为 5 时它应该中断并给出结果 5 。
now, if i > 5:
break
print(i)
Output:
number is 1
number is 2
number is 3
number is 4
number is 5
这里 5 > 5 不是真的。那它呢??
最后一件事 if 结构
for i in range(1,10):
print(i)
if i < 5:
break
Output: only 1
问题:结果至少应该是 1 到 4 。但不是为什么?
感谢大家。希望您能理解并详细说明我的问题。
在第 i = 5 次迭代中,您通过了 if 测试的条件并执行语句的 then 块,其中包含 break。 break 语句使您退出循环。打印语句是循环体的一部分,在 i=5.
的迭代中永远不会到达
它没有打印 5 的原因是因为你在它到达 print("number is ", i)
之前中断了循环。
for i in range(1,10):
if i == 5: #when i is 5 this will be true
break #break the loop exits the loop
#----------------------This is not run when i==5 because the loop already ended
print("number is ", i)
如果你想打印 5
for i in range(1,10):
print("number is ", i) #put this in front
if i == 5: #when i is 5 this will be true
break #break the loop exits the loop
附带说明:range(1,10)
实际上是 [1,2,3,4,5,6,7,8,9]
,不包括十,因为 python 跳过了最后一个
对OP的评论:
for i in range(1,10):
if i > 5: #when i is 5 this will be false, so the loop doesn't break
break #break the loop when i > 5 (ie. 6) so now the print() isn't reached and will not print 6
print("number is ", i) #since the loop didn't break when i is 5, it printed i
你另外两个新例子:
for i in range(1,10):
if i > 5:
break #exits here skips the print since it's after this
print(i) #the print statement is here so when i > 5 this is not reached
#on the other hand:
for i in range(1,10):
print(i) #the print statement is in front of the break statement so now it will print 6 too since the loop hasn't break yet
if i > 5:
break #exits here after the print()
#output also includes 6
最后一件事 if 结构
for i in range(1,10):
print(i) #only 1 got printed since the loop break before it get to 2
if i < 5: #when i < 5 so it breaks in the first loop when i is 1
break #exits the loop
你可能想看看 this 看看是否有帮助
任何人都可以提供有关此问题的详细信息吗?
for i in range(1,10):
if i == 5:
break
print("number is ", i)
Output:
number is 1
number is 2
number is 3
number is 4
我的问题是:为什么没有来5个??我在这里告诉我 == 5 这意味着当 i 值为 5 时它应该中断并给出结果 5 。
now, if i > 5:
break
print(i)
Output:
number is 1
number is 2
number is 3
number is 4
number is 5
这里 5 > 5 不是真的。那它呢??
最后一件事 if 结构
for i in range(1,10):
print(i)
if i < 5:
break
Output: only 1
问题:结果至少应该是 1 到 4 。但不是为什么?
感谢大家。希望您能理解并详细说明我的问题。
在第 i = 5 次迭代中,您通过了 if 测试的条件并执行语句的 then 块,其中包含 break。 break 语句使您退出循环。打印语句是循环体的一部分,在 i=5.
的迭代中永远不会到达它没有打印 5 的原因是因为你在它到达 print("number is ", i)
之前中断了循环。
for i in range(1,10):
if i == 5: #when i is 5 this will be true
break #break the loop exits the loop
#----------------------This is not run when i==5 because the loop already ended
print("number is ", i)
如果你想打印 5
for i in range(1,10):
print("number is ", i) #put this in front
if i == 5: #when i is 5 this will be true
break #break the loop exits the loop
附带说明:range(1,10)
实际上是 [1,2,3,4,5,6,7,8,9]
,不包括十,因为 python 跳过了最后一个
对OP的评论:
for i in range(1,10):
if i > 5: #when i is 5 this will be false, so the loop doesn't break
break #break the loop when i > 5 (ie. 6) so now the print() isn't reached and will not print 6
print("number is ", i) #since the loop didn't break when i is 5, it printed i
你另外两个新例子:
for i in range(1,10):
if i > 5:
break #exits here skips the print since it's after this
print(i) #the print statement is here so when i > 5 this is not reached
#on the other hand:
for i in range(1,10):
print(i) #the print statement is in front of the break statement so now it will print 6 too since the loop hasn't break yet
if i > 5:
break #exits here after the print()
#output also includes 6
最后一件事 if 结构
for i in range(1,10):
print(i) #only 1 got printed since the loop break before it get to 2
if i < 5: #when i < 5 so it breaks in the first loop when i is 1
break #exits the loop
你可能想看看 this 看看是否有帮助