Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why have confusing syntax?
Message
De
29/10/2008 15:30:56
 
 
À
29/10/2008 15:08:35
Information générale
Forum:
Visual C++
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01358131
Message ID:
01358158
Vues:
15
>>I got the information below from a training/video site I subscribe to. Why would they make a language harder to understand than is needed? What is the reason for having "++" to add 1 and "+=" to add 10? Why not just i+1 or i+10? Doesn't make sense to me. And, this actually is C#, not C++, but I didn't see a C# forum. Unless they are the same?
>>
>>int i = 0;
>>// convert from a string
>>int i = int.Parse("1");
>>// convert froma string and don’t throw exceptions
>>if (int.TryParse("1", out i)) {}
>>i++; // increment by one
>>i--; // decrement by one
>>i += 10; // add 10
>>i -= 10; // subtract 10
>>i *= 10; // multiply by 10
>>i /= 10; // divide by 10
>>
>
>They are shorthand notations, and can come in handy. Take a loop:
>
>
for (int i = 0; i++; i < 10)
>{
>  Console.WriteLine("i = " + i);
>}
>That will write out the numbers from 0 to 9. If you change the centre part of the 'for' to "++i" instead, it will write out the numbers from 1 to 10.
>
>In the first case, i is incremented after it is used, while in the 2nd case, it's incremented prior to use.
>
>It's a little more messing around to do the same thing other ways.

Let me rephrase that:
for (int i = 0; i < 10;)
        {
            Console.WriteLine("i = " + i++);
        }
I don't know where my head was at when I initially wrote that reply. Sorry about that. This produces 0 - 9 while chaning it to ++i produces 1 - 10.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform