- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 170字
- 2021-06-24 14:21:46
The break statement
Sometimes, it is convenient to stop the loop repetition inside the loop when a certain condition is reached. This can be done with the break statement, which is as follows:
a = 10; b = 150 while a < b # process(a) println(a) global a += 1 if a >= 50
break
end end
This prints out the numbers 10 to 49, and then exits the loop when break is encountered. The following is an idiom that is often used; how to search for a given element in an array, and stop when we have found it:
arr = rand(1:10, 10) println(arr) # get the index of search in an array arr: searched = 4 for (ix, curr) in enumerate(arr) if curr == searched println("The searched element $searched occurs on index $ix") break end end
A possible output might be as follows:
[8,4,3,6,3,5,4,4,6,6] The searched element 4 occurs on index 2
The break statement can be used in for loops as well as in while loops. It is, of course, mandatory in a while true...end loop.
推薦閱讀
- Mastering Ext JS(Second Edition)
- Python for Secret Agents:Volume II
- Oracle從新手到高手
- JavaScript 網(wǎng)頁(yè)編程從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開(kāi)發(fā)視頻大講堂)
- Learning Selenium Testing Tools(Third Edition)
- Hands-On Functional Programming with TypeScript
- Swift語(yǔ)言實(shí)戰(zhàn)精講
- UVM實(shí)戰(zhàn)
- 快速入門與進(jìn)階:Creo 4·0全實(shí)例精講
- Spring Security Essentials
- Orleans:構(gòu)建高性能分布式Actor服務(wù)
- Arduino Wearable Projects
- Flink技術(shù)內(nèi)幕:架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- MyBatis 3源碼深度解析
- Arduino電子設(shè)計(jì)實(shí)戰(zhàn)指南:零基礎(chǔ)篇