Working with For Loops

I am trying to write code that uses a for loop, starting with user input, that will each manipulation of the input over each iteration. For example if I wanted to write a for loop that multiplies the input by two until you exceed 100. This is what I imagine it should look like. What I would like is the output to be something like: 4,8,16,32,64 (with an input of 2). I don't want to get too caught up on how this specific program would work, rather I want to know how I can continue to manipulate the variable in the for loop and print each manipulation. Thanks

1
2
3
4
5
6
7
int input;
int value;

for(int i = input; i<100; i++){
  value= input*2;
}
Last edited on
Here's how you can add input:

1
2
3
4
5
6
7
int input;
int value;
std::cout << "Enter input: ";
std::cin >> input;
for(int i = input; i<100; i++){
  value= input*2;
}


Now you can add some numbers to your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	int input;
	int value;
	cout << "Enter input: ";
	cin >> input;
	for(int i = 0; value < 100; i++)
	{
		input *= 2; //same as input = input *2
  		value = input;
  		cout << value << endl;
	}	
}
To print each manipulation you could do something like this:
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
#include <iostream>

using namespace std;

void pause();


int main()
{
	int input;
	int value;

//.....
	cin >> input;
	cout << "\nInitial value is: " << input;

	for (value = input * 2; value < 100; value *= 2)//for each iteration, value will be multiplied by 2 until value is greater or equal to 100
	    {	                            
			std::cout << "\nValue is now: " << value;//prints on screen the variable value for each iteration of the for loop
	    }
//.....


	return 0;
}


This is one way of doing it.

Output:
2[Enter]

Initial value is: 2
Value is now: 4
Value is now: 8
Value is now: 16
Value is now: 32
Value is now: 64


EDIT:

Watch out for the following things:

__In your for loop you created a new int i as an iterator, you don't actually need it since you want to
check if value is lower than 100. So it's more logical to have value as your iterator.
__In your for loop still, you don't want to increment your iterator by one since you don't want your
value to go like (2, 3, 4, 5, .... 99), you want your value (which is also your iterator) to double each
time. Hence the use of value *= 2 and not value ++.
__Any time you want to print something you can use cout << "Print me"; asuming
you are using namesapce std;, otherwise you'd use std::cout << "Print me";
Last edited on
One of the great things about C++'s for construct is that it's very general. It doesn't always have to be a simple counter, and each of the parts is optional. You should think of a for loop as a way to combine initiation, test, and iteration, not simply as a way to count up by ones. In your program if you want to initialize a loop iterator from a stream and double it each time then just do it:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int
main()
{
    int value;
    std::cout << "Enter input: ";
    for(std::cin >> value; value<100; value *= 2){
	std::cout << value << ' ';
    }
    std::cout << '\n';
}

I wasn't aware that you could initialise a loop from a stream, that's pretty interesting actually!
1
2
for (init; test; increment)
    body;

is almost exactly the same as:
1
2
3
4
5
6
7
{
    init;
    while (test) {
        body;
        increment;
    }
}

In particular, init and increment can be any expression.
Topic archived. No new replies allowed.