What operator would I use on a variable to increase it for addition from another variable. Thinking loops perhaps. Like the loop add's data to that variable but plus's it on, instead of changes it. So 50 + 60 = 110, instead of 50 + 60 = 60.

Also, if I had a for statement, what type of if would I use to display a portion of output once the for loop had finished?

Thanks

Recommended Answers

All 6 Replies

can u give example ?

Um sounds to be link you want something like this.

aVariable += anotherVariable;

Ok,

LOOP START
X=5
A=X

A=5
Loop restarts,
X=10
A=X

A=10, and so on. Also, if the loop was a FOR loop, what kind of IF would I need to recognise when it has finished looping?
Thanks

Hi LucyB, I still don't really understand what you mean. For example, in your first post, what do you mean by "So 50 + 60 = 110, instead of 50 + 60 = 60"?

From your second post, I'm assuming that you are trying to change the value of one variable, and then setting another variable equal to that value? If so, one way to do this involves storing the values you need in an array, and then accessing the elements of the array using a for loop.

#include <iostream>
using namespace std;

int main() {

int X[7] = {0,1,1,2,3,5,8};

for(int i=0; i < 7; i++) {
	int A = X[i];
	cout << "A = " << A << endl;
}
}

Um sounds to be link you want something like this.

aVariable += anotherVariable;

Hmm I tried this without success. It's along the lines of what I'm trying to do though.

Basically I want a variable to keep being incremented by another variable each time the loop repeats, without the need for arrays and more loops for those arrays etc making things more complicated. I'm trying to keep a tally.

Would it be more like variable1++avariable2;? Lets say variable2=5, then each time the program loops 5 keeps being added on to variable1.

Thanks to all.

Hey LucyB, sorry I was thinking that you wanted the increment value to change (variable => change.) In that case you would need to store the values in an array.

If the increment is constant, say 5 like you suggest, then the following code will increment the tally by 5 at each iteration of the loop.

#include <iostream>
using namespace std;

int main() {

int tally = 0;
int increment = 5;

while(tally < 40) {
	cout << "tally = " << tally << endl; 
	tally += increment;
}
}

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.