- 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.
推薦閱讀
- Visual C++數(shù)字圖像模式識(shí)別技術(shù)詳解
- 深入實(shí)踐Spring Boot
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- Wireshark Network Security
- Data Analysis with IBM SPSS Statistics
- Mastering Julia
- Podman實(shí)戰(zhàn)
- Data Analysis with Stata
- Python數(shù)據(jù)分析從0到1
- The HTML and CSS Workshop
- 大模型RAG實(shí)戰(zhàn):RAG原理、應(yīng)用與系統(tǒng)構(gòu)建
- R語言數(shù)據(jù)可視化:科技圖表繪制
- Drupal 8 Development Cookbook(Second Edition)
- Learn Linux Quickly
- Mastering Node.js