Things would be more interesting if we could do different things depending on the data we have.
For example, if we were coding a calculator it would be nice to ask the user if they want to add, subtract, multiply or divide and then
do what they asked for. Maybe check if the password somebody entered is right and let them log in based on that (or not). And in our
case, tell us if we are old enough to drink based on our age.
</p>
<p>
All this situations require what is called a <b>conditional</b>. A conditional is a statement that tells a programming language <i>"do this
if this happens; otherwise do this"</i>. In LDPL this is done using the <code>if - then</code> statement, together with the <code>else</code> and <code>end if</code> statements. They are used like this:
<br><spanclass="sta">display</span><spanclass="txt">"Hello there, how old are you?"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">accept</span> age
<br><spanclass="sta">if</span> age <spanclass="typ">is less than</span> 21 <spanclass="sta">then</span>
<br> <spanclass="sta">display</span><spanclass="txt">"You are too young to dring!"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">else</span>
<br> <spanclass="sta">display</span><spanclass="txt">"You are old enough to drink!"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">end if</span>
</div>
<p>
Here we use the <code>is less than</code> condition, but you can also use <code>is greater than</code>, <code>is less than or equal too</code> and <code>is greater than or equal to</code>.
You can find more information about the <code>if</code> statement <ahref="https://docs.ldpl-lang.org/control-flow-statements/if-is-then"target=_blank>on the documentation</a>.
</p>
<p>
We can make conditions more complex by adding <code>else if</code> statements. These are used always <i>after</i> an <code>if</code> statement has been used and before it's been closed (<code>end if</code>).
If the condition for the first <code>if</code> statement hasn't been fulfilled, this one is checked instead. For example:
<br><spanclass="sta">display</span><spanclass="txt">"Hello there, how old are you?"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">accept</span> age
<br><spanclass="sta">if</span> age <spanclass="typ">is less than</span> 21 <spanclass="sta">then</span>
<br> <spanclass="sta">display</span><spanclass="txt">"You are too young to dring!"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">else if</span> age <spanclass="typ">is greater than</span> 100 <spanclass="sta">then</span>
<br> <spanclass="sta">display</span><spanclass="txt">"Man, you are old enough for ANYTHING."</span><spanclass="typ">crlf</span>
<br><spanclass="sta">else</span>
<br> <spanclass="sta">display</span><spanclass="txt">"You are old enough to drink!"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">end if</span>
</div>
<p>
You can add as many <code>else if</code> statements as you want inside a single <code>if</code>, but always before <code>else</code> and <code>end if</code>.
</p>
<p>
If statements can also be used with the other data types LDPL provides. For example, if we wanted to check if the user is called "Bob", we could also use an <code>if</code> statement; like this:
<br><spanclass="sta">display</span><spanclass="txt">"What's your name? "</span>
<br><spanclass="sta">accept</span> name
<br><spanclass="sta">if</span> name <spanclass="typ">is equal to</span><spanclass="txt">"Bob"</span><spanclass="sta">then</span>
<br> <spanclass="sta">display</span><spanclass="txt">"You are called Bob!"</span><spanclass="typ">crlf</span>
<br><spanclass="sta">end if</span>
<br><spanclass="com"># ...etc...</span>
</div>
<p>
Now that you know how to use conditionals, there are two things left to learn: <b>loops</b> and <b>sub-procedures</b>. We'll tackle these in the remaining sections.