Question

Im trying to learn C# from scratch and I have issue with one simple task. I cant understand why this is Not working,can you please explain me :

     namespace ConsoleApplication1
{
    class intro
    {
        static void Main(string[] args)
        {
            int i;
            int j;
            for (i = 1; j=-1; i <= 100 && j >= -100; i += 2, j -=2)
            {
                Console.WriteLine(i+j);
            }
        }
    }
}     

Edit: I missed why is NOT working, sorry for that. Semicolon instead of colon was the problem... Stupid question but thank you for patience.

Was it helpful?

Solution

There is a very common mistake, you placed `;' in the place ',';

for (i = 1, j=-1; i <= 100....

I don't know what you want to do with the code but it produce only '0' but the code is working as it should.

OTHER TIPS

What do you mean by:

I cant understand why this is working

It is working because it is a valid code.Except this comma should be semicolon:

i = 1; <-- wrong
i = 1, <-- correct

You are constructing a for loop.You define two variable,i and j then,you starting i from 1 and j form -1. Then you specify the loop condition. i <= 100 && j >= -100.With this condition you are saying the loop should execute as long as i <= 100 AND j >= -100 And last statement you specifying the expression which will be executed in every step.That's it.

for loop needs to have a structure of

for (initializer; condition; iterator)
    body

Please refer to MSDN, these are the most basics of every programming language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top