[swift-users] while case and if case usage

Ramakrishna Mallireddy ramakrishna.malli at gmail.com
Wed Feb 24 01:21:01 CST 2016


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'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 &
scanner(i.e parser, still in development) in swift my understanding of
swift syntax is very good.

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.
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.

The Swift Programming Language book Pages: 848 & 852 explains the if and
case-condition clause syntax.

Note: ‹› stands for optional syntax.

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.1).”
iBooks. https://itun.es/in/jEUH0.l

*If syntax:*
  “if *condition-clause* code-block ‹else-clause›”

*case-condition*: (is a condition-clause, along with others)
 "case pattern initializer ‹where-clause›"

Once you understand how to deal with the grammar, then you won't search for
language construct example or tutorials.

On Wed, Feb 24, 2016 at 11:56 AM, Ramakrishna Mallireddy <
ramakrishna.malli at gmail.com> wrote:

> I am not sure if this helps, This is how I understand it.
>
> what happens when you use normal switch.
>
> switch someEnumExpression {
>  case someEnum.Element1: execute block / statements
>  default: executeDefault
> }
>
> // it checks each case with someEnumExpression, and if true then run it,
> may be actual implementation might use dictionary with someEnum as Key
> if case someEnum.Element1 == someEnumExpression {
>   execute block / statements
> } else {
>   executeDefault
> }
>
> unroll your switch to if'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.
>
> I am sorry if my explanation is vague.
>
>
> On Wed, Feb 24, 2016 at 10:49 AM, Thomas# Chiang via swift-users <
> swift-users at swift.org> wrote:
>
>> have any document explain about "while case" and "if case" ? thank you ~
>>
>> TMS#
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160224/b91b99b3/attachment.html>


More information about the swift-users mailing list