官术网_书友最值得收藏!

while loops

When you want to use looping as long as a condition stays true, use the while loop, which is as follows:

a = 10; b = 15 
while a < b 
    # body: process(a) 
    println(a) 
    global a += 1 
end 
# prints on consecutive lines: 10 11 12 13 14 

In the body of the loop, something has to change the value of a so that the initial condition becomes false and the loop ends. If the initial condition is false at the start, the body of the while loop is never executed. The global keyword makes a in the current scope refer to the global variable of that name.

If you need to loop over an array while adding or removing elements from the array, use a while loop, as follows:

arr = [1,2,3,4] 
while !isempty(arr) 
    print(pop!(arr), ", ") 
end 

The preceding code returns the output as 4, 3, 2, 1.

主站蜘蛛池模板: 建阳市| 长岛县| 逊克县| 西畴县| 定日县| 西平县| 威海市| 东阳市| 五原县| 大名县| 同德县| 紫金县| 镇江市| 连山| 琼海市| SHOW| 长宁县| 南昌县| 台东县| 犍为县| 南城县| 临洮县| 镇江市| 咸宁市| 察隅县| 高淳县| 灵川县| 唐海县| 会昌县| 犍为县| 和林格尔县| 汉沽区| 新河县| 罗山县| 丹棱县| 涟源市| 普安县| 延长县| 巴楚县| 南木林县| 会东县|