A
A
Anton_repr2019-07-30 17:49:39
Programming
Anton_repr, 2019-07-30 17:49:39

Why does the loop output 0 at the very beginning?

int i = 0;
for(; i<5; i++){
Console.Write(i);
}

Output : 01234
Why is zero output? I increase the variable by 1 (i++)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kot2566, 2019-07-30
@Anton_repr

In this case, there is no difference. Since this operation (i++ or ++i) will be executed at the end of the iteration.
And in the example below, there is already a difference:

int i = 0;
for (; i < 5;)
{		
  Console.Write(++i);  // 12345	
  
  // i = i+1;
  // Console.Write(i);
}
    
Console.WriteLine();
    
i=0;	
  
for (; i < 5;)
{			
  Console.Write(i++); // 01234

  // Console.Write(i);
  // i = i+1;	
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question