1<!DOCTYPE HTML> 2<html lang="en" class="sidebar-visible no-js light"> 3 <head> 4 <!-- Book generated using mdBook --> 5 <meta charset="UTF-8"> 6 <title>Programming Language Definition - Veetaha Website</title> 7 8 9 10 11 <!-- Custom HTML head --> 12 13 14 15 <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 16 <meta name="description" content=""> 17 <meta name="viewport" content="width=device-width, initial-scale=1"> 18 <meta name="theme-color" content="#ffffff" /> 19 20 21 <link rel="icon" href="favicon.svg"> 22 23 24 <link rel="shortcut icon" href="favicon.png"> 25 26 <link rel="stylesheet" href="css/variables.css"> 27 <link rel="stylesheet" href="css/general.css"> 28 <link rel="stylesheet" href="css/chrome.css"> 29 <link rel="stylesheet" href="css/print.css" media="print"> 30 31 <!-- Fonts --> 32 <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> 33 34 <link rel="stylesheet" href="fonts/fonts.css"> 35 36 37 <!-- Highlight.js Stylesheets --> 38 <link rel="stylesheet" href="highlight.css"> 39 <link rel="stylesheet" href="tomorrow-night.css"> 40 <link rel="stylesheet" href="ayu-highlight.css"> 41 42 <!-- Custom theme stylesheets --> 43 44 45 46 </head> 47 <body> 48 <!-- Provide site root to javascript --> 49
49<script type="text/javascript"> 50 var path_to_root = ""; 51 var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light"; 52 </script>
52 53 54 <!-- Work around some values being stored in localStorage wrapped in quotes --> 55
55<script type="text/javascript"> 56 try { 57 var theme = localStorage.getItem('mdbook-theme'); 58 var sidebar = localStorage.getItem('mdbook-sidebar'); 59 60 if (theme.startsWith('"') && theme.endsWith('"')) { 61 localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); 62 } 63 64 if (sidebar.startsWith('"') && sidebar.endsWith('"')) { 65 localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); 66 } 67 } catch (e) { } 68 </script>
68 69 70 <!-- Set the theme before any content is loaded, prevents flash --> 71
71<script type="text/javascript"> 72 var theme; 73 try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 74 if (theme === null || theme === undefined) { theme = default_theme; } 75 var html = document.querySelector('html'); 76 html.classList.remove('no-js') 77 html.classList.remove('light') 78 html.classList.add(theme); 79 html.classList.add('js'); 80 </script>
80 81 82 <!-- Hide / unhide sidebar before it is displayed --> 83
83<script type="text/javascript"> 84 var html = document.querySelector('html'); 85 var sidebar = 'hidden'; 86 if (document.body.clientWidth >= 1080) { 87 try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } 88 sidebar = sidebar || 'visible'; 89 } 90 html.classList.remove('sidebar-visible'); 91 html.classList.add("sidebar-" + sidebar); 92 </script>
92 93 94 <nav id="sidebar" class="sidebar" aria-label="Table of contents"> 95 <div class="sidebar-scrollbox"> 96 <ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Index</a></li><li class="chapter-item expanded "><a href="programming-language-definition.html" class="active"><strong aria-hidden="true">1.</strong> Programming Language Definition</a></li><li class="chapter-item expanded "><a href="impl-trait-parameters-and-turbofish.html"><strong aria-hidden="true">2.</strong> Impl Trait Parameters And Turbofish</a></li></ol> 97 </div> 98 <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div> 99 </nav> 100 101 <div id="page-wrapper" class="page-wrapper"> 102 103 <div class="page"> 104 105 <div id="menu-bar-hover-placeholder"></div> 106 <div id="menu-bar" class="menu-bar sticky bordered"> 107 <div class="left-buttons"> 108 <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> 109 <i class="fa fa-bars"></i> 110 </button> 111 <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> 112 <i class="fa fa-paint-brush"></i> 113 </button> 114 <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> 115 <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li> 116 <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> 117 <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> 118 <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> 119 <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> 120 </ul> 121 122 <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> 123 <i class="fa fa-search"></i> 124 </button> 125 126 </div> 127 128 <h1 class="menu-title">Veetaha Website</h1> 129 130 <div class="right-buttons"> 131 <a href="print.html" title="Print this book" aria-label="Print this book"> 132 <i id="print-button" class="fa fa-print"></i> 133 </a> 134 135 </div> 136 </div> 137 138 139 <div id="search-wrapper" class="hidden"> 140 <form id="searchbar-outer" class="searchbar-outer"> 141 <input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> 142 </form> 143 <div id="searchresults-outer" class="searchresults-outer hidden"> 144 <div id="searchresults-header" class="searchresults-header"></div> 145 <ul id="searchresults"> 146 </ul> 147 </div> 148 </div> 149 150 151 <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> 152
152<script type="text/javascript"> 153 document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); 154 document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); 155 Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { 156 link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); 157 }); 158 </script>
158 159 160 <div id="content" class="content"> 161 <main> 162 <h1><a class="header" href="#programming-language-definition" id="programming-language-definition">Programming Language Definition</a></h1> 163<p>Welcome! We are about to find out what programming languages are, and how they work. We are probably eager to start with a code example. To satisfy our whim here is one written in <a href="https://www.rust-lang.org/">Rust language</a>:</p> 164<pre><pre class="playground"><code class="language-rust">fn main() { 165 println!("Hello world"); 166} 167</code></pre></pre> 168<p>It is a classic program that prints the words "Hello world" to the terminal window.</p> 169<p>We are going to use Rust as our example programming language. However, there may be references and examples written in other languages further.</p> 170<h2><a class="header" href="#formal-vs-natural-language" id="formal-vs-natural-language">Formal vs Natural Language</a></h2> 171<p>What we've seen in the code example above is a <em>formal language</em> called Rust. We may wonder what the difference between a programming language and a human language like English is. That's a good question.</p> 172<p>First of all, we must understand that classic programming languages are text-based. I.e. all code that programmers write is just simple text, and all code editors are text editors as well.</p> 173<p>The main feature of programming language is that it is <em>formal</em>, which means that it is:</p> 174<ul> 175<li><strong>Unambiguous</strong> - each word of the language has a singular interpretation within the context where it is written. For example, the computer knows that <code>println!</code> instructs it to write to the console, and <code>"Hello world"</code> defines what should be printed. The rules of language's syntax, grammar, and semantics fully define what alphabet the language uses, what punctuation, keywords, and other symbols are valid, and in what order. From this follows the next feature.</li> 176<li><strong>Strict</strong> - the language disallows having grammar mistakes (e.g. a misspelled word or wrong punctuation). If we have any mistakes in our code, we'll see errors when running our program.</li> 177<li><strong>Little redundancy</strong> - the language is very laconic. It doesn't have redundant constructions that are not necessary for understanding. For example, the word <code>fn</code> in the code snippet shown at the beginning stands for "function". The language designers decided to shorten it because it is a widely used keyword that we would like to spend less time typing. Other than that we wouldn't write <code>The fn main</code> in a programming language, because articles are usually considered to be a redundancy of a natural language, so we omit <code>a/an/the</code> in code to keep it succinct.</li> 178</ul> 179<h2><a class="header" href="#purpose" id="purpose">Purpose</a></h2> 180<p>Programming languages are mostly used to write applications running on different platforms (servers, PC, Laptops, mobile phones, etc.).</p> 181<p>We already know what programs are capable of (web browser, file browser, games, torrent, etc). We can make the computer do almost anything we want, but before we harness the full power we will begin with the basics. Knowing just the basics may make us feel like a child playing in a sandbox, but this groundwork is of the highest importance to building the ground in any language.</p> 182<h2><a class="header" href="#general-purpose-programming-language-structure" id="general-purpose-programming-language-structure">General-Purpose Programming Language Structure</a></h2> 183<h3><a class="header" href="#direction" id="direction">Direction</a></h3> 184<p>Classic programming languages are written in the usual direction just like natural languages. We should read them left-to-right and top-to-bottom. Regular programming languages like Rust, TypeScript, C/C++, C#, Java, Kotlin, Python, etc. share a similar structure.</p> 185<h3><a class="header" href="#comments" id="comments">Comments</a></h3> 186<p>To address an elephant in the room we'll begin with what makes our code readable by letting programmers insert snippets of their natural language (99% of the time the conventional language is English) that explain what happens in the formal language around them. They are called comments, and they are completely ignored by a computer when it runs a program.</p> 187<p>Different languages have different syntax for comments, but the most frequent syntax is to use <code>//</code> (Rust, C/C++, Java...) or <code>
187#</code> (Python, Ruby, YAML) symbol to denote the beginning of the comment.</p> 188<p>The comment doesn't have to start on a new line. Any occurrence of <code>//</code> or <code>#</code> denotes the beginning of our natural language prattle. Such a comment is called a single-line comment because it finishes only at the end of the line.</p> 189<pre><pre class="playground"><code class="language-rust"> 190<span class="boring">#![allow(unused)] 191</span><span class="boring">fn main() { 192</span>// Blah, bruh, this code is dope, just look at this =) 193println!("Hello world"); 194 195println!("Twigly"); // Comments can also be written after real code 196<span class="boring">} 197</span></code></pre></pre> 198<h3><a class="header" href="#whitespace" id="whitespace">Whitespace</a></h3> 199<p>Most of the languages ignore redundant whitespace. For example, the following 4 programs written in Rust are completely identical in what they do.</p> 200<pre><pre class="playground"><code class="language-rust"> 201<span class="boring">#![allow(unused)] 202</span><span class="boring">fn main() { 203</span>println!("Hello World"); 204<span class="boring">} 205</span></code></pre></pre> 206<pre><pre class="playground"><code class="language-rust"> 207<span class="boring">#![allow(unused)] 208</span><span class="boring">fn main() { 209</span>println!( "Hello world" ); 210<span class="boring">} 211</span></code></pre></pre> 212<pre><pre class="playground"><code class="language-rust"> 213<span class="boring">#![allow(unused)] 214</span><span class="boring">fn main() { 215</span>println!( "Hello world" ) 216; 217<span class="boring">} 218</span></code></pre></pre> 219<pre><pre class="playground"><code class="language-rust"> 220<span class="boring">#![allow(unused)] 221</span><span class="boring">fn main() { 222</span>println!( 223 224 "Hello world" 225 226 ) 227 228 ; 229<span class="boring">} 230</span></code></pre></pre> 231<p>We may recall that formal languages were said to be 'Little redundant'. This "little" is what still allows for exceptional trivial redundancy like this. However, it makes writing code easier and more flexible for humans. In some cases, extra whitespace makes our code prettier.</p> 232<p>For example when formatting a long list of things we would like to write it across several lines, disregarding the fact that it would make the text of the program longer.</p> 233<blockquote> 234<p>Good programmers strive to write their code in a way that it would be easier to read and understand by other people!</p> 235</blockquote> 236<p>E.g. this code is hard to read because it is all on one line.</p> 237<pre><pre class="playground"><code class="language-rust"> 238<span class="boring">#![allow(unused)] 239</span><span class="boring">fn main() { 240</span>let vocabulary = ["Programming", "Rust", "Language", "Niko", "Veetaha", "Dictionary", "List", "Array", "Blackjack", "Morning Glory", "Rampage", "P21", "Project", "Horizons", "Octavia", "Vynil", "University", "Days"]; 241<span class="boring">} 242</span></code></pre></pre> 243<p>This code is easier to read, even though it takes much more screen space, just because human's perception of smaller lines of text is better than the longer ones.</p> 244<pre><pre class="playground"><code class="language-rust"> 245<span class="boring">#![allow(unused)] 246</span><span class="boring">fn main() { 247</span>let vocabulary = [ 248 "Programming", 249 "Rust", 250 "Language", 251 "Niko", 252 "Veetaha", 253 "Dictionary", 254 "List", 255 "Array", 256 // When code is written vertically it's easier to add comments to it 257 "Blackjack", 258 "Morning Glory", 259 "Rampage", 260 "P21", 261 "Project", 262 "Horizons", 263 264 // We can even use whitespace to group related parts of code together visually 265 "Octavia", 266 "Vynil", 267 "University", 268 "Days" 269]; 270<span class="boring">} 271</span></code></pre></pre> 272<p>Languages like Python or Ruby give programmers less freedom in how they can use whitespace. However, this way they reduce the number of punctuation symbols in their language (e.g. <code>;</code>).</p> 273<h3><a class="header" href="#expressions" id="expressions">Expressions</a></h3> 274<p>Expressions are parts of the code that evaluate to some value. They are highly composite, and it is very important to understand how expressions compose into an expression tree and how the computer computes them.</p> 275<h4><a class="header" href="#literals" id="literals">Literals</a></h4> 276<p>The simplest expression is just a <em>literal</em> value</p> 277<pre><pre class="playground"><code class="language-rust"> 278<span class="boring">#![allow(unused)] 279</span><span class="boring">fn main() { 280</span>99
281<span class="boring">; 282</span><span class="boring">} 283</span></code></pre></pre> 284<pre><pre class="playground"><code class="language-rust"> 285<span class="boring">#![allow(unused)] 286</span><span class="boring">fn main() { 287</span>"Hello world" 288<span class="boring">; 289</span><span class="boring">} 290</span></code></pre></pre> 291<pre><pre class="playground"><code class="language-rust"> 292<span class="boring">#![allow(unused)] 293</span><span class="boring">fn main() { 294</span>true 295<span class="boring">; 296</span><span class="boring">} 297</span></code></pre></pre> 298<p>These values are the atoms of our LEGO. They are called <strong>literals</strong>.</p> 299<h4><a class="header" href="#mathematical-expressions" id="mathematical-expressions">Mathematical expressions</a></h4> 300<p>Mathematical expressions are the most widely-known kind of expressions:</p> 301<pre><pre class="playground"><code class="language-rust"> 302<span class="boring">#![allow(unused)] 303</span><span class="boring">fn main() { 304</span>2 + 2 305<span class="boring">; 306</span><span class="boring">} 307</span></code></pre></pre> 308<p>All expressions like LEGO pieces are composed of smaller LEGO pieces. And this relationship is best described with a tree.</p> 309<p>For instance, a computer will build the following expression tree for a simple <code>2 + 2</code>.</p> 310<p><img src="https://user-images.githubusercontent.com/36276403/162851904-e65e6952-94fb-40fc-94f2-dd5372045fed.jpg" alt="" /></p> 311<p>The tree is then evaluated from the bottom to the top. Mathematical operators always sit in the middle of the tree and literal values are always at the bottom.</p> 312<p>To compute the result of this tree the computer will begin from the bottom and look at the left <code>2</code>.</p> 313<p><img src="https://user-images.githubusercontent.com/36276403/162851349-6654bd2d-dc57-4f69-a6f0-2a889fc2be44.jpg" alt="" /></p> 314<p>Since that value is already known (it is <em>literally</em> <code>2</code> duh...). The computer will look at the right branch of the tree, which is also <code>2</code>.</p> 315<p><img src="https://user-images.githubusercontent.com/36276403/162851348-7fa08aef-4d38-47a6-b76d-617f9c652561.jpg" alt="" /></p> 316<p>Then it will conclude that the parameters of the plus (<code>+</code>) operator are known, so it can proceed with getting the sum of them, outputting <code>4</code> at the end.</p> 317<p><img src="https://user-images.githubusercontent.com/36276403/162851347-153be3fb-e598-467f-b5bc-959859b2ea56.jpg" alt="" /></p> 318<p>Let's take another example</p> 319<pre><pre class="playground"><code class="language-rust"> 320<span class="boring">#![allow(unused)] 321</span><span class="boring">fn main() { 322</span>(2 + 6) * 9 / 3 > 46 323<span class="boring">; 324</span><span class="boring">} 325</span></code></pre></pre> 326<p>We can see how mathematical expressions can grow in complexity without bounds. What's important is to understand how a computer gets the result of an expression. Our tree representation can scale to this arbitrarily complex math expression. The computer builds the following tree for it:</p> 327<p><img src="https://user-images.githubusercontent.com/36276403/162851346-91a5e2a8-df04-443f-956d-ee77d5e24f55.jpg" alt="" /></p> 328<p>We've already seen how the computer copes with a simple <code>2 + 2</code>. Similarly, it evaluates <code>2 + 6</code> here to <code>8</code>:</p> 329<p><img src="https://user-images.githubusercontent.com/36276403/162851342-e1d8ff5d-d973-4695-8940-dc5f576acf6a.jpg" alt="" /></p> 330<p>We can see that parentheses have a single parameter that they evaluate to directly. From a semantics standpoint, the parentheses operator is used only for grouping parts of the expression tree that have lower operator precedence to give them a higher one. The operator precedence should be known to us from the school (e.g. <code>*</code> goes before <code>+</code>). So we just replace the parenthesis operator with the value of its argument.</p> 331<p><img src="https://user-images.githubusercontent.com/36276403/162851361-a8d0cfc0-4767-4abd-b42d-757f7539708d.jpg" alt="" /></p> 332<p>Then the computer tries to evaluate the right branch of multiplication (<code>*</code>), but since that branch is also a nested expression, it follows to evaluate that from the bottom too begging with the literal value <code>9</code> that is already known.</p> 333<p><img src="https://user-images.githubusercontent.com/36276403/162851359-295160f4-1465-4436-a545-3689edd7f803.jpg" alt="" /></p> 334<p>Then it continues with the right branch of that subexpression and experiences the literal value <code>3</code> that is also known.</p> 335<p><img src="https://user-images.githubusercontent.com/36276403/162851593-7216e9e9-3511-426d-a103-ca96a00c2616.jpg" alt="" /></p> 336<p>Likewise, the operator <code>/</code> has fully-evaluated values of parameters and the result is substituted with <code>3</code>.</p> 337<p><img src="https://user-images.githubusercontent.com/36276403/162851597-eeabdde1-e366-476c-a7c6-acf59d7b7410.jpg" alt="" /></p> 338<p>
338Now we can see where it goes. Operator <code>*</code> has fully evaluated values of parameters and the result is substituted with <code>24</code>.</p> 339<p><img src="https://user-images.githubusercontent.com/36276403/162851599-f056d9e5-6cff-4fcb-8910-8fa1b8fcd82f.jpg" alt="" /></p> 340<p>But we are not done yet! The computer must evaluate the right branch of the expression tree. We as humans see that the value is already <code>46</code>, but the computer doesn't have eyesight, so it reads whatever is in the right branch to confirm that.</p> 341<p><img src="https://user-images.githubusercontent.com/36276403/162851605-f6035cda-258f-4b93-a283-e81ccf314952.jpg" alt="" /></p> 342<p>Now the result of the expression is substituted with the result of a logical comparison operation. <code>24</code> is not greater than (<code>></code>) <code>46</code>, so <code>false</code> is returned.</p> 343<p><img src="https://user-images.githubusercontent.com/36276403/162851733-9cdc5713-0e6c-4385-8793-165d748e4a9f.jpg" alt="" /></p> 344<h4><a class="header" href="#arbitrary-expressions" id="arbitrary-expressions">Arbitrary expressions</a></h4> 345<p>As we can see computers work not only with numbers, they work with values of different types including strings, booleans, lists, dictionaries (maps), etc. All of them also have their representation on the expression tree.</p> 346<p>For example, suppose we have a dictionary and we would like to get the value of the key <code>"blackjack"</code> from it.</p> 347<pre><code class="language-rust ignore">dict["blackjack"] 348</code></pre> 349<p>The expression tree for this would be</p> 350<p><img src="https://user-images.githubusercontent.com/36276403/162853769-64176e02-9633-45ac-98a4-aab7936d4cc3.jpg" alt="" /></p> 351<p>If we want to call a function <code>print</code> that would output the value of the dictionary we would do it this way:</p> 352<pre><code class="language-rust ignore">print(dict["blackjack"]) 353</code></pre> 354<p>resulting in the following expression tree:</p> 355<p><img src="https://user-images.githubusercontent.com/36276403/162854376-76ca0ba2-ddc7-4d45-ba8b-4b873ebadaba.jpg" alt="" /></p> 356<h3><a class="header" href="#statements" id="statements">Statements</a></h3> 357<p>Statements are the next level of LEGO. They consist of expressions.</p> 358<p>A statement usually occupies a single line, but it can span multiple lines (except for Python-like languages where they are single-line), and it is delimited with a semicolon (<code>;</code>).</p> 359<pre><pre class="playground"><code class="language-rust"> 360<span class="boring">#![allow(unused)] 361</span><span class="boring">fn main() { 362</span>let blackjack = 42; 363 364println!("Hello world"); 365 366std::process::exit(0); 367<span class="boring">} 368</span></code></pre></pre> 369<p>Statements usually represent the execution of a single command. They are similar to sentences in natural languages. 370For example, if we translate the code written in Rust language above to English we would get:</p> 371<pre><code class="language-md">Initialize a variable called blackjack with the integer value 42. 372 373Output the string "Hello world" to the terminal. 374 375Shut down the application with the status code of zero. 376</code></pre> 377<p>One difference, as well as the similarity, is that statements have a semicolon at the end, but sentences in English finish with a dot (<code>.</code>) or a question mark (<code>?</code>) or an exclamation point (<code>!</code>).</p> 378<h3><a class="header" href="#special-kinds-of-statements" id="special-kinds-of-statements">Special Kinds of Statements</a></h3> 379<p>A semicolon-delimited statement is the most frequent kind of statement we will see in any code. There exist also special kinds of statements that build on top of the regular statement allowing us to compose our program like a LEGO figurine.</p> 380<p>There are a bunch of them that can be found in many programming languages:</p> 381<ul> 382<li>Conditional statement (e.g. <code>if</code> statement)</li> 383<li>Loop statement (e.g. <code>while/for</code> statement)</li> 384</ul> 385<pre><pre class="playground"><code class="language-rust"> 386<span class="boring">#![allow(unused)] 387</span><span class="boring">fn main() { 388</span>if 2 + 2 == 4 { 389 println!("Yes that's true!"); 390} 391 392for counter in 0..5 { 393 println!("Counter value: {counter}"); 394} 395 396while std::io::stdin().lines().next().is_some() { 397 println!("You've input a line!"); 398 println!("Reading your next input..."); 399} 400<span class="boring">} 401</span></code></pre></pre> 402<p>They aren't usually delimited by a semicolon, and they create irregular execution paths (conditional execution, loops). 403We can see how they are composed with regular statements. They usually delimit a block of code making the regular statements inside of them be written after a small whitespace gap (indentation). The more statements we embed in them, the more nested our
403code looks like:</p> 404<pre><pre class="playground"><code class="language-rust"> 405<span class="boring">#![allow(unused)] 406</span><span class="boring">fn main() { 407</span>if 2 + 2 == 4 { 408 if 4 + 4 == 8 { 409 if 2 * 2 == 4 { 410 println!("All checks passed!"); 411 } else { 412 println!("Last check didn't pass..."); 413 } 414 } 415} 416<span class="boring">} 417</span></code></pre></pre> 418<h3><a class="header" href="#variables" id="variables">Variables</a></h3> 419<p>Variables are a little bit different in different programming languages. Here are examples of how we would create a variable in different programming languages:</p> 420<p>Rust uses the introducer keyword <code>let</code> to distinguish between variable declarations and reassignments.</p> 421<pre><pre class="playground"><code class="language-rust"> 422<span class="boring">#![allow(unused)] 423</span><span class="boring">fn main() { 424</span>let variable = "Hello world"; 425<span class="boring">} 426</span></code></pre></pre> 427<p>Python doesn't use an introducer keyword. This however increases the chance that a programmer creates a variable with a new name instead of reassigning it to the existing variable if they make a typo in the variable name.</p> 428<pre><code class="language-python">variable = "Hello world"; 429 430# for example suppose we want to reassign to `variable` 431# but we make a typo here `voriable` 432voriable = "Equestria" 433 434# This program doesn't work as expected... 435print(variable) 436</code></pre> 437<p>Java uses an introducer type annotation for the variable</p> 438<pre><code class="language-java">String variable = "Hello world"; 439 440// With introducer syntax there is no chance we could make a typo 441// This will result in an error when building our application, 442// because such a variable wasn't previously declared 443voriable = "Equestria"; 444</code></pre> 445<p>We can use variables in place of literal values. Their main purpose is to serve as storage for intermediate calculations as well as to reduce the repetition of literal values in a program.</p> 446<h2><a class="header" href="#conclusion" id="conclusion">Conclusion</a></h2> 447<p>These were the most common parts of any language. Once we understand how they work, we will be able to learn any classic programming language with ease, because they differ only in small syntactic peculiarities, which are trivial to study.</p> 448<h2><a class="header" href="#references" id="references">References</a></h2> 449<ul> 450<li><a href="https://disted.edu.vn.ua/courses/learn/579">ФоÑмалÑÐ½Ñ Ñ Ð¿ÑиÑÐ¾Ð´Ð½Ñ Ð¼Ð¾Ð²Ð¸</a></li> 451</ul> 452<p><em>2022-04-12</em></p> 453 454 </main> 455 456 <nav class="nav-wrapper" aria-label="Page navigation"> 457 <!-- Mobile navigation buttons --> 458 459 <a rel="prev" href="index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> 460 <i class="fa fa-angle-left"></i> 461 </a> 462 463 464 465 <a rel="next" href="impl-trait-parameters-and-turbofish.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> 466 <i class="fa fa-angle-right"></i> 467 </a> 468 469 470 <div style="clear: both"></div> 471 </nav> 472 </div> 473 </div> 474 475 <nav class="nav-wide-wrapper" aria-label="Page navigation"> 476 477 <a rel="prev" href="index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> 478 <i class="fa fa-angle-left"></i> 479 </a> 480 481 482 483 <a rel="next" href="impl-trait-parameters-and-turbofish.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> 484 <i class="fa fa-angle-right"></i> 485 </a> 486 487 </nav> 488 489 </div> 490 491 492 493 494 495 496 497 498
498<script type="text/javascript"> 499 window.playground_copyable = true; 500 </script>
500 501 502 503 504 505 506
506<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
506 507
507<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
507 508
508<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
508 509 510 511
511<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
511 512
512<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
512 513
513<script src="book.js" type="text/javascript" charset="utf-8"></script>
513 514 515 <!-- Custom JS scripts --> 516 517 518 519 520 </body> 521</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.