## tintincute

i = 1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
}

I would like to know what's wrong with this code?
Is "i" here the loop variable?

I'm also not sure what's the function of "j" and "k" here. Could you also please explain it to me? I know they are variables...but don't get their function here in the code.
Thanks ;-)

## ddanbe 2,720

This while loop continues forever, as i is always equal to 1. You must change your loop variable in some way, in order to finish your loop. Mostly this is done by adding 1 like in i = i +1; or i++; in your loop.
J and k are variables that never get used here. In the above situation : j=0 and k=-1 and your loop never stops.

## luvuyomaq

start quote:

``````i = 1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
}
``````

I would like to know what's wrong with this code?
Is "i" here the loop variable?

I'm also not sure what's the function of "j" and "k" here. Could you also please explain it to me? I know they are variables...but don't get their function here in the code.
Thanks ;-)

i is the only problem in the code.just get a way to change its value increment it or assign it to a formula. Most importantly know what you want your code to do.have an objective and you wont have parts that you dont understand.

## tintincute

ok so J & K doesn't play any important roles here?
I would like to correct the code. I would like to know if this correct.
Thanks

int i;
i = 1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i = i + 1;
Console.WriteLine("The value of i: " + i);
}

## mvmalderen 2,072

A shorter way to write `i = i + 1;` : `i++;`

## ddanbe 2,720

Your code still would not compile
int i;
i = 1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i = i + 1;
Console.WriteLine("The value of i: " + i);
}
Rewrite it like this:

``````int i =1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i++;;
Console.WriteLine("The value of i: " , i);
}``````

It will print the values 1 to 10 to the console.

## tintincute

thanks ddanbe, but i = i + 1 is the same as i++ is that correct?

## mvmalderen 2,072

thanks ddanbe, but i = i + 1 is the same as i++ is that correct?

Yes, that's absolutely right !

## ddanbe 2,720

Yes, i++ is just a shorthand for i = i + 1.
It was invented to make the life of newbees a bit harder...

## MosaicFuneral 812

Yes, i++ is just a shorthand for i = i + 1.

Well.... if I remember right, `var++` and `var--` on x86 style machines will generate INC and DEC instead of SUB and ADD. Which do not affect the carry flag and by not having a following value taking space: it keeps both written code and the generated executable leaner.

## ddanbe 2,720

Your erudition is beyond believe MosaicFuneral.
But I believe the discussion about what instructions are generated by a compiler by the code you write is not at hand here.
In fact I was just joking.
B.t.w. IL-code is generated, perhaps it is the same for i++ or i=i+1.
I should check that someday. The JITcompiler is the one who generates the INC or ADD instructions.

## tintincute

Your code still would not compile
int i;
i = 1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i = i + 1;
Console.WriteLine("The value of i: " + i);
}
Rewrite it like this:

``````int i =1;
while ( i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i++;;
Console.WriteLine("The value of i: " , i);
}``````

It will print the values 1 to 10 to the console.

ok. if i = i + 1 and i++ are the same, why I can't write in my code the shorthand? And why does my code will not compile?

Thanks again

## ddanbe 2,720

J and k have also have to be declared.
WriteLine also has to contain a placeholder {0} for the variable i.
You have to put in somethin like a ReadKey method to let the console window stay on the screen.
Here is the code:

``````static void Main(string[] args)
{
int i = 1;
int j;
int k;
while (i < 10)
{
j = i * i - 1;
k = 2 * j - 1;
i++;
Console.WriteLine("The value of i: {0}", i);
}
}``````

## tintincute

thanks ddanbe:-) now I understand.
it's obvious to me now, as I keep on reading the code. I can see slowly the connections.
It's fun learning C#;-)

## ddanbe 2,720

Shure it is. I am learning myself all the time!

## MosaicFuneral 812

But I believe the discussion about what instructions are generated by a compiler by the code you write is not at hand here.

True, just view my post as irrelevant or a minor side note of what might happen. What other time do I get to actually use this weird info, I've learned?