-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.html
3388 lines (3304 loc) · 167 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.23">
<title>Nim basics</title>
<link rel="stylesheet" href="./styles/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./rouge-gruvbox.css">
</head>
<body class="book toc2 toc-left">
<div id="header">
<h1>Nim basics</h1>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_who_is_this_for">Who is this for?</a></li>
<li><a href="#_who_is_this_not_for">Who is this <em>not</em> for?</a></li>
<li><a href="#_how_to_use_this_tutorial">How to use this tutorial?</a></li>
<li><a href="#_installation">Installation</a>
<ul class="sectlevel1">
<li><a href="#_installing_nim">Installing Nim</a></li>
<li><a href="#_installing_additional_tools">Installing additional tools</a></li>
<li><a href="#_testing_the_installation">Testing the installation</a></li>
</ul>
</li>
<li><a href="#_naming_values">Naming values</a>
<ul class="sectlevel1">
<li><a href="#_variable_declaration">Variable declaration</a></li>
<li><a href="#_immutable_assignment">Immutable assignment</a>
<ul class="sectlevel2">
<li><a href="#_const">Const</a></li>
<li><a href="#_let">Let</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_basic_data_types">Basic data types</a>
<ul class="sectlevel1">
<li><a href="#_integers">Integers</a></li>
<li><a href="#_floats">Floats</a>
<ul class="sectlevel2">
<li><a href="#_converting_floats_and_integers">Converting floats and integers</a></li>
</ul>
</li>
<li><a href="#_characters">Characters</a></li>
<li><a href="#_strings">Strings</a>
<ul class="sectlevel2">
<li><a href="#_special_characters">Special characters</a></li>
<li><a href="#_string_concatenation">String concatenation</a></li>
</ul>
</li>
<li><a href="#_boolean">Boolean</a>
<ul class="sectlevel2">
<li><a href="#_relational_operators">Relational operators</a></li>
<li><a href="#_logical_operators">Logical operators</a></li>
</ul>
</li>
<li><a href="#_recap">Recap</a></li>
<li><a href="#_exercises">Exercises</a></li>
</ul>
</li>
<li><a href="#_control_flow">Control flow</a>
<ul class="sectlevel1">
<li><a href="#_if_statement">If statement</a>
<ul class="sectlevel2">
<li><a href="#_else">Else</a></li>
<li><a href="#_elif">Elif</a></li>
</ul>
</li>
<li><a href="#_case">Case</a></li>
</ul>
</li>
<li><a href="#_loops">Loops</a>
<ul class="sectlevel1">
<li><a href="#_for_loop">For loop</a></li>
<li><a href="#_while_loop">While loop</a></li>
<li><a href="#_break_and_continue">Break and continue</a></li>
<li><a href="#_exercises_2">Exercises</a></li>
</ul>
</li>
<li><a href="#_containers">Containers</a>
<ul class="sectlevel1">
<li><a href="#_arrays">Arrays</a></li>
<li><a href="#_sequences">Sequences</a></li>
<li><a href="#_indexing_and_slicing">Indexing and slicing</a></li>
<li><a href="#_tuples">Tuples</a></li>
<li><a href="#_exercises_3">Exercises</a></li>
</ul>
</li>
<li><a href="#_procedures">Procedures</a>
<ul class="sectlevel1">
<li><a href="#_declaring_a_procedure">Declaring a procedure</a></li>
<li><a href="#_calling_the_procedures">Calling the procedures</a></li>
<li><a href="#_result_variable">Result variable</a></li>
<li><a href="#_forward_declaration">Forward declaration</a></li>
<li><a href="#_exercises_4">Exercises</a></li>
</ul>
</li>
<li><a href="#_modules">Modules</a>
<ul class="sectlevel1">
<li><a href="#_importing_a_module">Importing a module</a></li>
<li><a href="#_creating_our_own">Creating our own</a></li>
</ul>
</li>
<li><a href="#_interacting_with_user_input">Interacting with user input</a>
<ul class="sectlevel1">
<li><a href="#_reading_from_a_file">Reading from a file</a></li>
<li><a href="#_reading_user_input">Reading user input</a></li>
<li><a href="#_dealing_with_numbers">Dealing with numbers</a></li>
<li><a href="#_exercises_5">Exercises</a></li>
</ul>
</li>
<li><a href="#_conclusion">Conclusion</a>
<ul class="sectlevel1">
<li><a href="#_next_steps">Next steps</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://nim-lang.org/">Nim</a> is a relatively new programming language which allows users to write easy-to-read high-performance code.
But if you are reading this Nim tutorial, the chances are that you already know about Nim.</p>
</div>
<div class="paragraph">
<p>The tutorial is available both <a href="https://narimiran.github.io/nim-basics/">online</a> and as a <a href="https://github.com/narimiran/nim-basics/raw/master/nim-basics.epub">book in epub format</a>.</p>
</div>
<div class="paragraph">
<p>This is a work-in-progress: if you spot any errors and/or you have an idea how to make this tutorial better, please report it to the <a href="https://github.com/narimiran/nim-basics/issues">issue tracker</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_who_is_this_for">Who is this for?</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p>People with no or minimal previous programming experience</p>
</li>
<li>
<p>People with some programming experience in other programming languages</p>
</li>
<li>
<p>People who want to explore Nim for the first time, starting from scratch</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_who_is_this_not_for">Who is this <em>not</em> for?</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p>People with lots of programming experience: other, more advanced, tutorials might suit you better.
See <a href="https://nim-lang.org/docs/tut1.html">Official Tutorial</a> or <a href="https://nim-by-example.github.io/">Nim by Example</a>.</p>
</li>
<li>
<p>People experienced in Nim (feel free to help make this tutorial better)</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_how_to_use_this_tutorial">How to use this tutorial?</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The aim of this tutorial is to give you the basics of programming and the Nim syntax so you can have an easier time following other tutorials and/or explore further by yourself.</p>
</div>
<div class="paragraph">
<p>Instead of just reading what is written, it would be the best if you try the stuff by yourself, modify the examples, think of some examples of your own, and be curious in general.
The exercises at the end of some chapters should be non-negotiable — don’t skip them.</p>
</div>
<div class="paragraph">
<p>If you need additional help understanding some parts of the tutorial or with the exercises, you can always ask for help on the <a href="https://forum.nim-lang.org/">Nim forum</a>, the <a href="https://gitter.im/nim-lang/Nim">Nim Gitter channel</a>, their <a href="https://discordapp.com/invite/ezDFDw2">Discord server</a>, or Nim’s IRC channel on freenode, #nim.</p>
</div>
</div>
</div>
<h1 id="_installation" class="sect0">Installation</h1>
<div class="sect1">
<h2 id="_installing_nim">Installing Nim</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Nim has ready made distributions for all three major operating systems and there are several options when it comes to installing Nim.</p>
</div>
<div class="paragraph">
<p>You can follow <a href="https://nim-lang.org/install.html">the official installation procedure</a> to install the latest stable version, or you can use a tool called <a href="https://github.com/nim-lang/choosenim">choosenim</a> which enables you to easily switch between the stable and the latest development version if you’re interested in the latest features and bugfixes.</p>
</div>
<div class="paragraph">
<p>Whichever way you choose, just follow the installation procedure explained at each link and Nim should be installed.
We will check that the installation went well in a coming chapter.</p>
</div>
<div class="paragraph">
<p>If you’re using Linux, there is a high probability that your distribution has Nim in the package manager.
If you are installing it that way, make sure it’s the most recent version (see the website for what is the latest version), otherwise install via one of two methods mentioned above.</p>
</div>
<div class="paragraph">
<p>In this tutorial we will use the stable version.
Originally, this tutorial was written for Nim 0.19 (released in September 2018), and it should work for any newer version, including Nim 1.x and Nim 2.x.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_installing_additional_tools">Installing additional tools</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can write Nim code in any text editor, and then compile and run it from the terminal.
If you want syntax highlighting and code completion there are plugins for popular code editors which provide these features.</p>
</div>
<div class="paragraph">
<p>Most of Nim users prefer the <a href="https://code.visualstudio.com/">VS Code</a> editor, with the <a href="https://marketplace.visualstudio.com/items?itemName=NimLang.nimlang">Nim extension</a> which provides syntax highlighting and code completion, and the <a href="https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner">Code Runner extension</a> for quick compiling and running.</p>
</div>
<div class="paragraph">
<p>The author personally uses <a href="https://neovim.io/">NeoVim</a> editor, with <a href="https://github.com/alaviss/nim.nvim">this plugin</a> which provides additional features like syntax highlighting and code completion.</p>
</div>
<div class="paragraph">
<p>If you’re using other code editors, see <a href="https://github.com/nim-lang/Nim/wiki/Editor-Support">the wiki</a> for available editor support.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_testing_the_installation">Testing the installation</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To check if the installation was successful, we will write a program which is traditionally used as an introductory example: <a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">Hello World</a>.</p>
</div>
<div class="paragraph">
<p>Printing (as in: displaying on the screen; not on a paper with a printer) the phrase <code>Hello World!</code> in Nim is straightforward and it doesn’t require any boilerplate code.</p>
</div>
<div class="paragraph">
<p>In a new text file called e.g. <code>helloworld.nim</code> we need to write just one line of code:</p>
</div>
<div class="listingblock">
<div class="title">helloworld.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="n">echo</span> <span class="s">"Hello World!"</span></code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The phrase you want to print must follow the <code>echo</code> command and must be enclosed in double-quotes (<code>"</code>).
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>First we need to compile our program, and then run it to see if it works as expected.</p>
</div>
<div class="paragraph">
<p>Open your terminal in the same directory where your file is (on Linux you can get "Open Terminal here" if you right-click the directory in your file manager, on Windows you should use Shift + right-click to get the menu option for opening the command line).</p>
</div>
<div class="paragraph">
<p>We compile our program by typing in the terminal:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="terminal"><span class="go">nim c helloworld.nim</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>After a successful compilation, we can run our program.
On Linux we can run our program by typing <code>./helloworld</code> in the terminal, and on Windows we do it by typing <code>helloworld.exe</code>.</p>
</div>
<div class="paragraph">
<p>There is also a possibility to both compile and run the program with just one command.
We need to type:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="terminal"><span class="go">nim c -r helloworld.nim</span></code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<code>c</code> is telling Nim to compile the file, and <code>-r</code> is telling it to run it immediately.<br>
To see all compiler options, type <code>nim --help</code> in your terminal.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>If you’re using VSCode with the Code Runner extension mentioned before, you’ll just have to press <code>Ctrl+Alt+N</code> and your file will be compiled and run.</p>
</div>
<div class="paragraph">
<p>Whichever way you chose to run your program, after a brief moment in the output window (or in your terminal) you should see:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">Hello World!</code></pre>
</div>
</div>
<div class="paragraph">
<p>Congratulations, you have successfully run your first Nim program!</p>
</div>
<div class="paragraph">
<p>Now you know how to print some stuff on the screen (using the <code>echo</code> command), compile your program (typing <code>nim c programName.nim</code> in your terminal), and run it (various possibilities).</p>
</div>
<div class="paragraph">
<p>We can now start to explore the basic elements which will help us to write simple Nim programs.</p>
</div>
</div>
</div>
<h1 id="_naming_values" class="sect0">Naming values</h1>
<div class="openblock partintro">
<div class="content">
<div class="paragraph">
<p>It is often helpful to give the values in our programs names to help us keep track of things.
If we ask a user for his/her name, we want to store it for the later usage, without asking for it again and again every time we need to do some computation with it.</p>
</div>
<div class="paragraph">
<p>In the example <code>pi = 3.14</code>, the name <code>pi</code> is connected to the value <code>3.14</code>.
From our experience, we can tell that the type of a variable <code>pi</code> is a (decimal) number.</p>
</div>
<div class="paragraph">
<p>Another example would be <code>firstName = Alice</code>, where <code>firstName</code> is the name of a variable with the value <code>Alice</code>.
We would say that the type of this variable is a word.</p>
</div>
<div class="paragraph">
<p>In programming languages this works similarly.
These name assignments have their <em>name</em>, the <em>value</em>, and a <em>type</em>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_variable_declaration">Variable declaration</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Nim is a <a href="https://en.wikipedia.org/wiki/Type_system#STATIC">statically typed</a> programming language, meaning that the type of an assignment needs to be declared before using the value.</p>
</div>
<div class="paragraph">
<p>In Nim we also distinguish values that can change, or mutate, from those that can’t, but more on this later.
We can declare a variable (a mutable assignment) using the <code>var</code> keyword, just by stating its name and type (the value can be added later) by using this syntax:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <span class="o"><</span><span class="n">name</span><span class="o">></span><span class="p">:</span> <span class="o"><</span><span class="k">type</span><span class="o">></span></code></pre>
</div>
</div>
<div class="paragraph">
<p>If we already know its value, we can declare a variable and give it a value immediately:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <span class="o"><</span><span class="n">name</span><span class="o">></span><span class="p">:</span> <span class="o"><</span><span class="k">type</span><span class="o">></span> <span class="o">=</span> <span class="o"><</span><span class="n">value</span><span class="o">></span></code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Angular brackets(<code><></code>) are used to show something you can change.<br>
So <code><name></code> is not literally the word <code>name</code> in angular brackets but rather any name.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Nim also has <a href="https://en.wikipedia.org/wiki/Type_inference">type inference</a> ability: the compiler can automatically detect the type of a name assignment from its value, without explicitly stating the type.
We’ll look more into the various types in the <a href="#_basic_data_types">next chapter</a>.</p>
</div>
<div class="paragraph">
<p>So we can assign a variable without an explicit type like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <span class="o"><</span><span class="n">name</span><span class="o">></span> <span class="o">=</span> <span class="o"><</span><span class="n">value</span><span class="o">></span></code></pre>
</div>
</div>
<div class="paragraph">
<p>An example of this in Nim looks like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <span class="n">a</span><span class="p">:</span> <span class="kt">int</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="k">var</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">7</span> <i class="conum" data-value="2"></i><b>(2)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Variable <code>a</code> is of type <code>int</code> (integer) with no value explicitly set.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Variable <code>b</code> has a value of <code>7</code>. Its type is automatically detected as an integer.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>When assigning names it is important to choose names that mean something for your program.
Simply naming them <code>a</code>, <code>b</code>, <code>c</code>, and so forth will quickly become confusing.
It is not possible to use spaces in a name, as that would split it into two.
So if the name you choose consists of more than one word the usual way is to write it in <code>camelCase</code> style (notice that the first letter in a name should be lowercase).</p>
</div>
<div class="paragraph">
<p>Note however that Nim is both case- and underscore-insensitive meaning that <code>helloWorld</code> and <code>hello_world</code> would be the same name.
The exception to this is the first character, which <em>is</em> case-sensitive.
Names can also include both numbers and other UTF-8 characters, even emojis should you wish that, but keep in mind you and possibly others will have to type them.</p>
</div>
<div class="paragraph">
<p> </p>
</div>
<div class="paragraph">
<p>Instead of typing <code>var</code> for each variable, multiple variables (not necessarily of the same type) can be declared in the same <code>var</code> block.
In Nim, blocks are parts of code with the same indentation (same number of spaces before the first character), and the default indentation level is two spaces.
You will see such blocks everywhere in a Nim program, not only for assigning names.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">-</span><span class="mi">11</span>
<span class="n">d</span> <span class="o">=</span> <span class="s">"Hello"</span>
<span class="n">e</span> <span class="o">=</span> <span class="sc">'!'</span></code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
In Nim tabs are not allowed as indentation.<br>
You can set up your code editor to convert pressing <code>Tab</code> to any number of spaces.<br>
In VS Code, the default setting is to convert <code>Tab</code> to four spaces.
This is easily overridden in settings (<code>Ctrl+,</code>) by setting <code>"editor.tabSize": 2</code>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>As previously mentioned variables are mutable, i.e. their value can change (multiple times), but their type must stay the same as declared.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <span class="n">f</span> <span class="o">=</span> <span class="mi">7</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="n">f</span> <span class="o">=</span> <span class="o">-</span><span class="mi">3</span> <i class="conum" data-value="2"></i><b>(2)</b>
<span class="n">f</span> <span class="o">=</span> <span class="mi">19</span>
<span class="n">f</span> <span class="o">=</span> <span class="s">"Hello"</span> <span class="c"># error </span><i class="conum" data-value="3"></i><b>(3)</b> <i class="conum" data-value="4"></i><b>(4)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Variable <code>f</code> has an initial value of <code>7</code> and its type is inferred as <code>int</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>The value of <code>f</code> is first changed to <code>-3</code>, and then to <code>19</code>. Both of these are integers, the same as the original value.</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>Trying to change the value of <code>f</code> to <code>"Hello"</code> produces an error because <code>Hello</code> is not a number, and this would change the type of <code>f</code> from an integer to a string.</td>
</tr>
<tr>
<td><i class="conum" data-value="4"></i><b>4</b></td>
<td><code># error</code> is a comment. Comments in Nim code are written after a <code>#</code> character. Everything after it on the same line will be ignored.</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_immutable_assignment">Immutable assignment</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Unlike variables declared with <code>var</code> keyword, two more types of assignment exist in Nim, whose value cannot change, one declared with the <code>const</code> keyword, and the other declared with the <code>let</code> keyword.</p>
</div>
<div class="sect2">
<h3 id="_const">Const</h3>
<div class="paragraph">
<p>The value of an immutable assignment declared with <code>const</code> keyword must be known at compile time (before the program is run).</p>
</div>
<div class="paragraph">
<p>For example, we can declare the acceleration of gravity as <code>const g = 9.81</code> or pi as <code>const pi = 3.14</code>, as we know their values in advance and these values will not change during the execution of our program.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">const</span> <span class="n">g</span> <span class="o">=</span> <span class="mi">35</span>
<span class="n">g</span> <span class="o">=</span> <span class="o">-</span><span class="mi">27</span> <span class="c"># error </span><i class="conum" data-value="1"></i><b>(1)</b>
<span class="k">var</span> <span class="n">h</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span>
<span class="k">const</span> <span class="n">i</span> <span class="o">=</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">7</span> <span class="c"># error </span><i class="conum" data-value="2"></i><b>(2)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The value of a constant cannot be changed.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Variable <code>h</code> is not evaluated at compile time (it is a variable and its value can change during the execution of a program), consequently the value of constant <code>i</code> can’t be known at compile time, and this will raise an error.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>In some programming languages it is a common practice to have the names of constants written in <code>ALL_CAPS</code>.
Constants in Nim are written just like any other variable.</p>
</div>
</div>
<div class="sect2">
<h3 id="_let">Let</h3>
<div class="paragraph">
<p>Immutable assignments declared with <code>let</code> don’t need to be known at compile time, their value can be set at any time during the execution of a program, but once it is set, their value cannot change.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">35</span>
<span class="n">j</span> <span class="o">=</span> <span class="o">-</span><span class="mi">27</span> <span class="c"># error </span><i class="conum" data-value="1"></i><b>(1)</b>
<span class="k">var</span> <span class="n">k</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span>
<span class="k">let</span> <span class="n">l</span> <span class="o">=</span> <span class="n">k</span> <span class="o">+</span> <span class="mi">7</span> <i class="conum" data-value="2"></i><b>(2)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The value of an immutable cannot be changed.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>In contrast to the <code>const</code> example above, this works.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>In practice, you will see/use <code>let</code> more frequently than <code>const</code>.</p>
</div>
<div class="paragraph">
<p>While you could use <code>var</code> for everything, your default choice should be <code>let</code>.
Use <code>var</code> only for the variables which will be modified.</p>
</div>
</div>
</div>
</div>
<h1 id="_basic_data_types" class="sect0">Basic data types</h1>
<div class="sect1">
<h2 id="_integers">Integers</h2>
<div class="sectionbody">
<div class="paragraph">
<p>As seen in the previous chapter, integers are numbers which are written without a fractional component and without a decimal point.</p>
</div>
<div class="paragraph">
<p>For example: <code>32</code>, <code>-174</code>, <code>0</code>, <code>10_000_000</code> are all integers.
Notice that we can use <code>_</code> as a thousands separator, to make larger numbers more readable (it is easier to see that we’re talking about 10 million when it’s written as 10_000_000 rather than as 10000000).</p>
</div>
<div class="paragraph">
<p>The usual mathematical operators — addition (<code>+</code>), subtraction (<code>-</code>), multiplication (<code>*</code>), and division (<code>/</code>) — work as one would expect.
The first three operations always produce integers, while dividing two integers always gives a floating point number (a number with a decimal point) as a result, even if two numbers can be divided without a remainder.</p>
</div>
<div class="paragraph">
<p>Integer division (division where the fractional part is discarded) can be achieved with the <code>div</code> operator.
An operator <code>mod</code> is used if one is interested in the remainder (modulus) of an integer division.
The result of these two operations is always an integer.</p>
</div>
<div class="listingblock">
<div class="title">integers.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">11</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">echo</span> <span class="s">"a + b = "</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="n">echo</span> <span class="s">"a - b = "</span><span class="p">,</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span>
<span class="n">echo</span> <span class="s">"a * b = "</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
<span class="n">echo</span> <span class="s">"a / b = "</span><span class="p">,</span> <span class="n">a</span> <span class="o">/</span> <span class="n">b</span>
<span class="n">echo</span> <span class="s">"a div b = "</span><span class="p">,</span> <span class="n">a</span> <span class="ow">div</span> <span class="n">b</span>
<span class="n">echo</span> <span class="s">"a mod b = "</span><span class="p">,</span> <span class="n">a</span> <span class="ow">mod</span> <span class="n">b</span></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The <code>echo</code> command will print to the screen everything that follows it separated by commas. In this case, it first prints the string <code>a + b = </code>, and then after it, in the same row, it prints the result of the expression <code>a + b</code>.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>We can compile and run the above code, and the output should be:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">a + b = 15
a - b = 7
a * b = 44
a / b = 2.75
a div b = 2
a mod b = 3</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_floats">Floats</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Floating-point numbers, or floats for short, are an <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic">approximate representation</a> of real numbers.</p>
</div>
<div class="paragraph">
<p>For example: <code>2.73</code>, <code>-3.14</code>, <code>5.0</code>, <code>4e7</code> are floats.
Notice that we can use scientific notation for large floats, where the number after the <code>e</code> is the exponent.
In this example, <code>4e7</code> is a notation representing <code>4 * 10^7</code>.</p>
</div>
<div class="paragraph">
<p>We can also use the four basic mathematical operations between two floats.
Operators <code>div</code> and <code>mod</code> are not defined for floats.</p>
</div>
<div class="listingblock">
<div class="title">floats.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">c</span> <span class="o">=</span> <span class="mf">6.75</span>
<span class="n">d</span> <span class="o">=</span> <span class="mf">2.25</span>
<span class="n">echo</span> <span class="s">"c + d = "</span><span class="p">,</span> <span class="n">c</span> <span class="o">+</span> <span class="n">d</span>
<span class="n">echo</span> <span class="s">"c - d = "</span><span class="p">,</span> <span class="n">c</span> <span class="o">-</span> <span class="n">d</span>
<span class="n">echo</span> <span class="s">"c * d = "</span><span class="p">,</span> <span class="n">c</span> <span class="o">*</span> <span class="n">d</span>
<span class="n">echo</span> <span class="s">"c / d = "</span><span class="p">,</span> <span class="n">c</span> <span class="o">/</span> <span class="n">d</span></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">c + d = 9.0 <i class="conum" data-value="1"></i><b>(1)</b>
c - d = 4.5
c * d = 15.1875
c / d = 3.0 <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Notice that in the addition and division examples, even though we get a number without a decimal part, the result is still of the floating type.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The precedence of mathematical operations is as one would expect: multiplication and division have higher priority than addition and subtraction.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="n">echo</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span> <span class="o">*</span> <span class="mi">4</span>
<span class="n">echo</span> <span class="mi">24</span> <span class="o">-</span> <span class="mi">8</span> <span class="o">/</span> <span class="mi">4</span></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">14
22.0</code></pre>
</div>
</div>
<div class="sect2">
<h3 id="_converting_floats_and_integers">Converting floats and integers</h3>
<div class="paragraph">
<p>Mathematical operations between variables of different numerical types are not possible in Nim, and they will produce an error:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">e</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">f</span> <span class="o">=</span> <span class="mf">23.456</span>
<span class="n">echo</span> <span class="n">e</span> <span class="o">+</span> <span class="n">f</span> <span class="c"># error</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The values of variables need to be converted to the same type.
Conversion is straight-forward: to convert to an integer, we use the <code>int</code> function, and to convert to a float the <code>float</code> function is used.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">e</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">f</span> <span class="o">=</span> <span class="mf">23.987</span>
<span class="n">echo</span> <span class="kt">float</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="n">echo</span> <span class="kt">int</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <i class="conum" data-value="2"></i><b>(2)</b>
<span class="n">echo</span> <span class="kt">float</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">+</span> <span class="n">f</span> <i class="conum" data-value="3"></i><b>(3)</b>
<span class="n">echo</span> <span class="n">e</span> <span class="o">+</span> <span class="kt">int</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <i class="conum" data-value="4"></i><b>(4)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Printing a <code>float</code> version of an integer <code>e</code>. (<code>e</code> remains of integer type)</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Printing an <code>int</code> version of a float <code>f</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>Both operands are floats and can be added.</td>
</tr>
<tr>
<td><i class="conum" data-value="4"></i><b>4</b></td>
<td>Both operands are integers and can be added.</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">5.0
23
28.987
28</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
When using the <code>int</code> function to convert a float to an integer no rounding will be performed.
The number simply drops any decimals.<br>
To perform rounding we must call another function, but for that we must know a bit more about how to use Nim.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_characters">Characters</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>char</code> type is used for representing a single <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> character.</p>
</div>
<div class="paragraph">
<p>Chars are written between two single ticks (<code>'</code>).
Chars can be letters, symbols, or single digits.
Multiple digits or multiple letters produce an error.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">h</span> <span class="o">=</span> <span class="sc">'z'</span>
<span class="n">i</span> <span class="o">=</span> <span class="sc">'+'</span>
<span class="n">j</span> <span class="o">=</span> <span class="sc">'2'</span>
<span class="n">k</span> <span class="o">=</span> <span class="sc">'35'</span> <span class="c"># error</span>
<span class="n">l</span> <span class="o">=</span> <span class="sc">'xy'</span> <span class="c"># error</span></code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_strings">Strings</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Strings can be described as a series of characters.
Their content is written between two double quotes (<code>"</code>).</p>
</div>
<div class="paragraph">
<p>We might think of strings as words, but they can contain more than one word, some symbols, or digits.</p>
</div>
<div class="listingblock">
<div class="title">strings.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">m</span> <span class="o">=</span> <span class="s">"word"</span>
<span class="n">n</span> <span class="o">=</span> <span class="s">"A sentence with interpunction."</span>
<span class="n">o</span> <span class="o">=</span> <span class="s">""</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="n">p</span> <span class="o">=</span> <span class="s">"32"</span> <i class="conum" data-value="2"></i><b>(2)</b>
<span class="n">q</span> <span class="o">=</span> <span class="s">"!"</span> <i class="conum" data-value="3"></i><b>(3)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>An empty string.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>This is not a number (int). It is inside double quotes, making it a string.</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>Even though this is only one character, it is not a char because it is enclosed inside of double quotes.</td>
</tr>
</table>
</div>
<div class="sect2">
<h3 id="_special_characters">Special characters</h3>
<div class="paragraph">
<p>If we try to print the following string:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="n">echo</span> <span class="s">"some</span><span class="se">\n</span><span class="s">im</span><span class="se">\t</span><span class="s">ips"</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>the result might surprise us:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">some
im ips</code></pre>
</div>
</div>
<div class="paragraph">
<p>This is because there are several characters which have a special meaning.
They are used by prepending the escape character <code>\</code> to them.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>\n</code> is a newline character</p>
</li>
<li>
<p><code>\t</code> is a tab character</p>
</li>
<li>
<p><code>\\</code> is a backslash (since one <code>\</code> is used as the escape character)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If we wanted to print the above example as it was written, we have two possibilities:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Use <code>\\</code> instead of <code>\</code> to print backslashes, or</p>
</li>
<li>
<p>Use raw strings which have syntax <code>r"…​"</code> (putting a letter <code>r</code> immediately before the first quote), in which there are no escape characters and no special meanings: everything is printed as it is.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="n">echo</span> <span class="s">"some</span><span class="se">\\</span><span class="s">nim</span><span class="se">\\</span><span class="s">tips"</span>
<span class="n">echo</span> <span class="s">r"some\nim\tips"</span></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">some\nim\tips
some\nim\tips</code></pre>
</div>
</div>
<div class="paragraph">
<p>There are more special characters than the ones listed above, and they are all found in the <a href="https://nim-lang.org/docs/manual.html#lexical-analysis-string-literals">Nim manual</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_string_concatenation">String concatenation</h3>
<div class="paragraph">
<p>Strings in Nim are mutable, meaning their content can change.
With the <code>add</code> function we can add (append) either another string or a char to an existing string.
If we don’t want to change the original string, we can also concatenate (join together) strings with the <code>&</code> operator, this returns a new string.</p>
</div>
<div class="listingblock">
<div class="title">stringConcat.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">var</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="n">p</span> <span class="o">=</span> <span class="s">"abc"</span>
<span class="n">q</span> <span class="o">=</span> <span class="s">"xy"</span>
<span class="n">r</span> <span class="o">=</span> <span class="sc">'z'</span>
<span class="n">p</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="s">"def"</span><span class="p">)</span> <i class="conum" data-value="2"></i><b>(2)</b>
<span class="n">echo</span> <span class="s">"p is now: "</span><span class="p">,</span> <span class="n">p</span>
<span class="n">q</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <i class="conum" data-value="3"></i><b>(3)</b>
<span class="n">echo</span> <span class="s">"q is now: "</span><span class="p">,</span> <span class="n">q</span>
<span class="n">echo</span> <span class="s">"concat: "</span><span class="p">,</span> <span class="n">p</span> <span class="o">&</span> <span class="n">q</span> <i class="conum" data-value="4"></i><b>(4)</b>
<span class="n">echo</span> <span class="s">"p is still: "</span><span class="p">,</span> <span class="n">p</span>
<span class="n">echo</span> <span class="s">"q is still: "</span><span class="p">,</span> <span class="n">q</span></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>If we plan to modify strings, they should be declared as <code>var</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Adding another string modifies the existing string <code>p</code> in-place, changing its value.</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>We can also add a <code>char</code> to a string.</td>
</tr>
<tr>
<td><i class="conum" data-value="4"></i><b>4</b></td>
<td>Concatenating two strings produces a new string, without modifying the original strings.</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">p is now: abcdef
q is now: xyz
concat: abcdefxyz
p is still: abcdef
q is still: xyz</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_boolean">Boolean</h2>
<div class="sectionbody">
<div class="paragraph">
<p>A boolean (or just <code>bool</code>) data type can only have two values: <code>true</code> or <code>false</code>.
Booleans are usually used for control flow (see <a href="#_control_flow">next chapter</a>), and they are often a result of relational operators.</p>
</div>
<div class="paragraph">
<p>The usual naming convention for boolean variables is to write them as a simple yes/no (true/false) question, e.g. <code>isEmpty</code>, <code>isFinished</code>, <code>isMoving</code>, etc.</p>
</div>
<div class="sect2">
<h3 id="_relational_operators">Relational operators</h3>
<div class="paragraph">
<p>Relational operators test the relation between two entities, which must be comparable.</p>
</div>
<div class="paragraph">
<p>To compare if two values are the same, <code>==</code> (two equal signs) is used.
Do not confuse this with <code>=</code>, which is used for assignment as we saw earlier.</p>
</div>
<div class="paragraph">
<p>Here are all the relational operators defined for integers:</p>
</div>
<div class="listingblock">
<div class="title">relationalOperators.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">g</span> <span class="o">=</span> <span class="mi">31</span>
<span class="n">h</span> <span class="o">=</span> <span class="mi">99</span>
<span class="n">echo</span> <span class="s">"g is greater than h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o">></span> <span class="n">h</span>
<span class="n">echo</span> <span class="s">"g is smaller than h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o"><</span> <span class="n">h</span>
<span class="n">echo</span> <span class="s">"g is equal to h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o">==</span> <span class="n">h</span>
<span class="n">echo</span> <span class="s">"g is not equal to h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o">!=</span> <span class="n">h</span>
<span class="n">echo</span> <span class="s">"g is greater or equal to h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o">>=</span> <span class="n">h</span>
<span class="n">echo</span> <span class="s">"g is smaller or equal to h: "</span><span class="p">,</span> <span class="n">g</span> <span class="o"><=</span> <span class="n">h</span></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="output">g is greater than h: false
g is smaller than h: true
g is equal to h: false
g is not equal to h: true
g is greater or equal to h: false
g is smaller or equal to h: true</code></pre>
</div>
</div>
<div class="paragraph">
<p>We can also compare characters and strings:</p>
</div>
<div class="listingblock">
<div class="title">relationalOperators.nim</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="nim"><span class="k">let</span>
<span class="n">i</span> <span class="o">=</span> <span class="sc">'a'</span>
<span class="n">j</span> <span class="o">=</span> <span class="sc">'d'</span>
<span class="n">k</span> <span class="o">=</span> <span class="sc">'Z'</span>
<span class="n">echo</span> <span class="n">i</span> <span class="o"><</span> <span class="n">j</span>
<span class="n">echo</span> <span class="n">i</span> <span class="o"><</span> <span class="n">k</span> <i class="conum" data-value="1"></i><b>(1)</b>