The basics of programming in c++ for beginners

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 countermyTry. 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.

Result:

оператор 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.

Result:

оператор 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.

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

  1. I brought to mind a toy.
    I have it somehow vishla .
    #include
    #include
    using namespace std;
    int main ()
    {
    setlocale(LC_ALL, “Rus”);
    int zag = 0;
    int OTV = 0;
    cout <> zag;
    if (zag >= 1 && zag <= 7)
    {
    break;
    }
    else
    {
    cout << "Неправильный диапазон! Введите число (from 1 to 10): ";
    }

    }
    system ("cls");
    cout << "Попробуйте угадать число которое загадал ваш противник." << endl;
    cout <> oTV;
    if (oTV 10 )
    {
    mtry–;
    pop++;

    cout << "Неправильный диапазон! Введите число (from 1 to 10): ";
    }
    else if (OTV == saw)
    {
    cout << "Поздравляю.Вы угадали с " << pop++ << " attempts !";
    break;
    cin.get();
    }
    else
    {
    cout << "Вы не угадали, ische try again: ";
    mtry–;
    pop++;
    if(mtry == 0)
    {
    system ("cls");
    cout << "Закончилось количество попыток. You lose!";
    break;
    cin.get();
    }
    }
    cin.get();
    }

    }

  2. <pre class="lang:c++ decode:true "#include
    #include
    using namespace std;
    int main ()
    {
    setlocale(LC_ALL, “Rus”);
    int zag = 0;
    int OTV = 0;
    cout <> zag;
    if (zag >= 1 && zag <= 7)
    {
    break;
    }
    else
    {
    cout << "Неправильный диапазон! Введите число (from 1 to 10): ";
    }

    }
    system ("cls");
    cout << "Попробуйте угадать число которое загадал ваш противник." << endl;
    cout <> oTV;
    if (oTV 10 )
    {
    mtry–;
    pop++;

    cout << "Неправильный диапазон! Введите число (from 1 to 10): ";
    }
    else if (OTV == saw)
    {
    cout << "Поздравляю.Вы угадали с " << pop++ << " attempts !";
    break;
    cin.get();
    }
    else
    {
    cout << "Вы не угадали, ische try again: ";
    mtry–;
    pop++;
    if(mtry == 0)
    {
    system ("cls");
    cout << "Закончилось количество попыток. You lose!";
    break;
    cin.get();
    }
    }
    cin.get();
    }

    }

  3. Come on 3 once wrote, and normally does not display .
    remove comments
    That is normal

  4. How to clear the screen in Linux (as it is done in windows using system functions (“cls”); )???

    1. At first, which “screen”? – terminal?
      Secondly, in Linux, everything is much more flexible, varied and disciplined… input-output modes cm. struct termios and everything connected with it.
      A terminal screen management is done, as an option, “escape sequences” control byte. For example, cleaning the terminal screen:

      #define ESC 27
      printf( "%c[2J", ESC );

Leave a Reply

Your email address will not be published. Required fields are marked *