Converting these whiles into fors

Nov 1, 2019 at 11:44am
Hello how can i convert these while statements into for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
 using namespace std;
 int main()
    {
        int n1 =1;  //Declration of Variables
        int n2 = 1; //Declration of Variables
        int next = 0; //Declration of Variables
        int n;        //Declration of Variables
        cout << "Enter a positive number: "; 
        cin >> n; //Input
        cout << "Fibonacci Series: " << n1 << " " << n2 << " "; //Output
        next = n1 + n2; //Storing values
        
        while(next <= n) //While loop
        {
            cout << next << " ";
            n1 = n2;
            n2 = next;                //Body of loop
            next = n1 + n2;
        }
        
        return 0;
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <climits>
#include <iomanip>
using namespace std;

int main()
{  
    int num, count, max = 0;
    int min = INT_MAX; //Declaration of Variables
    int term = 0; //Declaration of Variables
    cout << "\n\n Input any number of integers and press 0 to see results: \n"; //Input Message
    while (cin >> num && num != term)  //While Loop Initialization
    {
        if (num > 0) 
        {
            ++count;
            if (max < num)  
                max = num;              //BODY OF LOOP
            if (min > num) 
                min = num;
        }
        else 
        {
            cout << "Error: Invalid" << endl; //Invalid input message
        }
    }
    if (count > 0) 
	{
        cout << " The maximum value is: " << max << endl; //Output
        cout << " The minimum value is: " << min << endl;; //Output
    }
}
Last edited on Nov 1, 2019 at 11:44am
Nov 1, 2019 at 11:49am
1
2
3
4
5
6
7
  while(next <= n) //While loop
        {
            cout << next << " ";
            n1 = n2;
            n2 = next;                //Body of loop
            next = n1 + n2;
        }

becomes something like
1
2
3
4
5
6
 for (int next = n1 + n2 ; next <=n ; next = n1 + n2)
{
            cout << next << " ";
            n1 = n2;
            n2 = next;                //Body of loop
}

and you remove all other mentions of next from your code.
Nov 1, 2019 at 2:11pm
all whiles can be crudely converted with a partial for loop:
for(; condition ;)
{
as you were
}

it is a little cleaner to move some of the variables to the for loop as shown, but its not required.

1
2
3
4
5
6
7
for(;next <= n;) //While loop
        {
            cout << next << " ";
            n1 = n2;
            n2 = next;                //Body of loop
            next = n1 + n2;
        }


do whiles are more trouble and usually end up with
set condition to be true
for()

eg
x = 10; //force loop to run one time here
for(;x == 10;) //do while x == 10
{
modify(x);
}
Last edited on Nov 1, 2019 at 2:14pm
Nov 1, 2019 at 3:11pm
You had trouble with logic in http://www.cplusplus.com/forum/beginner/264756/
Someone posted a while-loop and we did suggest that you convert it into for-loop, vaguely hoping that you were up to that task.

Perhaps we should have pointed you into study material: http://www.cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.