We have already considered one of the repetition structures in C ++ – for loop. In this article, I'll look at two loops: while and do while. They are very similar to each other. Their syntax:
The only difference is that, that at the beginning of the loopwhile, the conditional test occurs immediately. If it returns false (false), the body of the loop does not execute even once. And when using do while, immediately execute operator(-s) in the loop, and then it will check the condition in parentheses ( ) after the keyword while. And similarly, when returning false, loop shuts down, otherwise – runs the next step (iteration). That is, body of the loop do while executed at least once, in any case.
Let's talk about them separately. Let's start with the while. As mentioned above, to perform body of the loop , need to check if the conditions in parentheses ( ), it was returned to the truth (true).
Check the conditions and performance of the body will occur, until the condition will return false. From this comes out, that to break the loop, his body anything should occur, that will affect the test conditions.
This may be either entered by the user from the keyboard variable, which is present in the test loop provided. Or it may be an increase or decrease of the manipulated variable, which is also involved in the test condition.
Consider the case of changing the value of the manipulated variable in the loop. Formulation of the task: the user wishes to refill the card account through the terminal. He enters the amount from the keyboard. Provide the ability to change the program mistakenly entered the amount.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); double payment = 0; int exit = 1; // управляющая переменная while (exit == 1) { cout << "Введите сумму для пополнения счета: "; cin >> payment; cout << "Оплатить и выйти - нажмите 0. Изменить сумму - нажмите 1: "; cin >> exit; // изменение управляющей переменной } cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"; cout << "Ваш счет пополнен на " << payment << "$ Спасибо! До свидания!"; cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"; return 0; } |
The control variableexit declares and initializes the unit to loop while – string 9. Therefore, when the loop, check conditions while (exit == 1) return true (More details about this can be read read here) and you will be taken to implement the loop body. This condition is often written in such a way: while (exit). With such a condition of the program works, as with the previous. The reason is, that any variableexitthan zero will be treated by the compiler, how true. The body of the loop will be executed, until the user changes the value of this variable at 0. Then while (exit) It would be tantamount towhile (0). That is, the verification condition is false. This record is more common, because it is shorter, although not so clear, how and, in the example that. For this you just have to get used.
In strings 16 – 17 the user is asked and offered to make a choice: cout << “Pay and get out – press 0. Change amount – press 1: “; That is, if he made a mistake when entering the replenishment, he has the opportunity to correct it, clicking 1.
Once the user enters the correct amount and clicks 0 (that corresponds to the “Pay and get out”), exit the loop occurs and the program will continue to work. As a result, the screen will display a message on the amount of deposit – strings 20 – 22.
Result:
The same task can be accomplished using a loop do while. In this case, initial value of the variable exit It can be anything. Loop in any case carry out the body and offer to make a choice. Look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int main() { setlocale(LC_ALL, "rus"); double payment = 0; int exit = 0; do { cout << "Введите сумму для пополнения счета: "; cin >> payment; cout << "Оплатить и выйти - нажмите 0. Изменить сумму - нажмите 1: "; cin >> exit; } while (exit == 1); cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"; cout << "Ваш счет пополнен на " << payment << "$ Спасибо! До свидания!"; cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"; return 0; } |
Executing the program, see, that despite the fact, that the variable exit initially equal to 0, loop body do while executed. This is because, that verification of the conditions below is the body – in string 18. Note the semicolon ; after the closing parenthesis. For the loop do while it is obligatory.
Comparing loops for, whileanddo while it is seen, what they look like. Only in the loop for all necessary for its normal operation assembled on the same line (declaration and initialization of the control variable, verification condition, changes in the control variable).
Whereas, in loopswhile and do while the control variable is declared before the loop, and its change occurs later in the body. What kind of design use of repetition – you decide. Most loopswhileand do while then apply, when initially unknown, how many repetitions will (as in our example). It should still be remembered, that unlike the loops whileanddo while , control variable of loopfor not visible outside its body (that is, when the output of the loop, she is destroyed).
We also offer the same training video on the theme of loops in C ++. As it considered the loop for:
Leave your questions in the comments. If possible, share the article with your friends on social networks. Tasks on the subject already have!
pmjkmg