loops.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Gravity: Loop</title>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
  9. <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
  10. <link rel="stylesheet" href="stylesheets/styles.css">
  11. </head>
  12. <body>
  13. <!-- BEGIN NAVIGATION BAR -->
  14. <nav class="navbar navbar-default navbar-fixed-top">
  15. <div class="container">
  16. <div class="navbar-header">
  17. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  18. <span class="sr-only">Toggle navigation</span>
  19. <span class="icon-bar"></span>
  20. <span class="icon-bar"></span>
  21. <span class="icon-bar"></span>
  22. </button>
  23. <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
  24. </div>
  25. <div id="navbar" class="navbar-collapse collapse">
  26. <ul class="nav navbar-nav navbar-right">
  27. <li class="active"><a href="index.html">Gravity</a></li>
  28. <li><a href="internals/index.html">Internals</a></li>
  29. <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
  30. </ul>
  31. </div><!--/.nav-collapse -->
  32. </div>
  33. </nav>
  34. <!-- END NAVIGATION BAR -->
  35. <div class="container body-container">
  36. <div class="main-content">
  37. <div class="row">
  38. <!-- BEGIN SIDEBAR -->
  39. <div class="col-sm-3 border-right section-left">
  40. <div saveheight="1" class="sidebar-nav">
  41. <h4>INTRODUCTION</h4>
  42. <ul>
  43. <li><a href="index.html"><span>Introduction</span></a></li>
  44. <li><a href="getting-started.html"><span>Getting Started</span></a></li>
  45. </ul>
  46. <h4>LANGUAGE GUIDE</h4>
  47. <ul>
  48. <li><a href="syntax.html"><span>Syntax</span></a></li>
  49. <li><a href="operators.html"><span>Operators</span></a></li>
  50. <li><a href="types.html"><span>Types</span></a></li>
  51. <li><a href="lists.html"><span>Lists</span></a></li>
  52. <li><a href="maps.html"><span>Maps</span></a></li>
  53. <li><a href="enum.html"><span>Enum</span></a></li>
  54. <li><a href="functions.html"><span>Functions</span></a></li>
  55. <li><a href="closures.html"><span>Closures</span></a></li>
  56. <li><a href="classes.html"><span>Classes</span></a></li>
  57. <li><a href="control-flow.html"><span>Control Flow</span></a></li>
  58. <li><a href="loops.html" class="active"><span>Loops</span></a></li>
  59. <li><a href="fibers.html"><span>Fibers</span></a></li>
  60. </ul>
  61. <h4>ADVANCED</h4>
  62. <ul>
  63. <li><a href="api.html"><span>Embedding API</span></a></li>
  64. <li><a href="system.html"><span>System class</span></a></li>
  65. <li><a href="unit-test.html"><span>Unit test</span></a></li>
  66. <li><a href="contributing.html"><span>Contributing</span></a></li>
  67. </ul>
  68. </div>
  69. </div>
  70. <!-- END SIDEBAR -->
  71. <div class="col-sm-9 border-left section-right">
  72. <h1 class="section-header">Loop</h1><hr>
  73. <p class="section-content">
  74. <h4 class="section-h4">While loop</h4>
  75. <p>A while loop performs a set of statements until a condition becomes false. These kinds of loops are best used when the number of iterations is not known before the first iteration begins.</p>
  76. <pre><code class="swift">
  77. func main() {
  78. var i = 0;
  79. while (i < 50000) {
  80. i += 1;
  81. }
  82. return i;
  83. }
  84. </code></pre>
  85. <h4 class="section-h4">Repeat-while loop</h4>
  86. <p>The other variation of the while loop, known as the repeat-while loop, performs a single pass through the loop block first, before considering the loop’s condition. It then continues to repeat the loop until the condition is false.
  87. </p>
  88. <pre><code class="swift">
  89. func main() {
  90. var i = 0;
  91. repeat {
  92. i += 1;
  93. } while (i < 50000);
  94. return i;
  95. }
  96. </code></pre>
  97. <h4 class="section-h4">For loop</h4>
  98. <p>You can access an element from a list by calling the subscript operator [] on it with the index of the element you want. Like most languages, indices start at zero:</p>
  99. <pre><code class="swift">
  100. var count = 0;
  101. for (var i in 0...40) {
  102. count += i;
  103. }
  104. return count;
  105. </code></pre>
  106. <p>The for in loop can be used over any object that supports iteration, such as <a href="lists.html">Lists</a> or <a href="maps.html">Maps</a>.</p>
  107. <h4 class="section-h4">Loop method</h4>
  108. <p>Performing a loop is very common operation in any programming language, so Gravity adds a very convenient way to run a loop by adding a special loop method to some classes (Int, Range, List and Map) that accepts a <a href="closures.html">closure</a> as parameter:</p>
  109. <pre><code class="swift">
  110. func main() {
  111. 4.loop({System.print("Hello World");});
  112. }
  113. // Output:
  114. // Hello World
  115. // Hello World
  116. // Hello World
  117. // Hello World
  118. </code></pre>
  119. <p>If we need to access the current index of the loop we can just rewrite the closure:</p>
  120. <pre><code class="swift">
  121. func main() {
  122. var target = 5;
  123. target.loop(func (value){System.print("Hello World " + value);});
  124. }
  125. // Output:
  126. // Hello World 0
  127. // Hello World 1
  128. // Hello World 2
  129. // Hello World 3
  130. // Hello World 4
  131. </code></pre>
  132. <p>Loop within a <a href="types.html">Range</a>:</p>
  133. <pre><code class="swift">
  134. func main() {
  135. var target = 0...4;
  136. target.loop(func (value){System.print("Hello World " + value);});
  137. }
  138. // also in reverse order
  139. func main() {
  140. var target = 4...0;
  141. target.loop(func (value){System.print("Hello World " + value);});
  142. }
  143. </code></pre>
  144. <p>Loop within a <a href="lists.html">List</a>:</p>
  145. <pre><code class="swift">
  146. func main() {
  147. var target = [10,20,30,40,50,60,70,80,90];
  148. target.loop(func (value){System.print("Hello World " + value);});
  149. }
  150. </code></pre>
  151. <p>Loop within a <a href="maps.html">Map</a> where key is passed as closure argument (please note that key order is not preserved):</p>
  152. <pre><code class="swift">
  153. func main() {
  154. var target = ["key1":10,"key2":20,"key3":30,"key4":40];
  155. target.loop(func (key){System.print(key);});
  156. }
  157. // Output:
  158. // key1
  159. // key2
  160. // key4
  161. // key3
  162. </code></pre>
  163. </div>
  164. </div> <!-- /row -->
  165. </div> <!-- /main-content -->
  166. </div> <!-- /container -->
  167. <!-- BEGIN FOOTER -->
  168. <footer class="navbar-fixed-bottom">
  169. <div class="container footer">
  170. <p>
  171. </p>
  172. </div>
  173. </footer>
  174. <!-- END FOOTER -->
  175. <!-- Bootstrap JS -->
  176. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  177. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  178. <!-- Highlights JS -->
  179. <script src="scripts/highlight/highlight.min.js"></script>
  180. <script>hljs.initHighlightingOnLoad();</script>
  181. </body>
  182. </html>