<div dir="ltr">







<p class=""><span class="">An example for illustration:</span></p><p class=""><span class="">enum</span><span class=""> Test {</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> None</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> Integer(Int)</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> StringValue(String)</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> Addition(Int, Int)</span></p><p class=""><span class="">}</span></p><p class=""><span class="">func</span><span class=""> test(a:</span><span class="">Test</span><span class="">) {</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">switch</span><span class=""> a {</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> .None: </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> .Integer(</span><span class="">let</span><span class=""> value) </span><span class="">where</span><span class=""> value &gt; </span><span class="">50</span><span class="">: </span><span class="">print</span><span class="">(value)</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> .StringValue: </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  </span><span class="">case</span><span class=""> .Addition(</span><span class="">let</span><span class=""> operands): </span><span class="">print</span><span class="">(operands.</span><span class="">0</span><span class=""> + operands.</span><span class="">1</span><span class="">)</span></p><p class=""><span class="">  </span><span class="">default</span><span class="">: </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">case</span><span class=""> .None = a {</span></p><p class=""><span class="">    </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">case</span><span class=""> .Integer = a {</span></p><p class=""><span class="">    </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">case</span><span class=""> .Integer(</span><span class="">let</span><span class=""> value) = a </span><span class="">where</span><span class=""> value &gt; </span><span class="">50</span><span class=""> {</span></p><p class=""><span class="">    </span><span class="">print</span><span class="">(value)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">case</span><span class=""> .Addition(</span><span class="">let</span><span class=""> operands) = a  {</span></p><p class=""><span class="">    </span><span class="">print</span><span class="">(operands.</span><span class="">0</span><span class=""> + operands.</span><span class="">1</span><span class="">)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">  </span></p><p class=""><span class="">  </span><span class="">if</span><span class=""> </span><span class="">case</span><span class=""> .StringValue = a {</span></p><p class=""><span class="">    </span><span class="">print</span><span class="">(a)</span></p><p class=""><span class="">  }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">test</span><span class="">(</span><span class="">Test</span><span class="">.</span><span class="">StringValue</span><span class="">(</span><span class="">&quot;Hello&quot;</span><span class="">)) </span><span class="">// StringValue(&quot;Hello&quot;)</span></p><p class=""><span class="">test</span><span class="">(</span><span class="">Test</span><span class="">.</span><span class="">Integer</span><span class="">(</span><span class="">70</span><span class="">)) </span><span class="">// 70 &amp; Integer(70) &amp; 70</span></p><p class="">















































</p><p class=""><span class="">test</span><span class="">(</span><span class="">Test</span><span class="">.</span><span class="">Addition</span><span class="">(</span><span class="">10</span><span class="">, -</span><span class="">20</span><span class="">)) </span><span class="">// -10 &amp; -10</span></p></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 24, 2016 at 12:51 PM, Ramakrishna Mallireddy <span dir="ltr">&lt;<a href="mailto:ramakrishna.malli@gmail.com" target="_blank">ramakrishna.malli@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I find the swift grammar at the end of Swift Book very useful. This is the first time I have read a programming language grammar and I really felt why I haven&#39;t done this for my earlier programming languages. For couple of months I have just read the Language guide about using general constructs of swift language, but after reading the grammar and implemented a lexer &amp; scanner(i.e parser, still in development) in swift my understanding of swift syntax is very good.</div><div><br></div><div></div><div>Initially it was difficult to understand some parts of the grammar and its terminology as it was mostly used in compiler development. but apple really made it easy as there are no left recursive terminals.</div><div>Hence forth I decided to first learn the grammar along with its construct guide for any language I am going to learn in the feature.</div><div><br></div><div>The Swift Programming Language book Pages: 848 &amp; 852 explains the if and case-condition clause syntax.</div><div><br></div><div>Note: ‹› stands for optional syntax.</div><div><br></div><div>Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.1).” iBooks. <a href="https://itun.es/in/jEUH0.l" target="_blank">https://itun.es/in/jEUH0.l</a><br></div><div><br></div><div><b>If syntax:</b></div><div><div>  “if <b>condition-clause</b> code-block ‹else-clause›”</div><div><br></div></div><div><b>case-condition</b>: (is a condition-clause, along with others) <br></div> &quot;case pattern initializer ‹where-clause›&quot;<br><div><br></div><div>Once you understand how to deal with the grammar, then you won&#39;t search for language construct example or tutorials.</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 24, 2016 at 11:56 AM, Ramakrishna Mallireddy <span dir="ltr">&lt;<a href="mailto:ramakrishna.malli@gmail.com" target="_blank">ramakrishna.malli@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I am not sure if this helps, This is how I understand it.<div><br></div><div>what happens when you use normal switch.</div><div><br></div><div>switch someEnumExpression {</div><div> case someEnum.Element1: execute block / statements </div><div> default: executeDefault</div><div>}</div><div><br></div><div>// it checks each case with someEnumExpression, and if true then run it, may be actual implementation might use dictionary with someEnum as Key</div><div>if case someEnum.Element1 == someEnumExpression {</div><div>  execute block / statements </div><div>} else {</div><div>  executeDefault</div><div>}</div><div><br></div><div>unroll your switch to if&#39;s, then I am sure you will understand its construct...but why swift provide this control branch statements with case, because at time I am sure that or only want to check an expression is of particular enum case, then I use the if case like above. while is similar.</div><div><br></div><div>I am sorry if my explanation is vague.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><span>On Wed, Feb 24, 2016 at 10:49 AM, Thomas# Chiang via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br></span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><div dir="ltr"><div>have any document explain about &quot;while case&quot; and &quot;if case&quot; ? thank you ~<br><br></div>TMS#<br></div>
<br></span>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>