if 判断条件1:
做事情1
elif 判断条件2:
做事情2
else:
做其它事
if(判断条件1)
{
做事情1
}
else if(判断条件2)
{
做事情2
}
else
{
做其它事
}
#if 常量表达式1
// 编译1
#elif 常量表达式2
// 编译2
#else
// 编译3
#endif
for ( init; condition; increment ){
statement(s);
}
// java
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
// java
int[] a = {1,2,3};
for(int i : a){
System.out.print(i + ",");
}
// C#
int[] a = {1,2,3};
foreach(int i in a){
System.Console.WriteLine(i);
}
for iterating_var in sequence:
statements(s)
# 例子
for i in range(3):
print(i)
for i in "hello":
print(i)
# 例1,普通可迭代对象
x = [1, 2, 3]
for i in x:
print(i)
for i in x:
print(i)
# 例2,迭代器或生成器
y = iter([1, 2, 3])
# y = (i for i in [1,2,3])
for i in y:
print(i)
for i in y:
print(i)
自遍历
过程,同时在经过 for 循环的 它遍历
后,也不会破坏原有的结构。(这两个是我创造的概念,详见《Python进阶:迭代器与迭代器切片》)。x = [1, 2, 3]
for i in x:
print(i, end = " ")
else:
print("ok")
# 输出:1 2 3 ok
x = [1,2,3]
for i in x:
if i % 2 == 0:
print(i) # match
break
else:
print("mismatch")
execute the for-loop (or while-loop)
if you reach a `break`, jump to the end of the `for...else` block
else execute the `else` suite
阅读 Python 的历史,从中你可以看到设计者们对功能细节的打磨过程,最终你就明白了,Python 是如何一步一步地发展成今天的样子。