 bondscell_results $bd7fb049-8774-446b-8163-268025be8486queued¤logsrunning¦outputbody?<div class="markdown"><h2>Short-Circuit Evaluation</h2>
<p>The <code>&amp;&amp;</code> and <code>||</code> operators in Julia are the logical &quot;and&quot; and <code>or</code> operators, they are called short-circuit cause the second element is not always evaluated. That is because <code>false &amp;&amp; b</code> returns false whatever is the value of b, therefore in this case there is no need to evaluate b to know the veridicity of <code>a &amp;&amp; b</code>. Similar reasoning works for <code>||</code>, in particular <code>true || b</code> yields always true.</p>
<p>We can say that <code>a &amp;&amp; b</code> is equivalent to</p>
<pre><code class="language-julia-repl">if a
	b
end</code></pre>
<p>while <code>a || b</code> is equivalent to</p>
<pre><code class="language-julia-repl">if a
else
	b
end</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Ͼpersist_js_state·has_pluto_hook_features§cell_id$bd7fb049-8774-446b-8163-268025be8486depends_on_disabled_cells§runtime Եpublished_object_keysdepends_on_skipped_cells§errored$2bc37e0e-b61b-417e-b189-59e6845727a5queued¤logsrunning¦outputbody<div class="markdown"><p>Unlike other languages, if blocks are expressions and not statements &#40;many statements in Julia become expressions&#41;, they assume the value of the last expression evaluated:</p>
<pre><code class="language-julia-repl">julia&gt; x &#61; 3
3

julia&gt; if x &gt; 0
           &quot;positive&#33;&quot;
       else
           &quot;negative...&quot;
       end
&quot;positive&#33;&quot;</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\^Jpersist_js_state·has_pluto_hook_features§cell_id$2bc37e0e-b61b-417e-b189-59e6845727a5depends_on_disabled_cells§runtime lhpublished_object_keysdepends_on_skipped_cells§errored$43aa69fd-4c81-43fe-a390-3ebf208b2170queued¤logsrunning¦outputbody+<div class="markdown"><p>In the first line of the <code>let</code> block we are defining a new local variable to have the value of z. In the second line, the <code>x</code> used in the right side of the equal sign is the global variable <code>x</code> defined in the <code>begin</code> block before, while the x in the left side is a new local variable, we can check that the value of the global variable <code>x</code> is still 1. In the rest of the block the local variable <code>x</code> is going to shadow the global variable <code>x</code></p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\_persist_js_state·has_pluto_hook_features§cell_id$43aa69fd-4c81-43fe-a390-3ebf208b2170depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$9f687357-2f00-4ec1-8338-66c5b65aca89queued¤logsrunning¦outputbody'<div class="markdown"><h2>Tasks</h2>
<p>Tasks are a control flow feature that allows computations to be suspended and resumed in a flexible manner.</p>
<p>For example, you may want to start an operation, which could be the download of a file, and meanwhile do something else and resume it once it has ended.</p>
<p>A task can be created with the <code>Task</code> constructor which accepts a 0-argument function function to run. It can also be created using the macro <code>@task</code>:</p>
<pre><code class="language-julia-repl">julia&gt; t &#61; @task begin; sleep&#40;5&#41;; println&#40;&quot;done&quot;&#41;; end
Task &#40;runnable&#41; @0x00007f13a40c0eb0</code></pre>
<p>This only creates a task but it hasn&#39;t started yet. To do so we have to append it to the queue</p>
<pre><code class="language-julia-repl">julia&gt; schedule&#40;t&#41;;</code></pre>
<p>Now the task is set to run, but we can do other things in the meanwhile. Often tasks are added to the queue in the moment that they are created, this can be done with the macro <code>@async</code> which is equivalent to <code>schedule&#40;@task&#41;</code>.</p>
<p>If you need to wait for the task to be completed, you can use the function <code>wait</code>. This is necessary when performing this actions in a script instead that from REPL</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Upersist_js_state·has_pluto_hook_features§cell_id$9f687357-2f00-4ec1-8338-66c5b65aca89depends_on_disabled_cells§runtime ^published_object_keysdepends_on_skipped_cells§errored$4bcd22f2-c51e-4af9-867e-43c5e0fc5843queued¤logsrunning¦outputbody3mimetext/plainrootassigneelast_run_timestampAj^apersist_js_state·has_pluto_hook_features§cell_id$4bcd22f2-c51e-4af9-867e-43c5e0fc5843depends_on_disabled_cells§runtimeMpublished_object_keysdepends_on_skipped_cells§errored$f04fe25d-accc-4903-9864-e694c18eebdfqueued¤logsrunning¦outputbodyo<div class="markdown"><p>To trhow an error, you can use the <code>throw</code> function:</p>
<pre><code class="language-julia-repl">julia&gt; f&#40;x&#41; &#61; x&gt;&#61;0 ? exp&#40;-x&#41; : throw&#40;DomainError&#40;x, &quot;argument must be non-negative&quot;&#41;&#41;
f &#40;generic function with 1 method&#41;

julia&gt; f&#40;1&#41;
0.36787944117144233

julia&gt; f&#40;-1&#41;
ERROR: DomainError with -1:
argument must be non-negative
Stacktrace:
 &#91;1&#93; f&#40;::Int64&#41; at ./none:1</code></pre>
<p>Also the <code>error</code> function can be used, but it is not adviced since it is too general.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Эpersist_js_state·has_pluto_hook_features§cell_id$f04fe25d-accc-4903-9864-e694c18eebdfdepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$db39b23b-08e8-4af2-97c9-42b7e08843ecqueued¤logsrunning¦outputbody<div class="markdown"><p>To important control flow instructions in a loop are <code>break</code> and <code>continue</code>. The first one causes the exit from a loop, while the second one jumps directly to the next iteration of the loop, without finishing evaluating the current iteration.</p>
<pre><code class="language-julia-repl">julia&gt; i &#61; 1;

julia&gt; while true
           println&#40;i&#41;
		   i &gt;&#61; 3 &amp;&amp; break
           global i &#43;&#61; 1
       end
1
2
3

julia&gt; for j &#61; 1:1000
           println&#40;j&#41;
 		   j &gt;&#61; 3 &amp;&amp; break
		end
1
2
3
julia&gt; for i &#61; 1:10
		   i &#37; 3 || continue
           println&#40;i&#41;
       end
3
6
9</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\persist_js_state·has_pluto_hook_features§cell_id$db39b23b-08e8-4af2-97c9-42b7e08843ecdepends_on_disabled_cells§runtime u,published_object_keysdepends_on_skipped_cells§errored$a2e86dbc-44be-41f0-95dc-cc21441c3da4queued¤logsrunning¦outputbodys<div class="markdown"><h3><code>else</code> and <code>finally</code> clauses</h3>
<p>The <code>else</code> clause is used when you want to run something only if the <code>try</code> statement succeeds.</p>
<p>The <code>finally</code> clause instead is executed indipendently from if the try statement returned an error or not. This is very usefull to clean some data in case an error occured, for example to close safely a file, such that it won&#39;t be corrupted</p>
<pre><code class="language-julia-repl">f &#61; open&#40;&quot;file&quot;&#41;
try
    # operate on file f
finally
    close&#40;f&#41;
end</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\persist_js_state·has_pluto_hook_features§cell_id$a2e86dbc-44be-41f0-95dc-cc21441c3da4depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$3466d2e0-0357-41e1-b2ca-17f7665b8d09queued¤logsrunning¦outputbody3<div class="markdown"><h2>Compound Expressions.</h2>
<p>A compound expression is a single expression tha evaluates several subexpressions in order and having the value of the last subexpression. The main Julia constructs that accomplish this are <code>begin</code> block and <code>;</code> chains</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\ٰpersist_js_state·has_pluto_hook_features§cell_id$3466d2e0-0357-41e1-b2ca-17f7665b8d09depends_on_disabled_cells§runtime Lpublished_object_keysdepends_on_skipped_cells§errored$be77ed98-95bd-479c-88d7-5a35679e5281queued¤logsrunning¦outputbody<div class="markdown"><p>Another common function is <code>enumerate</code> which yields both the index of the collection and the value</p>
<pre><code class="language-julia-repl">julia&gt; for &#40;i, val&#41; in enumerate&#40;&#91;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;&#93;&#41;
           println&#40;&#40;i, val&#41;&#41;
       end
&#40;1, &quot;a&quot;&#41;
&#40;2, &quot;b&quot;&#41;
&#40;3, &quot;c&quot;&#41;</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\hpersist_js_state·has_pluto_hook_features§cell_id$be77ed98-95bd-479c-88d7-5a35679e5281depends_on_disabled_cells§runtime 0published_object_keysdepends_on_skipped_cells§errored$24e8a309-3e0a-408d-be70-5b679c1dec0dqueued¤logsrunning¦outputbody<div class="markdown"><p>if blocks do not create a new scope, so variables defined inside them are still accessible outside, for example we can redefine the function test as follow</p>
<pre><code class="language-julia-repl">julia&gt; function test&#40;x,y&#41;
           if x &lt; y
               relation &#61; &quot;less than&quot;
           elseif x &#61;&#61; y
               relation &#61; &quot;equal to&quot;
           else
               relation &#61; &quot;greater than&quot;
           end
           println&#40;&quot;x is &quot;, relation, &quot; y.&quot;&#41;
       end
test &#40;generic function with 1 method&#41;

julia&gt; test&#40;2, 1&#41;
x is greater than y.</code></pre>
<p>Remeber to always insert a definition to the variable in each path otherwise you could have errors of wrong behaviours. For example, if we hadn&#39;t added a default case &#40;else&#41; in the if, in case <code>x &gt; y</code> the variable relation wouldn&#39;t be defined and that would create an error. Worst case is if that variable existed in an outer scope, in that case we wouldn&#39;t recieve an error and the program would continue to run, but it would have the wrong behaviour &#40;remember: the absence of an error is worse than an error&#41; </p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Cpersist_js_state·has_pluto_hook_features§cell_id$24e8a309-3e0a-408d-be70-5b679c1dec0ddepends_on_disabled_cells§runtime Ipublished_object_keysdepends_on_skipped_cells§errored$f89c8a46-5f78-4b36-82f5-f1465e4e3b97queued¤logsrunning¦outputbody@<div class="markdown"><h2>Conditional Evaluation</h2>
<p>Conditional evaluations allows to decide which portion of code should be evaluated based on the veridicity of a predicate. The most common example is the <code>if</code>-<code>elseif</code>-<code>else</code>:</p>
<pre><code class="language-julia-repl">julia&gt; function test&#40;x, y&#41;
           if x &lt; y
               println&#40;&quot;x is less than y&quot;&#41;
           elseif x &gt; y
               println&#40;&quot;x is greater than y&quot;&#41;
           else
               println&#40;&quot;x is equal to y&quot;&#41;
           end
       end
test &#40;generic function with 1 method&#41;

julia&gt; test&#40;1, 2&#41;
x is less than y

julia&gt; test&#40;2, 1&#41;
x is greater than y

julia&gt; test&#40;1, 1&#41;
x is equal to y</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\%persist_js_state·has_pluto_hook_features§cell_id$f89c8a46-5f78-4b36-82f5-f1465e4e3b97depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$e5454d2e-5b75-4224-ba08-6ddb8a051f45queued¤logsrunning¦outputbody1mimetext/plainrootassigneelast_run_timestampAj^	հpersist_js_state·has_pluto_hook_features§cell_id$e5454d2e-5b75-4224-ba08-6ddb8a051f45depends_on_disabled_cells§runtime׵published_object_keysdepends_on_skipped_cells§errored$d7cf560e-e576-4594-9be2-f726586238e2queued¤logsrunning¦outputbody3mimetext/plainrootassigneelast_run_timestampAj^аpersist_js_state·has_pluto_hook_features§cell_id$d7cf560e-e576-4594-9be2-f726586238e2depends_on_disabled_cells§runtimeI%published_object_keysdepends_on_skipped_cells§errored$02e50952-2cb9-4807-b35c-56510690948dqueued¤logsrunning¦outputbodyٷ<div class="markdown"><p>Unlike other languages, Julia requires the condition to be of type <code>Bool</code>, it is not sufficient that the condition is convertible to it.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\xѰpersist_js_state·has_pluto_hook_features§cell_id$02e50952-2cb9-4807-b35c-56510690948ddepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$b8779469-c1a8-4f0d-8927-3fbf17aa4d0aqueued¤logsrunning¦outputbody<div class="markdown"><p>It is not rare the need to iterate over more than one collection, this can be achieved by using the function <code>zip</code>:</p>
<pre><code class="language-julia-repl">julia&gt; for &#40;j, k&#41; in zip&#40;&#91;1 2 3&#93;, &#91;4 5 6 7&#93;&#41;
           println&#40;&#40;j,k&#41;&#41;
       end
&#40;1, 4&#41;
&#40;2, 5&#41;
&#40;3, 6&#41;</code></pre>
<p>the loop exits when one of the collections is exhausted</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Npersist_js_state·has_pluto_hook_features§cell_id$b8779469-c1a8-4f0d-8927-3fbf17aa4d0adepends_on_disabled_cells§runtime "published_object_keysdepends_on_skipped_cells§errored$0a24c131-c50d-48e3-803d-d77f8dc12034queued¤logsrunning¦outputbody<div class="markdown"><p>Julia also offers an easy way to write nested loops:</p>
<pre><code class="language-julia-repl">julia&gt; for i &#61; 1:3, j &#61; i:3
           println&#40;&#40;i, j&#41;&#41;
       end
&#40;1, 1&#41;
&#40;1, 2&#41;
&#40;1, 3&#41;
&#40;2, 2&#41;
&#40;2, 3&#41;
&#40;3, 3&#41;</code></pre>
<p>Mind that a break statement would cause the exit from all the nested loops.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\1Ypersist_js_state·has_pluto_hook_features§cell_id$0a24c131-c50d-48e3-803d-d77f8dc12034depends_on_disabled_cells§runtime Mpublished_object_keysdepends_on_skipped_cells§errored$ed94aa97-b071-4ea8-b468-9fdf0375085cqueued¤logsrunning¦outputbody{<div class="markdown"><p>Julia also supports the ternary operator <code>?:</code> which is equivalent to an <code>if</code>-<code>else</code> block but it can be execute in one line. For example an easy way to get the maximum between 2 numbers is:</p>
<pre><code class="language-julia-repl">julia&gt; x &#61; 3; y &#61; 2;
julia&gt; z &#61; x &gt; y ? x : y
3</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\ϚVpersist_js_state·has_pluto_hook_features§cell_id$ed94aa97-b071-4ea8-b468-9fdf0375085cdepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$f39a1f46-e7d6-4875-8835-e303cedf92daqueued¤logsrunning¦outputbody<div class="markdown"><h2>Exception Handling</h2>
<p>Errors are one of the most important thing in a programming language, probably there is no worse thing than seeing your program crush or return wrong results, without an explanation.</p>
<p>In Python, errors are raised so that the user can see them, in Julia they are thrown at you, unless you don&#39;t catch them.</p>
<p>Errors always come with a backtrace &#40;also called stacktrace&#41;, which is the list of the called functions from the main to where the error has taken place, helping the user in debugging the code.</p>
<p>A list of the built-in exceptions in Julia can be found <a href="https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions">here</a></p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\Аdpersist_js_state·has_pluto_hook_features§cell_id$f39a1f46-e7d6-4875-8835-e303cedf92dadepends_on_disabled_cells§runtime p޵published_object_keysdepends_on_skipped_cells§errored$305a9bf1-16dc-4239-a89f-d2679554d343queued¤logsrunning¦outputbody6mimetext/plainrootassigneelast_run_timestampAj^Ͱpersist_js_state·has_pluto_hook_features§cell_id$305a9bf1-16dc-4239-a89f-d2679554d343depends_on_disabled_cells§runtimeWpublished_object_keysdepends_on_skipped_cells§errored$ce7f57b1-9b2c-4138-ad37-46a22982db18queued¤logsrunning¦outputbody:<div class="markdown"><h2>Repeated Evaluation: Loops</h2>
<p>Loops evaluate a portion of code each time the evaluation of a condition is <code>true</code>. There are 2 types of loops in Julia which are <code>for</code> and <code>while</code>. Note that both of them create a soft local scope.</p>
<pre><code class="language-julia-repl">julia&gt; i &#61; 1;

julia&gt; while i &lt;&#61; 3
           println&#40;i&#41;
           global i &#43;&#61; 1
       end
1
2
3</code></pre>
<pre><code class="language-julia-repl">julia&gt; for i &#61; 1:3
           println&#40;i&#41;
       end
1
2
3
julia&gt; for i in &#91;1,4,0&#93;
           println&#40;i&#41;
       end
1
4
0

julia&gt; for s ∈ &#91;&quot;foo&quot;,&quot;bar&quot;,&quot;baz&quot;&#93;
           println&#40;s&#41;
       end
foo
bar
baz</code></pre>
</div>mimetext/htmlrootassigneelast_run_timestampAj\ʰpersist_js_state·has_pluto_hook_features§cell_id$ce7f57b1-9b2c-4138-ad37-46a22982db18depends_on_disabled_cells§runtime *published_object_keysdepends_on_skipped_cells§errored$721109c6-5b68-47b7-bfaa-09a6dbf94ba9queued¤logslinemsg@is task started? yes
in the mean time
1
2
3
4
5
6
7
8
9
10
done
text/plaincell_id$721109c6-5b68-47b7-bfaa-09a6dbf94ba9kwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/5ete1/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbodymimetext/plainrootassigneelast_run_timestampAj_WѰpersist_js_state·has_pluto_hook_features§cell_id$721109c6-5b68-47b7-bfaa-09a6dbf94ba9depends_on_disabled_cells§runtime   +bpublished_object_keysdepends_on_skipped_cells§errored$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0queued¤logsrunning¦outputbody<div class="markdown"><h3>The <code>try/catch</code> statement</h3>
<p>If we fear that a code of block my return an error, we can <code>try</code> it, and in case it does, we can change something.</p>
<pre><code class="language-julia-repl">julia&gt; sqrt_second&#40;x&#41; &#61; try
           sqrt&#40;x&#91;2&#93;&#41;
       catch y
           if isa&#40;y, DomainError&#41;
               sqrt&#40;complex&#40;x&#91;2&#93;, 0&#41;&#41;
           elseif isa&#40;y, BoundsError&#41;
               sqrt&#40;x&#41;
		   else
			   retrhow&#40;e&#41;
           end
       end
sqrt_second &#40;generic function with 1 method&#41;

julia&gt; sqrt_second&#40;&#91;1 4&#93;&#41;	# this executes smoothly
2.0

julia&gt; sqrt_second&#40;&#91;1 -4&#93;&#41;	# this raises DomainError
0.0 &#43; 2.0im

julia&gt; sqrt_second&#40;9&#41;		# this raises BoundsError
3.0

julia&gt; sqrt_second&#40;-9&#41;		# this raises a BoundsError first, and a DomainError when recalled
ERROR: DomainError with -9.0:
sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt&#40;Complex&#40;x&#41;&#41;.
Stacktrace:
&#91;...&#93;</code></pre>
<div class="admonition note"><p class="admonition-title">Note</p><p>When catching expressions, remember to retrhow the ones you did not handled. The difference wrt trhow is that retrhow appends to the exiisting stacktrace</p>
</div>
</div>mimetext/htmlrootassigneelast_run_timestampAj\˧persist_js_state·has_pluto_hook_features§cell_id$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0depends_on_disabled_cells§runtime Xpublished_object_keysdepends_on_skipped_cells§errored$6b1d7868-5563-499a-9a90-20250b6210bcqueued¤logsrunning¦outputbodyV<div class="markdown"><p>Similar to <code>begin</code> expression, there is the <code>let</code> block, the main difference is that <code>let</code> creates a new hard scope while <code>begin</code> does not create a new scope. Given the last fact, <code>let</code> allows to define local variables from already existing variables.</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\_persist_js_state·has_pluto_hook_features§cell_id$6b1d7868-5563-499a-9a90-20250b6210bcdepends_on_disabled_cells§runtime #published_object_keysdepends_on_skipped_cells§errored$78cec08e-08b5-11f0-062d-65200c1ff50cqueued¤logsrunning¦outputbodye<div class="markdown"><h1>Control Flow</h1>
<p>The flow of a program is which expressions are executed and in what order, the easiest example of a control flow expression is the if/else block, where which expression should be executed is based on the veridicity of a predicate.</p>
<p>In Julia the control flow expressions are the following:</p>
<ul>
<li><p>Compound Expressions: <code>begin</code> and <code>;</code></p>
</li>
<li><p>Conditional Evaluation: <code>if</code>-<code>elseif</code>-<code>else</code> and <code>?:</code> &#40;ternary operator&#41;</p>
</li>
<li><p>Short-Circuit Evaluation: logical operators <code>&amp;&amp;</code> &#40;&quot;and&quot;&#41; and <code>||</code> &#40;&quot;or&quot;&#41;</p>
</li>
<li><p>Repeated Evaluation &#40;Loops&#41;: <code>while</code> and <code>for</code></p>
</li>
<li><p>Exception Handling: <code>try</code>-<code>catch</code>, <code>error</code> and <code>throw</code></p>
</li>
<li><p>Tasks: <code>yieldto</code></p>
</li>
</ul>
<p>The first 5 mechanisms are standard in high-level programming languages, that is not true for <code>Tasks</code>, therefore they will be added to the notebook for the sake of completeness, but it is not excepted from neo-progammers to be able to understand them.</p>
<p>It is useful to have a formal definition of what an expression and statemant are:</p>
<blockquote>
<p>An expression in programming is a combination of literals, variables, operators, and function calls that produce a value when evaluated.</p>
</blockquote>
<blockquote>
<p>Statement are similar to expressions but do not produce a value when evaluated.</p>
</blockquote>
</div>mimetext/htmlrootassigneelast_run_timestampAj\ίpersist_js_state·has_pluto_hook_features§cell_id$78cec08e-08b5-11f0-062d-65200c1ff50cdepends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$bcfb1e5a-621c-41da-82e9-7ea2b968a812queued¤logsrunning¦outputbody5mimetext/plainrootassigneezlast_run_timestampAj^9Űpersist_js_state·has_pluto_hook_features§cell_id$bcfb1e5a-621c-41da-82e9-7ea2b968a812depends_on_disabled_cells§runtime'published_object_keysdepends_on_skipped_cells§errored$c245f4d9-80af-41c4-b7cd-05192eec8cd8queued¤logsrunning¦outputbody<div class="markdown"><p>an example of the usage of these operators is the following:</p>
<pre><code class="language-julia-repl">julia&gt; function fact&#40;n::Int&#41;
           n &gt;&#61; 0 || error&#40;&quot;n must be non-negative&quot;&#41;
           n &#61;&#61; 0 &amp;&amp; return 1
           n * fact&#40;n-1&#41;
       end
fact &#40;generic function with 1 method&#41;

julia&gt; fact&#40;5&#41;
120

julia&gt; fact&#40;0&#41;
1

julia&gt; fact&#40;-1&#41;
ERROR: n must be non-negative
Stacktrace:
 &#91;1&#93; error at ./error.jl:33 &#91;inlined&#93;
 &#91;2&#93; fact&#40;::Int64&#41; at ./none:2
 &#91;3&#93; top-level scope</code></pre>
<p>From this usage we see that it is not important for the last element to be boolean, but the first one still needs to be boolean. Also, they do not always evaluate to a type <code>Bool</code>, but just like <code>if</code> blocks, they evaluate to the last evaluated term</p>
</div>mimetext/htmlrootassigneelast_run_timestampAj\#persist_js_state·has_pluto_hook_features§cell_id$c245f4d9-80af-41c4-b7cd-05192eec8cd8depends_on_disabled_cells§runtime <published_object_keysdepends_on_skipped_cells§errored±cell_dependencies $bd7fb049-8774-446b-8163-268025be8486precedence_heuristic	cell_id$bd7fb049-8774-446b-8163-268025be8486downstream_cells_mapupstream_cells_map@md_strgetindex$2bc37e0e-b61b-417e-b189-59e6845727a5precedence_heuristic	cell_id$2bc37e0e-b61b-417e-b189-59e6845727a5downstream_cells_mapupstream_cells_map@md_strgetindex$43aa69fd-4c81-43fe-a390-3ebf208b2170precedence_heuristic	cell_id$43aa69fd-4c81-43fe-a390-3ebf208b2170downstream_cells_mapupstream_cells_map@md_strgetindex$9f687357-2f00-4ec1-8338-66c5b65aca89precedence_heuristic	cell_id$9f687357-2f00-4ec1-8338-66c5b65aca89downstream_cells_mapupstream_cells_map@md_strgetindex$4bcd22f2-c51e-4af9-867e-43c5e0fc5843precedence_heuristic	cell_id$4bcd22f2-c51e-4af9-867e-43c5e0fc5843downstream_cells_mapx$305a9bf1-16dc-4239-a89f-d2679554d343$e5454d2e-5b75-4224-ba08-6ddb8a051f45yupstream_cells_map+$f04fe25d-accc-4903-9864-e694c18eebdfprecedence_heuristic	cell_id$f04fe25d-accc-4903-9864-e694c18eebdfdownstream_cells_mapupstream_cells_map@md_strgetindex$db39b23b-08e8-4af2-97c9-42b7e08843ecprecedence_heuristic	cell_id$db39b23b-08e8-4af2-97c9-42b7e08843ecdownstream_cells_mapupstream_cells_map@md_strgetindex$a2e86dbc-44be-41f0-95dc-cc21441c3da4precedence_heuristic	cell_id$a2e86dbc-44be-41f0-95dc-cc21441c3da4downstream_cells_mapupstream_cells_map@md_strgetindex$3466d2e0-0357-41e1-b2ca-17f7665b8d09precedence_heuristic	cell_id$3466d2e0-0357-41e1-b2ca-17f7665b8d09downstream_cells_mapupstream_cells_map@md_strgetindex$be77ed98-95bd-479c-88d7-5a35679e5281precedence_heuristic	cell_id$be77ed98-95bd-479c-88d7-5a35679e5281downstream_cells_mapupstream_cells_map@md_strgetindex$24e8a309-3e0a-408d-be70-5b679c1dec0dprecedence_heuristic	cell_id$24e8a309-3e0a-408d-be70-5b679c1dec0ddownstream_cells_mapupstream_cells_map@md_strgetindex$f89c8a46-5f78-4b36-82f5-f1465e4e3b97precedence_heuristic	cell_id$f89c8a46-5f78-4b36-82f5-f1465e4e3b97downstream_cells_mapupstream_cells_map@md_strgetindex$e5454d2e-5b75-4224-ba08-6ddb8a051f45precedence_heuristic	cell_id$e5454d2e-5b75-4224-ba08-6ddb8a051f45downstream_cells_mapupstream_cells_mapx$4bcd22f2-c51e-4af9-867e-43c5e0fc5843$d7cf560e-e576-4594-9be2-f726586238e2precedence_heuristic	cell_id$d7cf560e-e576-4594-9be2-f726586238e2downstream_cells_mapbaupstream_cells_map+$02e50952-2cb9-4807-b35c-56510690948dprecedence_heuristic	cell_id$02e50952-2cb9-4807-b35c-56510690948ddownstream_cells_mapupstream_cells_map@md_strgetindex$b8779469-c1a8-4f0d-8927-3fbf17aa4d0aprecedence_heuristic	cell_id$b8779469-c1a8-4f0d-8927-3fbf17aa4d0adownstream_cells_mapupstream_cells_map@md_strgetindex$0a24c131-c50d-48e3-803d-d77f8dc12034precedence_heuristic	cell_id$0a24c131-c50d-48e3-803d-d77f8dc12034downstream_cells_mapupstream_cells_map@md_strgetindex$ed94aa97-b071-4ea8-b468-9fdf0375085cprecedence_heuristic	cell_id$ed94aa97-b071-4ea8-b468-9fdf0375085cdownstream_cells_mapupstream_cells_map@md_strgetindex$f39a1f46-e7d6-4875-8835-e303cedf92daprecedence_heuristic	cell_id$f39a1f46-e7d6-4875-8835-e303cedf92dadownstream_cells_mapupstream_cells_map@md_strgetindex$305a9bf1-16dc-4239-a89f-d2679554d343precedence_heuristic	cell_id$305a9bf1-16dc-4239-a89f-d2679554d343downstream_cells_mapupstream_cells_map-x$4bcd22f2-c51e-4af9-867e-43c5e0fc5843*z$bcfb1e5a-621c-41da-82e9-7ea2b968a812$ce7f57b1-9b2c-4138-ad37-46a22982db18precedence_heuristic	cell_id$ce7f57b1-9b2c-4138-ad37-46a22982db18downstream_cells_mapupstream_cells_map@md_strgetindex$721109c6-5b68-47b7-bfaa-09a6dbf94ba9precedence_heuristic	cell_id$721109c6-5b68-47b7-bfaa-09a6dbf94ba9downstream_cells_maptupstream_cells_mapsleep:@asyncprintlnBase.TaskBaseistaskstartedBase.scheduleBase.put!wait##sync#41$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0precedence_heuristic	cell_id$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0downstream_cells_mapupstream_cells_map@md_strgetindex$6b1d7868-5563-499a-9a90-20250b6210bcprecedence_heuristic	cell_id$6b1d7868-5563-499a-9a90-20250b6210bcdownstream_cells_mapupstream_cells_map@md_strgetindex$78cec08e-08b5-11f0-062d-65200c1ff50cprecedence_heuristic	cell_id$78cec08e-08b5-11f0-062d-65200c1ff50cdownstream_cells_mapupstream_cells_map@md_strgetindex$bcfb1e5a-621c-41da-82e9-7ea2b968a812precedence_heuristic	cell_id$bcfb1e5a-621c-41da-82e9-7ea2b968a812downstream_cells_mapz$305a9bf1-16dc-4239-a89f-d2679554d343upstream_cells_map$c245f4d9-80af-41c4-b7cd-05192eec8cd8precedence_heuristic	cell_id$c245f4d9-80af-41c4-b7cd-05192eec8cd8downstream_cells_mapupstream_cells_map@md_strgetindexcell_execution_order $78cec08e-08b5-11f0-062d-65200c1ff50c$3466d2e0-0357-41e1-b2ca-17f7665b8d09$4bcd22f2-c51e-4af9-867e-43c5e0fc5843$d7cf560e-e576-4594-9be2-f726586238e2$6b1d7868-5563-499a-9a90-20250b6210bc$bcfb1e5a-621c-41da-82e9-7ea2b968a812$305a9bf1-16dc-4239-a89f-d2679554d343$43aa69fd-4c81-43fe-a390-3ebf208b2170$e5454d2e-5b75-4224-ba08-6ddb8a051f45$f89c8a46-5f78-4b36-82f5-f1465e4e3b97$24e8a309-3e0a-408d-be70-5b679c1dec0d$2bc37e0e-b61b-417e-b189-59e6845727a5$02e50952-2cb9-4807-b35c-56510690948d$ed94aa97-b071-4ea8-b468-9fdf0375085c$bd7fb049-8774-446b-8163-268025be8486$c245f4d9-80af-41c4-b7cd-05192eec8cd8$ce7f57b1-9b2c-4138-ad37-46a22982db18$db39b23b-08e8-4af2-97c9-42b7e08843ec$0a24c131-c50d-48e3-803d-d77f8dc12034$b8779469-c1a8-4f0d-8927-3fbf17aa4d0a$be77ed98-95bd-479c-88d7-5a35679e5281$f39a1f46-e7d6-4875-8835-e303cedf92da$f04fe25d-accc-4903-9864-e694c18eebdf$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0$a2e86dbc-44be-41f0-95dc-cc21441c3da4$9f687357-2f00-4ec1-8338-66c5b65aca89$721109c6-5b68-47b7-bfaa-09a6dbf94ba9last_hot_reload_time        shortpathControl_Flow.jlprocess_statusreadypathM/home/runner/work/JuliaWorkshopAISF/JuliaWorkshopAISF/Lesson1/Control_Flow.jlpluto_versionv0.20.8last_save_timeAj\lcell_order $78cec08e-08b5-11f0-062d-65200c1ff50c$3466d2e0-0357-41e1-b2ca-17f7665b8d09$4bcd22f2-c51e-4af9-867e-43c5e0fc5843$d7cf560e-e576-4594-9be2-f726586238e2$6b1d7868-5563-499a-9a90-20250b6210bc$bcfb1e5a-621c-41da-82e9-7ea2b968a812$305a9bf1-16dc-4239-a89f-d2679554d343$43aa69fd-4c81-43fe-a390-3ebf208b2170$e5454d2e-5b75-4224-ba08-6ddb8a051f45$f89c8a46-5f78-4b36-82f5-f1465e4e3b97$24e8a309-3e0a-408d-be70-5b679c1dec0d$2bc37e0e-b61b-417e-b189-59e6845727a5$02e50952-2cb9-4807-b35c-56510690948d$ed94aa97-b071-4ea8-b468-9fdf0375085c$bd7fb049-8774-446b-8163-268025be8486$c245f4d9-80af-41c4-b7cd-05192eec8cd8$ce7f57b1-9b2c-4138-ad37-46a22982db18$db39b23b-08e8-4af2-97c9-42b7e08843ec$0a24c131-c50d-48e3-803d-d77f8dc12034$b8779469-c1a8-4f0d-8927-3fbf17aa4d0a$be77ed98-95bd-479c-88d7-5a35679e5281$f39a1f46-e7d6-4875-8835-e303cedf92da$f04fe25d-accc-4903-9864-e694c18eebdf$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0$a2e86dbc-44be-41f0-95dc-cc21441c3da4$9f687357-2f00-4ec1-8338-66c5b65aca89$721109c6-5b68-47b7-bfaa-09a6dbf94ba9published_objectsnbpkginstall_time_nscinstantiatedòinstalled_versionsterminal_outputsnbpkg_sync@
[0m[1mResolving...[22m
[90m===[39m
[32m[1m  No Changes[22m[39m to `/tmp/jl_sVgjNk/Project.toml`
[32m[1m  No Changes[22m[39m to `/tmp/jl_sVgjNk/Manifest.toml`

[0m[1mInstantiating...[22m
[90m===[39m

[0m[1mPrecompiling...[22m
[90m===[39m
[32m[1m  Activating[22m[39m project at `/tmp/jl_sVgjNk`enabled÷restart_recommended_msgrestart_required_msgbusy_packageswaiting_for_permission,waiting_for_permission_but_probably_disabled«cell_inputs $bd7fb049-8774-446b-8163-268025be8486cell_id$bd7fb049-8774-446b-8163-268025be8486codeAmd"""
## Short-Circuit Evaluation
The `&&` and `||` operators in Julia are the logical "and" and `or` operators, they are called short-circuit cause the second element is not always evaluated. That is because `false && b` returns false whatever is the value of b, therefore in this case there is no need to evaluate b to know the veridicity of `a && b`. Similar reasoning works for `||`, in particular `true || b` yields always true.

We can say that `a && b` is equivalent to
```julia-repl
if a
	b
end
```
while `a || b` is equivalent to
```julia-repl
if a
else
	b
end
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$2bc37e0e-b61b-417e-b189-59e6845727a5cell_id$2bc37e0e-b61b-417e-b189-59e6845727a5code:md"""
Unlike other languages, if blocks are expressions and not statements (many statements in Julia become expressions), they assume the value of the last expression evaluated:
```julia-repl
julia> x = 3
3

julia> if x > 0
           "positive!"
       else
           "negative..."
       end
"positive!"
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$43aa69fd-4c81-43fe-a390-3ebf208b2170cell_id$43aa69fd-4c81-43fe-a390-3ebf208b2170codemd"""
In the first line of the `let` block we are defining a new local variable to have the value of z. In the second line, the `x` used in the right side of the equal sign is the global variable `x` defined in the `begin` block before, while the x in the left side is a new local variable, we can check that the value of the global variable `x` is still 1. In the rest of the block the local variable `x` is going to shadow the global variable `x`
"""metadatashow_logsèdisabled®skip_as_script«code_folded$9f687357-2f00-4ec1-8338-66c5b65aca89cell_id$9f687357-2f00-4ec1-8338-66c5b65aca89code,md"""
## Tasks
Tasks are a control flow feature that allows computations to be suspended and resumed in a flexible manner.

For example, you may want to start an operation, which could be the download of a file, and meanwhile do something else and resume it once it has ended.

A task can be created with the `Task` constructor which accepts a 0-argument function function to run. It can also be created using the macro `@task`:
```julia-repl
julia> t = @task begin; sleep(5); println("done"); end
Task (runnable) @0x00007f13a40c0eb0
```
This only creates a task but it hasn't started yet. To do so we have to append it to the queue
```julia-repl
julia> schedule(t);
```
Now the task is set to run, but we can do other things in the meanwhile. Often tasks are added to the queue in the moment that they are created, this can be done with the macro `@async` which is equivalent to `schedule(@task)`.

If you need to wait for the task to be completed, you can use the function `wait`. This is necessary when performing this actions in a script instead that from REPL
"""metadatashow_logsèdisabled®skip_as_script«code_folded$4bcd22f2-c51e-4af9-867e-43c5e0fc5843cell_id$4bcd22f2-c51e-4af9-867e-43c5e0fc5843codebegin
	x = 1
	y = 2
	x + y
endmetadatashow_logsèdisabled®skip_as_script«code_folded$f04fe25d-accc-4903-9864-e694c18eebdfcell_id$f04fe25d-accc-4903-9864-e694c18eebdfcodemd"""
To trhow an error, you can use the `throw` function:
```julia-repl
julia> f(x) = x>=0 ? exp(-x) : throw(DomainError(x, "argument must be non-negative"))
f (generic function with 1 method)

julia> f(1)
0.36787944117144233

julia> f(-1)
ERROR: DomainError with -1:
argument must be non-negative
Stacktrace:
 [1] f(::Int64) at ./none:1
```
Also the `error` function can be used, but it is not adviced since it is too general.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$db39b23b-08e8-4af2-97c9-42b7e08843eccell_id$db39b23b-08e8-4af2-97c9-42b7e08843eccode&md"""
To important control flow instructions in a loop are `break` and `continue`. The first one causes the exit from a loop, while the second one jumps directly to the next iteration of the loop, without finishing evaluating the current iteration.

```julia-repl
julia> i = 1;

julia> while true
           println(i)
		   i >= 3 && break
           global i += 1
       end
1
2
3

julia> for j = 1:1000
           println(j)
 		   j >= 3 && break
		end
1
2
3
julia> for i = 1:10
		   i % 3 || continue
           println(i)
       end
3
6
9
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$a2e86dbc-44be-41f0-95dc-cc21441c3da4cell_id$a2e86dbc-44be-41f0-95dc-cc21441c3da4codemd"""
### `else` and `finally` clauses
The `else` clause is used when you want to run something only if the `try` statement succeeds.

The `finally` clause instead is executed indipendently from if the try statement returned an error or not. This is very usefull to clean some data in case an error occured, for example to close safely a file, such that it won't be corrupted
```julia-repl
f = open("file")
try
    # operate on file f
finally
    close(f)
end
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$3466d2e0-0357-41e1-b2ca-17f7665b8d09cell_id$3466d2e0-0357-41e1-b2ca-17f7665b8d09codemd"""
## Compound Expressions.
A compound expression is a single expression tha evaluates several subexpressions in order and having the value of the last subexpression. The main Julia constructs that accomplish this are `begin` block and `;` chains
"""metadatashow_logsèdisabled®skip_as_script«code_folded$be77ed98-95bd-479c-88d7-5a35679e5281cell_id$be77ed98-95bd-479c-88d7-5a35679e5281codemd"""
Another common function is `enumerate` which yields both the index of the collection and the value
```julia-repl
julia> for (i, val) in enumerate(["a", "b", "c"])
           println((i, val))
       end
(1, "a")
(2, "b")
(3, "c")
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$24e8a309-3e0a-408d-be70-5b679c1dec0dcell_id$24e8a309-3e0a-408d-be70-5b679c1dec0dcodemd"""
if blocks do not create a new scope, so variables defined inside them are still accessible outside, for example we can redefine the function test as follow
```julia-repl
julia> function test(x,y)
           if x < y
               relation = "less than"
           elseif x == y
               relation = "equal to"
           else
               relation = "greater than"
           end
           println("x is ", relation, " y.")
       end
test (generic function with 1 method)

julia> test(2, 1)
x is greater than y.
```
Remeber to always insert a definition to the variable in each path otherwise you could have errors of wrong behaviours. For example, if we hadn't added a default case (else) in the if, in case `x > y` the variable relation wouldn't be defined and that would create an error. Worst case is if that variable existed in an outer scope, in that case we wouldn't recieve an error and the program would continue to run, but it would have the wrong behaviour (remember: the absence of an error is worse than an error) 
"""metadatashow_logsèdisabled®skip_as_script«code_folded$f89c8a46-5f78-4b36-82f5-f1465e4e3b97cell_id$f89c8a46-5f78-4b36-82f5-f1465e4e3b97codemmd"""
## Conditional Evaluation
Conditional evaluations allows to decide which portion of code should be evaluated based on the veridicity of a predicate. The most common example is the `if`-`elseif`-`else`:
```julia-repl
julia> function test(x, y)
           if x < y
               println("x is less than y")
           elseif x > y
               println("x is greater than y")
           else
               println("x is equal to y")
           end
       end
test (generic function with 1 method)

julia> test(1, 2)
x is less than y

julia> test(2, 1)
x is greater than y

julia> test(1, 1)
x is equal to y
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$e5454d2e-5b75-4224-ba08-6ddb8a051f45cell_id$e5454d2e-5b75-4224-ba08-6ddb8a051f45codexmetadatashow_logsèdisabled®skip_as_script«code_folded$d7cf560e-e576-4594-9be2-f726586238e2cell_id$d7cf560e-e576-4594-9be2-f726586238e2code(a = 1; b = 2; a + b)metadatashow_logsèdisabled®skip_as_script«code_folded$02e50952-2cb9-4807-b35c-56510690948dcell_id$02e50952-2cb9-4807-b35c-56510690948dcodeْmd"""
Unlike other languages, Julia requires the condition to be of type `Bool`, it is not sufficient that the condition is convertible to it.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$b8779469-c1a8-4f0d-8927-3fbf17aa4d0acell_id$b8779469-c1a8-4f0d-8927-3fbf17aa4d0acode.md"""
It is not rare the need to iterate over more than one collection, this can be achieved by using the function `zip`:
```julia-repl
julia> for (j, k) in zip([1 2 3], [4 5 6 7])
           println((j,k))
       end
(1, 4)
(2, 5)
(3, 6)
```
the loop exits when one of the collections is exhausted
"""metadatashow_logsèdisabled®skip_as_script«code_folded$0a24c131-c50d-48e3-803d-d77f8dc12034cell_id$0a24c131-c50d-48e3-803d-d77f8dc12034codemd"""
Julia also offers an easy way to write nested loops:
```julia-repl
julia> for i = 1:3, j = i:3
           println((i, j))
       end
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
```
Mind that a break statement would cause the exit from all the nested loops.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$ed94aa97-b071-4ea8-b468-9fdf0375085ccell_id$ed94aa97-b071-4ea8-b468-9fdf0375085ccode	md"""
Julia also supports the ternary operator `?:` which is equivalent to an `if`-`else` block but it can be execute in one line. For example an easy way to get the maximum between 2 numbers is:
```julia-repl
julia> x = 3; y = 2;
julia> z = x > y ? x : y
3
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$f39a1f46-e7d6-4875-8835-e303cedf92dacell_id$f39a1f46-e7d6-4875-8835-e303cedf92dacodemd"""
## Exception Handling
Errors are one of the most important thing in a programming language, probably there is no worse thing than seeing your program crush or return wrong results, without an explanation.

In Python, errors are raised so that the user can see them, in Julia they are thrown at you, unless you don't catch them.

Errors always come with a backtrace (also called stacktrace), which is the list of the called functions from the main to where the error has taken place, helping the user in debugging the code.

A list of the built-in exceptions in Julia can be found [here](https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions)
"""metadatashow_logsèdisabled®skip_as_script«code_folded$305a9bf1-16dc-4239-a89f-d2679554d343cell_id$305a9bf1-16dc-4239-a89f-d2679554d343coderlet z_local = z #I'm calling it z_local to make a distintion, but we could use shadowing
	x = z_local*2-x
	x-3
endmetadatashow_logsèdisabled®skip_as_script«code_folded$ce7f57b1-9b2c-4138-ad37-46a22982db18cell_id$ce7f57b1-9b2c-4138-ad37-46a22982db18codeCmd"""
## Repeated Evaluation: Loops
Loops evaluate a portion of code each time the evaluation of a condition is `true`. There are 2 types of loops in Julia which are `for` and `while`. Note that both of them create a soft local scope.

```julia-repl
julia> i = 1;

julia> while i <= 3
           println(i)
           global i += 1
       end
1
2
3
```

```julia-repl
julia> for i = 1:3
           println(i)
       end
1
2
3
julia> for i in [1,4,0]
           println(i)
       end
1
4
0

julia> for s ∈ ["foo","bar","baz"]
           println(s)
       end
foo
bar
baz
```
"""metadatashow_logsèdisabled®skip_as_script«code_folded$721109c6-5b68-47b7-bfaa-09a6dbf94ba9cell_id$721109c6-5b68-47b7-bfaa-09a6dbf94ba9codebegin
	t = @async (sleep(5), println("done"))
	sleep(0.1)	# give it time to start
	println("is task started? $(istaskstarted(t) ? "yes" : "no")")
	println("in the mean time")
	for i in 1:10
		println(i)
	end
	wait(t)
endmetadatashow_logsèdisabled®skip_as_script«code_folded$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0cell_id$d23d1cdf-9cf4-4181-b1ef-2b969db5bdd0codeBmd"""
### The `try/catch` statement
If we fear that a code of block my return an error, we can `try` it, and in case it does, we can change something.

```julia-repl
julia> sqrt_second(x) = try
           sqrt(x[2])
       catch y
           if isa(y, DomainError)
               sqrt(complex(x[2], 0))
           elseif isa(y, BoundsError)
               sqrt(x)
		   else
			   retrhow(e)
           end
       end
sqrt_second (generic function with 1 method)

julia> sqrt_second([1 4])	# this executes smoothly
2.0

julia> sqrt_second([1 -4])	# this raises DomainError
0.0 + 2.0im

julia> sqrt_second(9)		# this raises BoundsError
3.0

julia> sqrt_second(-9)		# this raises a BoundsError first, and a DomainError when recalled
ERROR: DomainError with -9.0:
sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
[...]
```
!!! note
	When catching expressions, remember to retrhow the ones you did not handled. The difference wrt trhow is that retrhow appends to the exiisting stacktrace
"""metadatashow_logsèdisabled®skip_as_script«code_folded$6b1d7868-5563-499a-9a90-20250b6210bccell_id$6b1d7868-5563-499a-9a90-20250b6210bccodemd"""
Similar to `begin` expression, there is the `let` block, the main difference is that `let` creates a new hard scope while `begin` does not create a new scope. Given the last fact, `let` allows to define local variables from already existing variables.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$78cec08e-08b5-11f0-062d-65200c1ff50ccell_id$78cec08e-08b5-11f0-062d-65200c1ff50ccodeG# The main reference for this argument will be [this](https://docs.julialang.org/en/v1/manual/control-flow/#man-compound-expressions) page from Julia Documentation.
md"""
# Control Flow
The flow of a program is which expressions are executed and in what order, the easiest example of a control flow expression is the if/else block, where which expression should be executed is based on the veridicity of a predicate.

In Julia the control flow expressions are the following:
* Compound Expressions: `begin` and `;`
* Conditional Evaluation: `if`-`elseif`-`else` and `?:` (ternary operator)
* Short-Circuit Evaluation: logical operators `&&` ("and") and `||` ("or")
* Repeated Evaluation (Loops): `while` and `for`
* Exception Handling: `try`-`catch`, `error` and `throw`
* Tasks: `yieldto`

The first 5 mechanisms are standard in high-level programming languages, that is not true for `Tasks`, therefore they will be added to the notebook for the sake of completeness, but it is not excepted from neo-progammers to be able to understand them.

It is useful to have a formal definition of what an expression and statemant are:
> An expression in programming is a combination of literals, variables, operators, and function calls that produce a value when evaluated.

> Statement are similar to expressions but do not produce a value when evaluated.
"""metadatashow_logsèdisabled®skip_as_script«code_folded$bcfb1e5a-621c-41da-82e9-7ea2b968a812cell_id$bcfb1e5a-621c-41da-82e9-7ea2b968a812codez = 5metadatashow_logsèdisabled®skip_as_script«code_folded$c245f4d9-80af-41c4-b7cd-05192eec8cd8cell_id$c245f4d9-80af-41c4-b7cd-05192eec8cd8codemd"""
an example of the usage of these operators is the following:
```julia-repl
julia> function fact(n::Int)
           n >= 0 || error("n must be non-negative")
           n == 0 && return 1
           n * fact(n-1)
       end
fact (generic function with 1 method)

julia> fact(5)
120

julia> fact(0)
1

julia> fact(-1)
ERROR: n must be non-negative
Stacktrace:
 [1] error at ./error.jl:33 [inlined]
 [2] fact(::Int64) at ./none:2
 [3] top-level scope
```
From this usage we see that it is not important for the last element to be boolean, but the first one still needs to be boolean. Also, they do not always evaluate to a type `Bool`, but just like `if` blocks, they evaluate to the last evaluated term
"""metadatashow_logsèdisabled®skip_as_script«code_foldedënotebook_id$79d73d9a-2f06-11f0-2fd8-396e560e3573in_temp_dir¨metadata