operators.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Gravity: Operators</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" class="active"><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"><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">Operators</h1><hr>
  73. <p class="section-content">
  74. An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator (+) adds two numbers, as in <strong>var i = 1 + 2</strong>, and the logical AND operator (&&) combines two Boolean values, as in <strong>if (flag1 && flag2)</strong>.
  75. <br><br>
  76. Gravity supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator (=) does not return a value, to prevent it from being mistakenly used when the equal to operator (==) is intended. Gravity also provides two <a href="types.html">range</a> operators as a shortcut for expressing a range of values.
  77. </p>
  78. <!-- ARITHMETIC -->
  79. <h4 class="section-h4">Arithmetic Operators</h4>
  80. <p>
  81. <ul>
  82. <li>Addition (+)</li>
  83. <li>Subtraction (-)</li>
  84. <li>Multiplication (*)</li>
  85. <li>Division (/)</li>
  86. <li>Remainder (%)</li>
  87. </ul>
  88. </p>
  89. <pre><code class="swift">
  90. var n1 = 1 + 2 // equals 3
  91. var n2 = 5 - 3 // equals 2
  92. var n3 = 2 * 3 // equals 6
  93. var n4 = 10.0 / 2.5 // equals 4.0
  94. var n5 = 9 % 4 // equals 1
  95. </code></pre>
  96. <p>Special attention has been placed in the division operator. A Division between two Int usually result in a Float in order to not truncate any decimal.</p>
  97. <!-- ASSIGNMENT -->
  98. <h4 class="section-h4">Assignment Operator</h4>
  99. <p>The assignment operator = initialize or update a value:</p>
  100. <pre><code class="swift">
  101. var a = 50; // a = 50
  102. var b = a; // b = 50
  103. var c = a * b; // c = 50 * 50
  104. </code></pre>
  105. <p>Please note that contrary to many other programming languages, the assignment operator has no side effect, it means that it does not return any value.</p>
  106. <!-- COMPARISON -->
  107. <h4 class="section-h4">Comparison Operators</h4>
  108. <p>The comparison operators return a Bool value to indicate whether or not the statement is true:</p>
  109. <p>
  110. <ul>
  111. <li>Equal (==)</li>
  112. <li>Not equal (!=)</li>
  113. <li>Less than (<)</li>
  114. <li>Less than or equal (<=)</li>
  115. <li>Greater than (>)</li>
  116. <li>Greater than or equal (>=)</li>
  117. <li>Identical (===)</li>
  118. <li>Not identical (!==)</li>
  119. <li>Type check (is)</li>
  120. <li>Pattern match (~=)</li>
  121. </ul>
  122. </p>
  123. <pre><code class="swift">
  124. 1 == 1 // true because 1 is equal to 1
  125. 1 != 2 // true because 1 is not equal to 2
  126. 1 < 2 // true because 1 is less than 2
  127. 1 <= 1 // true because 1 is less than or equal to 1
  128. 1 > 2 // false because 1 is not greater than 2
  129. 1 >= 1 // true because 1 is greater than or equal to 1
  130. 1 === 1 // true because 1 is identical to 1 (same value and same class)
  131. 1 is Int // true because 1 is of class Int
  132. </code></pre>
  133. <p>Gravity performs some conversions at runtime, so 1 == "1" but not 1 === '1'.</p>
  134. <!-- LOGICAL -->
  135. <h4 class="section-h4">Logical Operators</h4>
  136. <p>The comparison operators return a Bool value to indicate whether or not the statement is true:</p>
  137. <p>
  138. <ul>
  139. <li>Logical NOT (!)</li>
  140. <li>Logical AND (&&)</li>
  141. <li>Logical OR (||)</li>
  142. </ul>
  143. </p>
  144. <pre><code class="swift">
  145. !1 // false because 1 is true
  146. 1 && 0 // false because one of the two values is false
  147. 1 || 0 // true because one of the two values is true
  148. </code></pre>
  149. <p>In order to improve code readability the reserved keywords <strong>not, and, or</strong> has been introduces as an alisas to logical operators.</p>
  150. <!-- BITWISE -->
  151. <h4 class="section-h4">Bitwise Operators</h4>
  152. <p>
  153. <ul>
  154. <li>Bitwise shift left (<<)</li>
  155. <li>Bitwise shift right (>>)</li>
  156. <li>Bitwise AND (&)</li>
  157. <li>Bitwise OR (|)</li>
  158. <li>Bitwise XOR (^)</li>
  159. <li>Bitwise NOT or one's complement (~)</li>
  160. </ul>
  161. </p>
  162. <pre><code class="swift">
  163. var n = 0B00110011;
  164. var n1 = n << 2 // equals 11001100
  165. var n2 = n >> 2 // equals 00001100
  166. var n3 = n & 0B00001111 // equals 00000011
  167. var n4 = n | 0B00001111 // equals 00111111
  168. var n5 = n ^ 0B00001111 // equals 00111100
  169. var n6 = ~n; // equals 11001100
  170. </code></pre>
  171. <!-- COMPOUND -->
  172. <h4 class="section-h4">Compound Assignment Operators</h4>
  173. <p>As a shortcut, assignment and operators can be combined together:
  174. <ul>
  175. <li>Multiply and assign (*=)</li>
  176. <li>Divide and assign (/=)</li>
  177. <li>Remainder and assign (%=)</li>
  178. <li>Add and assign (+=)</li>
  179. <li>Subtract and assign (-=)</li>
  180. <li>Left bit shift and assign (<<=)</li>
  181. <li>Right bit shift and assign (>>=)</li>
  182. <li>Bitwise AND and assign (&=)</li>
  183. <li>Bitwise XOR and assign (^=)</li>
  184. <li>Bitwise OR and assign (|=)</li>
  185. </ul>
  186. </p>
  187. </div>
  188. </div> <!-- /row -->
  189. </div> <!-- /main-content -->
  190. </div> <!-- /container -->
  191. <!-- BEGIN FOOTER -->
  192. <footer class="navbar-fixed-bottom">
  193. <div class="container footer">
  194. <p>
  195. </p>
  196. </div>
  197. </footer>
  198. <!-- END FOOTER -->
  199. <!-- Bootstrap JS -->
  200. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  201. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  202. <!-- Highlights JS -->
  203. <script src="scripts/highlight/highlight.min.js"></script>
  204. <script>hljs.initHighlightingOnLoad();</script>
  205. </body>
  206. </html>