123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Gravity: Loop</title>
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
- <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
- <link rel="stylesheet" href="stylesheets/styles.css">
- </head>
- <body>
- <!-- BEGIN NAVIGATION BAR -->
- <nav class="navbar navbar-default navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
- </div>
- <div id="navbar" class="navbar-collapse collapse">
- <ul class="nav navbar-nav navbar-right">
- <li class="active"><a href="index.html">Gravity</a></li>
- <li><a href="internals/index.html">Internals</a></li>
- <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
- </ul>
- </div><!--/.nav-collapse -->
- </div>
- </nav>
- <!-- END NAVIGATION BAR -->
- <div class="container body-container">
- <div class="main-content">
- <div class="row">
-
- <!-- BEGIN SIDEBAR -->
- <div class="col-sm-3 border-right section-left">
- <div saveheight="1" class="sidebar-nav">
- <h4>INTRODUCTION</h4>
- <ul>
- <li><a href="index.html"><span>Introduction</span></a></li>
- <li><a href="getting-started.html"><span>Getting Started</span></a></li>
- </ul>
- <h4>LANGUAGE GUIDE</h4>
- <ul>
- <li><a href="syntax.html"><span>Syntax</span></a></li>
- <li><a href="operators.html"><span>Operators</span></a></li>
- <li><a href="types.html"><span>Types</span></a></li>
- <li><a href="lists.html"><span>Lists</span></a></li>
- <li><a href="maps.html"><span>Maps</span></a></li>
- <li><a href="enum.html"><span>Enum</span></a></li>
- <li><a href="functions.html"><span>Functions</span></a></li>
- <li><a href="closures.html"><span>Closures</span></a></li>
- <li><a href="classes.html"><span>Classes</span></a></li>
- <li><a href="control-flow.html"><span>Control Flow</span></a></li>
- <li><a href="loops.html" class="active"><span>Loops</span></a></li>
- <li><a href="fibers.html"><span>Fibers</span></a></li>
- </ul>
- <h4>ADVANCED</h4>
- <ul>
- <li><a href="api.html"><span>Embedding API</span></a></li>
- <li><a href="system.html"><span>System class</span></a></li>
- <li><a href="unit-test.html"><span>Unit test</span></a></li>
- <li><a href="contributing.html"><span>Contributing</span></a></li>
- </ul>
- </div>
- </div>
- <!-- END SIDEBAR -->
- <div class="col-sm-9 border-left section-right">
- <h1 class="section-header">Loop</h1><hr>
-
- <p class="section-content">
-
- <h4 class="section-h4">While loop</h4>
- <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>
- <pre><code class="swift">
- func main() {
- var i = 0;
-
- while (i < 50000) {
- i += 1;
- }
-
- return i;
- }
- </code></pre>
-
- <h4 class="section-h4">Repeat-while loop</h4>
- <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.
- </p>
- <pre><code class="swift">
- func main() {
- var i = 0;
-
- repeat {
- i += 1;
- } while (i < 50000);
-
- return i;
- }
- </code></pre>
-
- <h4 class="section-h4">For loop</h4>
- <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>
- <pre><code class="swift">
- var count = 0;
- for (var i in 0...40) {
- count += i;
- }
- return count;
- </code></pre>
- <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>
-
- <h4 class="section-h4">Loop method</h4>
- <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>
- <pre><code class="swift">
- func main() {
- 4.loop({System.print("Hello World");});
- }
- // Output:
- // Hello World
- // Hello World
- // Hello World
- // Hello World
- </code></pre>
- <p>If we need to access the current index of the loop we can just rewrite the closure:</p>
- <pre><code class="swift">
- func main() {
- var target = 5;
- target.loop(func (value){System.print("Hello World " + value);});
- }
- // Output:
- // Hello World 0
- // Hello World 1
- // Hello World 2
- // Hello World 3
- // Hello World 4
- </code></pre>
- <p>Loop within a <a href="types.html">Range</a>:</p>
- <pre><code class="swift">
- func main() {
- var target = 0...4;
- target.loop(func (value){System.print("Hello World " + value);});
- }
- // also in reverse order
- func main() {
- var target = 4...0;
- target.loop(func (value){System.print("Hello World " + value);});
- }
- </code></pre>
- <p>Loop within a <a href="lists.html">List</a>:</p>
- <pre><code class="swift">
- func main() {
- var target = [10,20,30,40,50,60,70,80,90];
- target.loop(func (value){System.print("Hello World " + value);});
- }
- </code></pre>
- <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>
- <pre><code class="swift">
- func main() {
- var target = ["key1":10,"key2":20,"key3":30,"key4":40];
- target.loop(func (key){System.print(key);});
- }
- // Output:
- // key1
- // key2
- // key4
- // key3
- </code></pre>
- </div>
- </div> <!-- /row -->
- </div> <!-- /main-content -->
- </div> <!-- /container -->
- <!-- BEGIN FOOTER -->
- <footer class="navbar-fixed-bottom">
- <div class="container footer">
- <p>
- </p>
- </div>
- </footer>
- <!-- END FOOTER -->
-
- <!-- Bootstrap JS -->
- <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
-
- <!-- Highlights JS -->
- <script src="scripts/highlight/highlight.min.js"></script>
- <script>hljs.initHighlightingOnLoad();</script>
-
- </body>
- </html>
|