Break and continue statements in C++

break и continue c++The break statement in C ++

The statement ' break ' you've met, If you read our article on switch statement and about the for loop. And probably already have an idea of how, how it works. Let's talk a little more about this statement, maybe someone will be useful.

Quite often, when using loops and almost always used when the statement switch, the programmer need to interrupt the body of loop or body switch respectively. break, just used in such cases. Same, using this statement is interrupted by an infinite loop. When it is necessary to interrupt, in the body of the loop is added to the condition, in the performance of work which the operator break, and the loop completes. The following example, implements a simple game: one player thinks of a number between 1 to 7, the second should guess it.

To be honest it was and the player, which makes, could not enter the number of invalid range, run an infinite loop in which occur enter and number checking. If the number corresponds to the desired range – the loop is aborted (strings 15 – 18), if not – the screen displays a warning and ask you to enter the number again. So will occur, while the number entered will not be included in the desired range.

After entering the correct number, in string 25 call a special function system(“cls”); , which will clear the contents of the screen, before, the second player will start guessing numbers (only works in windows).

Next, we define a second endless loop (strings 29 – 47), but with the counter myTry. It will take the number of player's attempts to guess the number. The player enters the number, and it is immediately checked against the range in lines 33 – 37. If it does not – we warn and subtract one from counter attempts myTry. Otherwise, if the number entered in the desired range, check whether it complies with unknown number (strings 38 – 42): yes – congratulations and interrupt the loop, no – turn to implement strings 43 – 46 – we speak error and offer to enter the number again.


оператор break c++

оператор break c++

Continue statement in C ++

This statement is used then, when you need to interrupt (skip) the current iteration of a loop and proceed to the next iteration. When it is running in a loop for , stops the current iteration, the transition to a change in the control variable, and then checking the conditions of continuing execution loop.

To solve this task: It should display the number of those, which are divided into 7 without the balance and range from 1 to 70 .

If the number of i is not divisible by 7 without a trace (remainder of the division not equal to 0), work continue. That is the line of code 12 already will fail and we will not see the number i on the screen. If the remainder of dividing i on 7 is equal to 0 (divisible by 7 integer), body if performed and the number will not be shown.


оператор continue в C++

If you liked the article, the best for me would be greatly appreciated, If you share with your friends a link to it. This can be done by clicking on your favorite social network below the article.

Subscribe to notifications about new articles on our website:

Agree to receive notifications from to my e-mail

Break and continue statements in C++

11 thoughts on “Break and continue statements in C++

  1. Well, again, solid jambs.
    The switch can not I use the continue?
    The examples are not successful.

    To work system it is necessary to connect stdlib.h.

    system(“cls”) only works in windows, IMHO it is not necessary to write about it in the tutorials, you do not describe what the function does.

    >> myTry–; // decrement counter attempts by one meter

    Counter, counter, counter, counter

    >> simple game: One player thinks of the number of

    Not sure, but IMHO colon – this is not the point, keep in small letters.

    >> Quite often, when using cycles !!!ALWAYS!!! using a switch statement, programmer to interrupt the loop or switch body body respectively.

    switch (a) {
    case 1: ++b;
    case 2: ++b;
    default: ++c;
    does not work?

    “You write, publish”, but you must “write, read, correct, publish”.

    1. why do you continue to switch???
      “To work system it is necessary to connect stdlib.h” >> No. Here you have a partial error. stdlib.h and its analogue cstdlib need to work with memory, but in the MVS environment, you can not connect the library, as it is already precompiled in all projects by default. This example can be clearly see with setlocale(), It seems to be necessary to connect the library locale.h, but without it, everything works well. So you're not part of the rights. But the truth under Windows!

    2. >> simple game: One player thinks of the number of

      Not sure, but IMHO the colon - is not the point, keep in small letters.

      Respect,)) and again you find fault… you obviously anxious to punctuation matters)) And why was Orfus you think invented??

    3. It's great,when you want to fix something and correctly,but you have to be grateful to the author website,for clear presentation of how the newcomer materiala.Mne:easy to be trained in this site,in comparison with other sites.

      1. yes? Well if you look at the problem with a game, second picture! it seems that there logic error, or I'm wrong? unknown number 7, and guessed 3)

  2. Interesting, if so deal, and the break and continue are interruptions, but only operate differently. break entire loop interrupts and exits, and continue just misses one turn loop and does not break it.

  3. and though Vova again many times shouting about mistakes, but still valuable article ;)

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>