# Thread: Another new person - incrementing

1. ## Another new person - incrementing

I have two problems to work. This one uses n = 9 and the other one uses n = 10. ++i means to increment i from 1 to 2 before determining if the integer is odd or even but when I run the program with both numbers, I get a blank answer.

Code:
```#include <iostream>
using namespace std;
#include <conio.h>

int main()
{
int n = 9;
int i = 1;
while (i < n) {
if ((i % 2) == 0)  {
++i;
}
}
cout << i << endl;
getch();
return 0;
}```

2. Hello,

Firstly, you are running into a loop issue. Your declaration states:

While i is less than n
> If i is odd
>> Increment i

What if i isn't odd? Your loop stalls and will not increment your variable.

- Stack Overflow

The answer is no because if n = 3 then 3 % 2 is one so i will not increment and your loop will run forever.

4. I thought that it was saying if the integer was even (==0) increment i and that it would do so up to the point that i was not less than n

So if n = 9, the number is odd and the loop stalls. Wouldn't my screen display '1' since that is the assigned value to begin with?

If n = 10, then the loop would continue up to the point of (i = 10) in which case the loop would terminate and'10' would display on my screen! ???

5. Originally Posted by MB1
I thought that it was saying if the integer was even (==0) increment i and that it would do so up to the point that i was not less than n

So if n = 9, the number is odd and the loop stalls. Wouldn't my screen display '1' since that is the assigned value to begin with?

If n = 10, then the loop would continue up to the point of (i = 10) in which case the loop would terminate and'10' would display on my screen! ???
Well, think about it...when n=3, 3%2 certainly isn't zero, so i doesn't even get incremented....ever. Therefore, your loop is stuck. i will never be > 10.

6. If you haven't got the picture yet it's always good to insert cout in your code so you know what's happening. Compile the code below and you will see what you have done wrong.

Code:
```#include <iostream>
using namespace std;

int main()
{
int n = 9;
int i = 1;
int countLoops = 1;

while (i < n) {
cout << "i is: " << i << " while has looped: " << countLoops
<< " times" <<  endl;
if ((i % 2) == 0)  {
++i;
}
countLoops++;
}
cout << i << endl;

return 0;
}```
As you see 1%2 will never be 0, therefore the code never get's into the if statement and i will always be 1.