Julia:循环

# while循环:格式如下
while *condition*
    *loop body*
end
# 例如用while计算1-100的和
s = 0
i = 1
while i <= 100
    s += i
    i += 1
end
print( s ) 

# for循环:结构如下
for *var* in *loop iterable*
    *loop body*
end
# 计算1-100之和
s = 0
for i in 1:100
    s += i
end
print( s )

# 输出字符串
str = ["abc","bcd","cde","edf","dfg"]
for substr in str
    println( substr )
end

# 对for循环还可以使用嵌套循环

# 以上就是Julia循环语法的使用方法,例如
a = [i+j for i in 1:10, j in 1:10]
与下面代码对应
a = zeros(10,10)
for i in 1:10, j in 1:10
    a[i,j] = i + j
end
# 或者
for i in 1:10
    for j in 1:10
        a[i,j] = i + j
    end
end

你可能感兴趣的:(JuliaNote,julia)