Conditional loops in Swift- While loop

A while loop performs a set of statements until a condition becomes false. These kinds of loops are best used when the number of iterations is not known before the first iteration begins. Swift provides two kinds of while loop:

  • while evaluates its condition at the start of each pass through the loop.

  • dowhile evaluates its condition at the end of each pass through the loop.

While

A while loop starts by evaluating a single condition. If the condition is true, a set of statements is repeated until the condition becomes false.

Here’s the syntax of a while loop:

Consider the Snakes and Ladders Game.

The rules of the game are as follows:

  • The board has 25 squares, and the aim is to land on or beyond square 25.

  • Each turn, you roll a six-sided dice and move by that number of squares, following the horizontal path indicated by the dotted arrow above.

  • If your turn ends at the bottom of a ladder, you move up that ladder.

  • If your turn ends at the head of a snake, you move down that snake.

The game board is represented by an array  (collection of data of a similar data type) of Int values. Its size is based on a constant called “let finalSquare", which is used to initialize the array and also to check for a win condition later in the example. The board is initialized with 26 zero Int values, not 25 (one each at indices 0 through 25 inclusive):

Some squares are then set to have more specific values for the snakes and ladders. Squares with a ladder base have a positive number to move you up the board, whereas squares with a snake head have a negative number to move you back down the board:

Square 3 contains the bottom of a ladder that moves you up to square 11. To represent this, board[03] is equal to +08, which is equivalent to an integer value of 8 (the difference between 3 and 11). The unary plus operator (+i) balances with the unary minus operator (-i), and numbers lower than 10 are padded with zeros so that all board definitions align.

The player’s starting square is “square zero”. The first dice roll always moves the player on to the board:

This example uses a very simple approach to dice rolling. Instead of a random number generator, it starts with a diceRoll value of 0. Each time through the while loop, diceRoll is incremented with the prefix increment operator (++i), and is then checked to see if it has become too large. The return value of ++diceRoll is equal to the value of diceRoll after it is incremented. Whenever this return value equals 7, the dice roll has become too large, and is reset to a value of 1. This gives a sequence of diceRoll values that is always 1, 2, 3, 4, 5, 6,1, 2 and so on.

After rolling the dice, the player moves forward by diceRoll squares. It’s possible that the dice roll may have moved the player beyond square 25, in which case the game is over.

A while loop is appropriate in this case because the length of the game is not clear at the start of the whileloop. Instead, the loop is executed until a particular condition is satisfied.

Next we will learn about Do-While loop.