sequence for statement

Asked 2 years ago, Updated 2 years ago, 44 views

using System;
class CodeFile1
{
    static void Main()
    {
        int[] sample = new int[100];
        inti;

        for(i=0;i<10;i=i+1);
        sample[i] = i;

        for(i=0;i<10;i=i+1)
            Console.WriteLine("This is sample["+i+"]:"+sample[i]);
    }
}

I'm making a sample app. Compiling the above,

This is sample[0]:0 This is sample[1]:1......

is not

This is sample[0]:0 This is sample[1]:0......

I don't know the cause.Is the loop statement strange?

c#

2022-09-30 17:55

2 Answers

for(i=0;i<10;i=i+1);

There is a semicolon, so the contents of the for statement end there.

for(i=0;i<10;i=i+1){
}

Would it be easy to understand if it is equivalent to ?


2022-09-30 17:55

 var sample=new int[100];

        for (vari=0;i<10;i++)
        {
            sample[i] = i;
        }

        for (vari=0;i<10;i++)
        {
            Console.WriteLine("This is sample["+i+"]:"+sample[i]);
        }


2022-09-30 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.