import{_ as s,c as t,o as a,a4 as n}from"./chunks/framework.Cy9JvDAg.js";const _=JSON.parse('{"title":"3.3.4 - 控制结构","description":"","frontmatter":{"title":"3.3.4 - 控制结构","footer":true,"lastUpdated":true,"outline":false},"headers":[],"relativePath":"3.3.4.md","filePath":"3.3.4.md"}'),e={name:"3.3.4.md"},o=n(`<h3 id="_3-3-4-控制结构" tabindex="-1">3.3.4 - 控制结构 <a class="header-anchor" href="#_3-3-4-控制结构" aria-label="Permalink to &quot;3.3.4 - 控制结构&quot;">​</a></h3><p>控制结构<strong>if</strong>，<strong>while</strong>，<strong>repeat</strong>的含义都很常见而且有相似的语法：</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span>stat ::= while exp do block end</span></span>
<span class="line"><span>stat ::= repeat block until exp</span></span>
<span class="line"><span>stat ::= if exp then block {elseif exp then block} [else block] end</span></span></code></pre></div><p>Lua也有<strong>for</strong>语句，其有两种用法（参见<a href="/lua5.4-manual-zh/3.3.5.html">3.3.5</a>）。</p><p>控制结构中的条件表达式可以返回任何值。<strong>false</strong>和<strong>nil</strong>均表示假值。所有不同于<strong>nil</strong>和<strong>false</strong>的值都表示真值。尤其数字0和空字符串也表示真值。</p><p>在<strong>repeat-until</strong>循环中，其里边的语法块不是以<strong>until</strong>关键词为结束，而是在条件语句的后面。所以其条件表达式可以引用循环语法块中声明的局部变量。</p><p><strong>goto</strong>语句用以改变程序的控制流到一个标签处。出于语法的原因，Lua中的标签也被视为语句：</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span>stat ::= goto Name</span></span>
<span class="line"><span>stat ::= label</span></span>
<span class="line"><span>label ::= ‘::’ Name ‘::</span></span></code></pre></div><p>标签在定义它的整个语法块中都是可见的，除了其内嵌的方法。goto可以跳转到任何可见的标签处，只要它没有进入另一个局部变量的作用域。不应当重复声明一个已存在的同名标签，即使这个此标签已经声明在完成的语法块中。</p><p><strong>break</strong>语句会打断<strong>while</strong>、<strong>repeat</strong>或<strong>for</strong>循环的执行，然后跳转到循环的下一条语句：</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span>stat ::= break</span></span></code></pre></div><p><strong>break</strong>只跳出最内层的循环。</p><p><strong>return</strong>语句被用来从方法或代码块（会被当成匿名函数处理）中返回值。方法可以返回多个值，所以<strong>return</strong>语句的语法为：</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code"><code><span class="line"><span>stat ::= return [explist] [‘;’]</span></span></code></pre></div><p><strong>return</strong>语句只能作为最后一条语句被写在语法块中。如果需要在语法块的中间<strong>return</strong>，你可以显式使用一个语句块，例如短句 do return end，这样现在<strong>return</strong>在（内部）语法块中就是最后一句了。</p>`,15),p=[o];function r(l,i,g,c,d,h){return a(),t("div",null,p)}const b=s(e,[["render",r]]);export{_ as __pageData,b as default};
