44 lines
No EOL
2.7 KiB
HTML
44 lines
No EOL
2.7 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>for … in — Ludic</title>
|
||
<meta name="description" content="Range loop — iterate the half-open range from a up to but not including b.">
|
||
<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">for … in</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-keyword">keyword</span>
|
||
<h1 id="top">for … in</h1>
|
||
</div>
|
||
<code class="sig">for name in a .. b { … }</code>
|
||
<div class="desc"><p>The numeric <code>for … in</code> loop walks the half-open range <code>a .. b</code>, running the body for each value from <code>a</code> up to <b>but not including</b> <code>b</code>. <b>Each pass binds a fresh, immutable <code>name</code></b> — it is not a reusable variable and cannot be reassigned inside the body — which makes the loop easy to reason about. Both bounds are ordinary expressions, so ranges built from constants like <code>0 .. GRID_WIDTH</code> read cleanly. The same <code>for … in</code> keyword also drives ECS query loops (<code>for (…) in query […]</code>); this page is the numeric range form. Use <code>break</code> and <code>continue</code> to exit early or skip to the next value.</p></div>
|
||
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Grid {
|
||
const GRID_WIDTH: int = 20
|
||
const GRID_HEIGHT: int = 15
|
||
const TILE_SIZE: int = 16
|
||
|
||
handler DrawWorld phase Render {
|
||
Screen.clear(Color.MidnightBlue)
|
||
for row in 0 .. GRID_HEIGHT {
|
||
for column in 0 .. GRID_WIDTH {
|
||
var tile_color = Color.MidnightBlue
|
||
if (column + row) % 2 == 0 { tile_color = Color.White }
|
||
Screen.fill_rectangle(x: column * TILE_SIZE, y: row * TILE_SIZE, width: TILE_SIZE, height: TILE_SIZE, color: tile_color)
|
||
}
|
||
}
|
||
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> |