# Loops
{id: loops}

## loop
{id: loop}
{i: loop}
{i: break}

![](examples/loops/loop.cr)


## loop controls (next, break)
{id: loop-controls}
{i: next}
{i: continue}

* next  (continue)
* break (last)

## while
{id: while}
{i: while}

![](examples/loops/while.cr)

## until
{id: until}
{i: until}

![](examples/loops/until.cr)

## Exercise: Number guessing game
{id: exercise-number-guessing-game}

Level 0

* Create a file called `number_guessing_game_0.cr`
* Using the random module the computer "thinks" about a whole number between 1 and 20.
* The user has to guess the number. After the user types in the guess the computer tells if this was bigger or smaller than the number it generated, or if was the same.
* The game ends after just one guess.

Level 1

* Create a file called `number_guessing_game_1.cr`
* The user can guess several times. The game ends when the user guessed the right number.

Level 2

* Create a file called `number_guessing_game_2.cr`
* If the user hits 'x', we leave the game without guessing the number.

Level 3

* Create a file called ``number_guessing_game_3.cr`
* If the user presses 's', show the hidden value (cheat)

Level 4

* Create a file called `number_guessing_game_4.cr`
* Soon we'll have a level in which the hidden value changes after each guess. In order to make that mode easier to track and debug, first we would like to have a "debug mode".
* If the user presses 'd' the game gets into "debug mode": the system starts to show the current number to guess every time, just before asking the user for new input.
* Pressing 'd' again turns off debug mode. (It is a toggle each press on "d" changes the value to to the other possible value.)

Level 5

* Create a file called `number_guessing_game_5.cr`
* The 'm' button is another toggle. It is called 'move mode'. When it is 'on', the hidden number changes a little bit after every step (+/-2). Pressing 'm' again will turn this feature off.

Level 6

* Create a file called `number_guessing_game_6.cr`
* Let the user play several games.
* Pressing 'n' will skip this game and start a new one. Generates a new number to guess.

## Solution: Number guessing game
{id: solution-number-guessing-game}


![](examples/game/number_guessing_game_1.cr)

![](examples/game/number_guessing_game_2.cr)

