Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow
if i change the variable of the for loop the compiler did not geive me any error , but i am not sure if this is legal
C#
int i;
for(i=0;i<10;i++)
{
MessageBox.Show("i="+i.ToString());
if( i==3 ) i++;
}
Posted
Updated 25-Oct-14 22:13pm
v4
Comments
Richard MacCutchan 26-Oct-14 4:31am    
Yes it is perfectly legal, but it may have unintended consequences.
Engineer khalid 26-Oct-14 4:39am    
long time ago ,i was a c programmer it was illegal ,but if i am mistaken then this was illegal in fortran languge. thanks god ,i am still ridding a horse
Richard MacCutchan 26-Oct-14 4:53am    
Legal in C also. It's a long time since I wrote any Fortran, but I don't know why it would be illegal there, or in any other language.

1 solution

Yes, it's legal in C# - IIRC it has been legal in all C-like languages, including C.
The final clause of the for statement can be blank if you need it to:
C#
for(i=0;i<10;)
    {
    MessageBox.Show("i="+i.ToString());
    i++;
    if( i==3 ) i++;
    }
 
Share this answer
 
Comments
BillWoodruff 26-Oct-14 5:28am    
Sometimes there is too much freedom :)
OriginalGriff 26-Oct-14 5:53am    
Philosophy on a Sunday morning? Not sure I can cope with that...:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900