while case and if case usage

have any document explain about "while case" and "if case" ? thank you ~

TMS#

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@swift.org> wrote:

have any document explain about "while case" and "if case" ? thank you ~

TMS#

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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. ‎The Swift Programming Language (Swift 5.7) on Apple Books

*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@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@swift.org> wrote:

have any document explain about "while case" and "if case" ? thank you ~

TMS#

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

An example for illustration:

enum Test {

  case None

  case Integer(Int)

  case StringValue(String)

  case Addition(Int, Int)

}

func test(a:Test) {

  switch a {

  case .None: print(a)

  case .Integer(let value) where value > 50: print(value)

  case .StringValue: print(a)

  case .Addition(let operands): print(operands.0 + operands.1)

  default: print(a)

  }

  if case .None = a {

    print(a)

  }

  if case .Integer = a {

    print(a)

  }

  if case .Integer(let value) = a where value > 50 {

    print(value)

  }

  if case .Addition(let operands) = a {

    print(operands.0 + operands.1)

  }

  if case .StringValue = a {

    print(a)

  }

}

test(Test.StringValue("Hello")) // StringValue("Hello")

test(Test.Integer(70)) // 70 & Integer(70) & 70

test(Test.Addition(10, -20)) // -10 & -10

···

On Wed, Feb 24, 2016 at 12:51 PM, Ramakrishna Mallireddy < ramakrishna.malli@gmail.com> wrote:

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@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@swift.org> wrote:

have any document explain about "while case" and "if case" ? thank you ~

TMS#

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users