Let's understand the switch statement!

This time, I learned about switch statements, so I will output them.

What is a switch statement?

*** In a nutshell, a switch statement is a control syntax that uses a pattern to switch an executable statement according to the value of a control expression. *** ***

The basic writing method is as follows

qiita.rbvar


switch control type{
case pattern 1:
Statement executed when the control expression matches pattern 1

case pattern 2:
Statement executed when the control expression matches pattern 2

default:
Statement executed when the control expression does not match any pattern
}

The switch statement has the characteristic that once it matches and the execution statement is executed, the matching ends and the subsequent patterns are skipped. The if statement and guard statement were branches into two cases, whether or not they hold, but the *** switch statement can branch into more cases. *** ***

Let's look at a simple example next

qiita.rbvar


let a = -2
switch a {

case Int.min..<0:
print("is a negative value of a")

case 1..<Int.max:
print("a is a positive value")

default:
print("a is 0")

}

Execution result:a is a negative value

where keyword

You can use the where keyword to add conditions that match the case. That's all, so let's take a look at the basic syntax right away!

qiita.rbvar


switch control type{
case pattern control expression:
Control expression matches pattern 1,And,Statement executed when the conditional expression is satisfied

default:
Statement executed when the control expression does not match any pattern

The following is a simple example.

qiita.rbvar


let optionalA: Int? = 5

switch optionalA {

case .some(let a)where a > 10:
print("Value greater than 10\(a)Exists")
default:
print("Value does not exist,Or less than 10")

Execution result:Value does not exist,Or less than 10

In the above example, the constant optionalA has a value, so it matches the *** case .some (let a) *** part, but does not meet the condition where a> 10.

break statement

A break statement is a statement that interrupts the execution of a switch statement case. Let's look at a simple example.

qiita.rbvar


let a = 1
switch a{
case 1: 
print("Login was successful")
break
print("I failed to login")
default:
break
}
Execution result:Login was successful

In the above example, two print () functions are written in the matching case1: but the break statement is written before the second print function, so the second print The function will not be executed.

label

The label is a mechanism for specifying the control target of the break statement. It is used in cases where it is necessary to specify the switch statement that is the target of the break statement, such as when the switch statement is nested.

Nesting: In programming terms, *** means a structure in which the same thing is put in a certain thing. *** ***

Let's look at the basic structure!

qiita.rbvar


Label name:switch statement
break label name

Now let's look at an example where a label is needed.

qiita.rbvar


import UIKit
let value = 0 as Any

outerSwitch: switch value{
case let int as Int:
    let description: String
    switch int {
    case 1,3,5,7,9:
        description = "Odd"
   case 2,4,6,8,10:
        description = "Even"
  default:
        print("Not applicable")//Read here
        break outerSwitch
    }
print("value is\(description)is")

default:
    print("Value is not of type Int")
}

Execution result:Not applicable

In the above example, if the Any type value is an Int type from 1 to 10, it is a program that outputs whether the value is odd or even. Since the specified value is 0, it is output that the value is out of scope.

*** Now, let's change the let value specification to "aaa". *** ***

qiita.rbvar


import UIKit
let value = "aaa" as Any

outerSwitch: switch value{
case let int as Int:
    let description: String
    switch int {
    case 1,3,5,7,9:
        description = "Odd"
   case 2,4,6,8,10:
        description = "Even"
default:
        print("Not applicable")
        break outerSwitch
    }
print("value is\(description)is")

default:
    print("Value is not of type Int")//Read here
}

Execution result:Value is not of type Int

The second print function is called because the value specified in this way is not of type Int.

fallthrough statement

A fallthrough statement is a control syntax that ends the execution of a switch statement case and executes the next case.

qiita.rbvar


let a = 1
switch a{
case 1: 
print("Login was successful")
fallthrough
case 2:
print("I failed to login")
default:
print("default")
}

Execution result:
Login was successful
I failed to login

In the above example, the fallthrough statement moves execution to the next case, case2. Therefore, case1 and case2 are output.

Recommended Posts

Let's understand the switch statement!
Let's understand the if statement!
Let's understand the for-in statement!
Let's understand the function!
switch statement
Let's understand the Optional (Wrapped) type!
Let's understand closures!
Java switch statement
Switch statement range specification
Studying Java-Part 11-switch statement
Isn't the While statement evil?
Let's make the app better
12 Corresponds to the while statement
Let's try the S2Struts tutorial (# 3_180425)
Let's solve the roman numerals
Understand the helper method form_with
Let's try the S2Struts tutorial (# 5_180526)
Let's try the S2Struts tutorial (# 4_180505)
Let's solve the FizzBuzz problem!
Let's try the S2Struts tutorial (# 2_180424)
Switch the version of bundler
Let's create a TODO application in Java 5 Switch the display of TODO
Understand the basic mechanism of log4j2.xml
Dynamically switch the database to connect to
[java8] To understand the Stream API
[Java] Branch enum with switch statement
Let's verify the image search filter
Understand the difference between each_with_index and each.with_index
[Java] Really scary switch statement story
I met a great switch statement
Yes, let's preview the image. ~ part5 ~
Image processing: Let's play with the image
Let's attack the vulnerability (2) Open redirect