Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labeled statements #105

Merged
merged 1 commit into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions codes/LabeledStatements.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Labeled Statements
//
// In Swift, you can nest loops and conditional statements inside other
// loops and conditional statements to create complex control flow structures.
// However, loops and conditional statements can both use the break statement
// to end their execution prematurely. Therefore, it is sometimes useful to be
// explicit about which loop or conditional statement you want a break statement
// to terminate. Similarly, if you have multiple nested loops, it can be useful
// to be explicit about which loop the continue statement should affect.

// To achieve these aims, you can mark a loop statement or conditional statement
// with a statement label. With a conditional statement, you can use a statement
// label with the break statement to end the execution of the labeled statement.
// With a loop statement, you can use a statement label with the break or
// continue statement to end or continue the execution of the labeled statement.

// A labeled statement is indicated by placing a label on the same line as the
// statement’s introducer keyword, followed by a colon.

// Here’s an example of this syntax for a for loop, although the principle is
// the same for all loops and switch statements:

loopLabel: for i in 1...10 {
for j in 1...10 {
let product = i * j
print("Product is \(product)")
break loopLabel
}
}

// prints: Product is 1
Binary file added screenshots/LabeledStatements.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.