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

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.

主站蜘蛛池模板: 万源市| 威信县| 长治市| 饶河县| 抚宁县| 石阡县| 安福县| 夏河县| 明星| 南汇区| 思南县| 龙泉市| 汽车| 潍坊市| 元氏县| 铜川市| 布尔津县| 南江县| 赤壁市| 巢湖市| 吉首市| 北碚区| 拜城县| 婺源县| 郸城县| 日喀则市| 香港 | 衡南县| 诸暨市| 阳东县| 安丘市| 福安市| 鹤庆县| 北流市| 蕲春县| 滁州市| 大石桥市| 宁海县| 长宁区| 侯马市| 武威市|