0
Sum of 5 integers, using while loop.
Please provide a breakdown and explain when while loop is used.
1 Odpowiedź
+ 2
Well, supposed you want to add 5 integers, as you stated in your heading.
Then you want to add to a variable called "result" (or whatever you like) 5 times a integer.
And this is where the loop comes in play.
You use loops when you want to use a part of code repeatedly, here 5 times.
So you make your piece of code, e.g:
cin >> number; //get number to add from user
result += number; //add it to result
and then put it inside a loop and specify how often this should run.
Way easier and shorter than manually asking 5 times for a number and adding it, right?
PS: If you want an actual code example to study, feel free to ask.