+ 1

How the while loop is getting executed ???

#include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; return 0; }

30th Aug 2017, 1:27 PM
Soumyadeep Chatterjee
Soumyadeep Chatterjee - avatar
6 Respuestas
+ 3
before while loop begins num=1; total=0; In while, for each loop user inputs the number and it gets added to total. i.e total =total+number; for each loop num increases by 1 when num= 6,while loop stops executing the value of total is printed
30th Aug 2017, 1:54 PM
shadab gada
+ 3
@Daniel thanks for correcting me !!
30th Aug 2017, 2:01 PM
shadab gada
+ 2
'while' begin with num=1 and it is executed, increasing num. When num is 6 then while finish
30th Aug 2017, 1:30 PM
Daniel
Daniel - avatar
+ 2
@Daniel Fernandez Hidalgo thanks for explaining.
30th Aug 2017, 1:33 PM
Soumyadeep Chatterjee
Soumyadeep Chatterjee - avatar
+ 2
@Shadab your explanation is more completed but is bad. While not stop for num=5 because condition is num<=5 so stop by num=6
30th Aug 2017, 1:56 PM
Daniel
Daniel - avatar
+ 2
Not problem @Shadab at the begining not all people understand completely difference between comparison operators for that code is <= but for your answer must be only <
30th Aug 2017, 2:05 PM
Daniel
Daniel - avatar