what is ++for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cmath>
using namespace std;

int i;
int randomnumber = rand()%10+1;
int main()
{
	int yourval = 22, numEven = 0;
	while (yourval > 0)
	{
		cout<<"pleae enter a number(enter negative or zero to end)\n";
			cin>> yourval;
			if (yourval % 2 !=0)
				numEven++;
	}
	cout<<"you entered"<<numEven<<"odds.\n";

}


what does numEven++ do in the program? i know it means increment but i read my text book go over thousands of c++ review on what is ++ but i still don't understand what it really does and why is it necessary (well ofcuz because when i delete that sentence the program doesn't work)but yea someone please explain it to me. Oh and explain to me the use of -- too i am very confused and it's starting to affecting me in class.
Last edited on
numEven++ is the same as saying:

numEven += 1;

or

numEven = numEven + 1;

It is just incrementing the value in the variable by 1.
numEven++ is the same as saying:
...

Well, it is slightly different. I believe incrementing is faster than addition, although the compiler likely optimizes this. Here is one thing to know:
There are circumstances where numEven++ performs differently than ++numEven, although both can be used. Here's an example:

1
2
3
4
5
6
7
int num1, num2;
num1 = 4;
num2 = num1++;//num2 is assigned before num1 is incremented
cout << num1 << ' '<< num2 << endl;
num1 = 4;
num2 = ++num1;//num2 is assigned after num1 is incremented
cout << num1 << ' ' << num2 << endl;

This will give the following as output:
5 4
5 5


Decrementing is just the opposite of incrementing.
Last edited on
I know what it "means" but i want to know what it does to the program? why need numEven++; at the end of the if function? what does it do? And what does it means when the program dont' have it?

in the if function it says

if your value don't have remainder numEven = numEven +1, it doesn't make sense to me.
Last edited on
In your piece of code, it is a "counter" to count how many even numbers the user inputs. Note that IF the number is even
yourval % 2 !=0
then the number of evennumbers
numEven
has increased.

numEven is initially zero stored in the variable. So every time you input an even number it satisfies the if condition and the ++ (increment) statement follows. This is done so at the end of the programme it prints out numEven to display how many evens there were.
Last edited on
What Sputnik said is correct. It's counting the number of even numbers, hence the name "numEven" so when yourval is even it will increment or add one to it so

 
numEven++;


being the same as

 
numEven = numEven + 1;


means that you are assigning to the variable the number it had plus 1.

-- is the opposite, being it subtracts 1. Some people prefer incrementing before, because it doesn't set a temp var so is faster when things aren't optimized... but even then it's not much of a diff..

So if numEven was 2 then numEven++ will be 3...
Last edited on
thank you very much.
Topic archived. No new replies allowed.