ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 第1.2流程控制 - if else,左括号和关键词同一行 ``` <pre class="calibre14">``` <span class="token5">if</span> condition1 <span class="token2">{</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token5">if</span> condition2 <span class="token2">{</span> <span class="token2">}</span><span class="token5">else</span> <span class="token2">{</span> <span class="token2">}</span> ``` ``` - 循环for只有一个 ``` <pre class="calibre14">``` sum <span class="token2">:</span><span class="token">=</span> <span class="token3">0</span> <span class="token5">for</span> i <span class="token2">:</span><span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> i <span class="token"><</span> <span class="token3">10</span><span class="token2">;</span> i<span class="token">++</span> <span class="token2">{</span> sum <span class="token">+</span><span class="token">=</span> i <span class="token2">}</span> <span class="token5">for</span> key<span class="token2">,</span> val <span class="token2">:</span><span class="token">=</span> range coll <span class="token2">{</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span> <span class="token2">}</span> <span class="token5">for</span> y <span class="token2">:</span><span class="token">=</span> <span class="token3">1</span><span class="token2">;</span> y <span class="token"><=</span> <span class="token3">9</span><span class="token2">;</span> y<span class="token">++</span> <span class="token2">{</span> <span class="token6">// 遍历, 决定这一行有多少列</span> <span class="token5">for</span> x <span class="token2">:</span><span class="token">=</span> <span class="token3">1</span><span class="token2">;</span> x <span class="token"><=</span> y<span class="token2">;</span> x<span class="token">++</span> <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Printf</span><span class="token2">(</span><span class="token4">"%d*%d=%d "</span><span class="token2">,</span> x<span class="token2">,</span> y<span class="token2">,</span> x<span class="token">*</span>y<span class="token2">)</span> <span class="token2">}</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">}</span> ``` ``` - switch ``` <pre class="calibre14">``` var a <span class="token">=</span> <span class="token4">"hello"</span> switch a <span class="token2">{</span> case <span class="token4">"hello"</span><span class="token2">:</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token3">1</span><span class="token2">)</span> case <span class="token4">"mum"</span><span class="token2">,</span> <span class="token4">"daddy"</span><span class="token2">:</span><span class="token">/</span><span class="token">/</span>多分支 fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token3">2</span><span class="token2">)</span> default<span class="token2">:</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token3">0</span><span class="token2">)</span> <span class="token2">}</span> ``` ``` - goto 跳出多重循环; break 语句可以结束 for、switch 和 select 的代码块; continue 语句可以结束当前循环,开始下一次的循环迭代过程,仅限在 for 循环内使用;