Вообще не понимаю цикл while | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Вообще не понимаю цикл while

Особенно этот код не понимаю что он делает, и как применяется, а точнее как он работает, мне тяжело вообще с while а этот код меня вообще из колеи выбил https://code.sololearn.com/cTrQmEUR6pMb/?ref=app

4th Nov 2020, 12:32 PM
Aleksandra
Aleksandra - avatar
3 Answers
+ 1
while(num <= 5){//if num<=5 is true then enters into loop and iteratively executed until it is false. cin>>numbe; //this is taking input into numbe total += numbe; //input numbe is added to total num++; //num value incremented //next goes to again to loop condition and all above steps are repeated } //here a sample result for every statement //num=1 /*num<=5=> 1<=5 true ,it takes input into numbe ex: 1 and that is added to total is total=1, num++=> num=2 num<=5 => 2<=5 true ,it takes input into numbe ex: 2 and that is added to total is total=1+2=3, num++=> num=3 num<=5 => 3<=5 true ,it takes input into numbe ex: 3 and that is added to total is total=3+3=6, num++=> num=4 num<=5 => 4<=5 true ,it takes input into numbe ex: 4 and that is added to total is total=6+4=10, num++=> num=5 num<=5 => 5<=5 true ,it takes input into numbe ex: 15 and that is added to total is total=10+15=25, num++=> num=6 num<=5 => 6<=5 false , so loop stop now and comes out of loop continue with next statements i.e cout prints 25
4th Nov 2020, 1:24 PM
Jayakrishna 🇮🇳
+ 1
Contd: num <= 5 => 6 <= 5 false, поэтому цикл останавливается сейчас и выходит из цикла, продолжить со следующими операторами, то есть cout печатает 25
4th Nov 2020, 1:39 PM
Jayakrishna 🇮🇳
0
while (num <= 5) {// если num <= 5 истинно, то входит в цикл и итеративно выполняется до тех пор, пока не станет ложным. cin >> numbe; // вводится число итог + = число; // входное число добавляется к итоговому число ++; // увеличенное число // далее снова переходит к условию цикла, и все вышеуказанные шаги повторяются } // вот пример результата для каждого оператора // число = 1 /* num <= 5 => 1 <= 5 true, вводится число ex: 1 и добавляется к итогу total = 1, num ++ => num = 2 num <= 5 => 2 <= 5 true, вводится число ex: 2 и добавляется к итогу total = 1 + 2 = 3, num ++ => num = 3 num <= 5 => 3 <= 5 true, вводится число ex: 3 и добавляется к итогу total = 3 + 3 = 6, num ++ => num = 4 num <= 5 => 4 <= 5 true, вводится число ex: 4 и добавляется к итоговому значению total = 6 + 4 = 10, num ++ => num = 5 num <= 5 => 5 <= 5 true, вводится число ex: 15 и добавляется к итогу total = 10 + 15 = 25, num ++ => num = 6 num <= 5 => 6 <= 5 false, поэтому цикл останавливается сейчас и выходит из цикла, (contd)
4th Nov 2020, 1:38 PM
Jayakrishna 🇮🇳