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

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.

主站蜘蛛池模板: 威远县| 堆龙德庆县| 台山市| 辽宁省| 贵定县| 新竹市| 金堂县| 余庆县| 大理市| 沁阳市| 江孜县| 和顺县| 宝坻区| 尚义县| 油尖旺区| 永年县| 罗定市| 建宁县| 成都市| 娱乐| 云霄县| 屏东县| 龙游县| 大邑县| 谢通门县| 芦山县| 高碑店市| 晴隆县| 宽城| 文水县| 苍山县| 海南省| 偏关县| 桐庐县| 鄯善县| 贞丰县| 苍溪县| 太白县| 通渭县| 区。| 冀州市|