![]() |
How loops can be beneficial in a program? |
[Edit] |
Answer
you use loops to draw out information from an array or a vector. Also they can be used to count up or down from any giving number.
#include <iostream>
using namespace std;
int main()
{
short a = 1;
short b = 1;
short c = 2;
//do
while (a && b && c != 34);
{
a = c + b;
cout << a << "\n";
b = a + c;
cout << b << "\n";
c = b + a;
cout << c << "\n";
}//while (a && b && c != 34);
return 0;
}
the output of this program would be
3
5
8
13
21
34
if i was to rewrite this program with out the loop, i would have to write the body of the loop
a = c + b;
cout << a << "\n";
b = a + c;
cout << b << "\n";
c = b + a;
cout << c << "\n";
over and over again until i reached the number 34. its adding 1 + 1 = 2
2+1=3, 3+2=5 and so on, thats how the program is reaching 34.
First answer by ID414340462. Last edit by ID414340462. Question popularity: 8 [recommend question]





