+ 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; }
6 Respostas
+ 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
+ 3
@Daniel thanks for correcting me !!
+ 2
'while' begin with num=1 and it is executed, increasing num. When num is 6 then while finish
+ 2
@Daniel Fernandez Hidalgo thanks for explaining.
+ 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
+ 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 <