Welcome to Swift (initial translation and annotations of Apple's official Swift document 30)---219~224 page (fourth--process Control)

Source: Internet
Author: User

Labeled Statements (Label statement)

Loops or switch statements can nest other loops or switch so that a complex process control structure can be created in Swift. A loop or switch statement can be permanently terminated by a break statement, so it is sometimes necessary to explicitly Indicates that you need to break the statement in a loop or switch. Similarly, if you nest multiple loops, you explicitly indicate that the continue statements inside are similar.

To achieve this, you can use a statement label (statement label) in a loop statement or switch statement to end or continue executing the label statement using this tag and the break or continue statement.

A label statement is written after the same statement line using the label keyword, followed by a colon (:), here is an example of a while loop, which is the same rule for the switch statement and all other loops:

Label Name:while Condition {

Statements

}

The following example implements an improved version of the Snake and ladder game by using the break and continue statements in the marked while loop, and the game adds an extended rule:

Must arrive at lattice 25 before winning.

If you throw a dice over a lattice 25, you must re-throw it again until you arrive at the exact lattice 25

The game's lattice is the same as before:

The values Finalsquare, board, Square, and Diceroll are initialized as before:

Let Finalsquare = 25

var board = int[] (count:finalsquare + 1, repeatedvalue:0)

BOARD[03] = +08; BOARD[06] = +11; BOARD[09] = +09; BOARD[10] = +02

BOARD[14] =-10; BOARD[19] =-11; BOARD[22] =-02; BOARD[24] = 08

var square = 0

var diceroll = 0

This version of the game uses a while loop and a switch statement to complete the game logic. The while loop has a markup statement gameloop to show that it is the main loop of the game.

The condition of the while loop is while square! = Finalsquare, which means you must reach lattice 25:

Gameloop:while Square! = finalsquare {

if ++diceroll = = 7 {diceroll = 1}

Switch Square + diceroll {

Case Finalsquare:

//Diceroll'll move us to the final square, so the game was over

Break Gameloop

Case let newsquare where Newsquare > Finalsquare:

//Diceroll'll move us beyond the final square, so roll again

Continue Gameloop

Default:

//This was a valid move, so find out its effect

Square + = Diceroll

square + = Board[square]

  }

}

println ("Game over!")

The dice are thrown at each time of the loop instead of moving the player immediately. The switch statement is used to process the moving results and calculate the movement:

      • If the player moves to the last lattice after the dice is thrown, the game ends. The statement break Gameloop statement jumps to control the first line of code outside the while loop, which ends the game.
      • If the player moves beyond the last lattice after the dice is thrown, the player needs to throw again, continue Gameloop statement ends the current while loop and begins a loop traversal.
      • All other cases, the throw dice move, the player moves forward diceroll a lattice, the game logically checks the snakes and ladders, then loops over, controls the return to the while condition to determine whether to make a loop.

  Note the point:

If the break statement does not use the gameloop tag, it will only jump out of the switch statement, not the while statement. Use the gameloop tag to clearly indicate the control statement that will end.

At the same time, it is not strictly necessary to use Gameloop tags to jump to the next loop when calling the continue Gameloop statement. There is only one loop in the game, so there is no ambiguity in the continue statement. But the use of The Gameloop tag continue statement is still harmless. The reason for this is to contrast the break statement and to help the game logic clear, increase readability and be easy to understand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.