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 ;-)

Recommended Answers

All 15 Replies

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.

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.

Thank you for your replies.
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);
}

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

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.

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

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

Yes, that's absolutely right !

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

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.

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.

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

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);
            }
            Console.ReadKey();
        }

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#;-)

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

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?

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.