ludic/kw-if.html
2026-09-17 22:10:03 +00:00

51 lines
No EOL
2.9 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>if / else — Ludic</title>
<meta name="description" content="A branch — run one block when a condition holds, another when it doesn&#x27;t.">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="docs.css">
</head>
<body>
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
<main class="wrap item">
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="api.html#control">Control flow</a> <span>›</span> <span class="here">if / else</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">if / else</h1>
</div>
<code class="sig">if cond { … } else { … }</code>
<div class="desc"><p>An <code>if</code> runs its block when the condition is true; an optional <code>else</code> block runs when it is false, and the <code>else</code> may itself be another <code>if</code> to form a ladder. The condition is a plain boolean expression with <b>no surrounding parentheses</b>, and the braces are always required even for a single statement. Ludic's boolean operators are the words <code>and</code>, <code>or</code> and <code>not</code> (not <code>&&</code>/<code>||</code>/<code>!</code>), and note that bitwise operators bind tighter than comparison, so <code>flags & MASK == 0</code> already means <code>(flags & MASK) == 0</code>. When the branches grow into a chain testing one value against several constants, reach for <code>match</code> instead.</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Threshold {
property Health { current: int = 100, maximum: int = 100 }
model Player { Health }
handler Boot phase Start {
spawn Hero { Health { current: 25 } }
}
handler DrawWorld phase Render {
Screen.clear(Color.MidnightBlue)
for (health) in query [Health, {Player}] {
if health.current &lt;= 0 {
Screen.draw_text(x: 8, y: 8, text: "DEFEATED", color: Color.Crimson, scale: 2)
} else {
if health.current &lt; 30 {
Screen.draw_text(x: 8, y: 8, text: "DANGER", color: Color.Gold, scale: 2)
} else {
Screen.draw_text(x: 8, y: 8, text: "OK", color: Color.LimeGreen, scale: 2)
}
}
}
Screen.show()
}
}</pre></div>
<a class="back" href="api.html">← All symbols</a>
</main>
<script src="ludic-highlight.js"></script>
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
</body></html>