一循环
for循环:
for循环使用一个特定的循环变量来迭代地运行相同的代码块指定的次数。通常用于遍历数组中的元素。
matlab:
%1 for j=1:4j end %2 for j=1:0.5:4j end %3 嵌套循环 for j=1:4for k=1:4a(j, k)=j*k;end end a 123456789101112131415
python:
#1 for j in range(4):print(j) #2直接遍历数组 import numpy as np a = np.array([1,2,3]) for j in a:print(j) 12345678
while循环:
循环只要测试条件为真就会迭代。一旦测试条件变为假,循环就退出。
matlab:
count = 0; while (count < 4)countcount = count + 1; end 12345
python
count = 0 while (count < 4):print(count)count = count + 1 1234
continue
内部循环中的continue将跳转到内部循环的下一个迭代,而外部循环不会受到影响。
break
终止当前循环。