Go Back  Xtreme .NET Talk > .NET > Syntax Specific > Visual C# .NET > trying to understand syntax


Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2011, 02:16 PM
jimmythefloyd jimmythefloyd is offline
Newcomer

Preferred language:
C#
 
Join Date: Dec 2010
Location: Madison, WI
Posts: 4
jimmythefloyd is on a distinguished road
Default trying to understand syntax



How come I don't have to use a semicolon after the "i++" in the for loop, but if I were to write i++ after the console.writeline i would. Is it just just some kind quirky thing? or is there some deeper logic behind it.
Code:
    for (int i = 0; i < 12; i++ ) // doesn't need it, won't accept it here
            {
                Console.WriteLine(myClass.MethodA());
                i++;  // Would need semicolon here
            }
            
            Console.ReadLine();
Reply With Quote
  #2  
Old 04-25-2011, 04:06 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default Re: trying to understand syntax

Quote:
Originally Posted by jimmythefloyd View Post
Is it just just some kind quirky thing?
The short answer is yes.

This syntax is inherited from C. It does seem inconsistent. However, consider the structure of the for statement (not sure if this is 100% technically correct, but it gets the idea across):
Code:
for (statement; expression; expression) 
    statement
Normally, a semi-colon denotes the end of a statement. As you can see, the last two are expressions. In this case, we are using the semi-colon as more of a delimiter. You don't normally write code with a trailing delimiter.
Code:
int x, y, z,;
At the same time, C# does accept trailing commas in some cases. One reason for this is because it makes code generation simpler. (I use trailing commas in enums and initializers if they are declared across multiple lines.)
Code:
int[] x = {0, 1, 2, 3, };
var x = new { A = "Aye", B = "Bee", };
enum x {a, b, c, }
__________________
e
Reply With Quote
  #3  
Old 07-21-2011, 04:14 PM
cloudmonkey cloudmonkey is offline
Newcomer
 
Join Date: Jul 2011
Posts: 6
cloudmonkey is on a distinguished road
Default Re: trying to understand syntax

Quote:
Originally Posted by jimmythefloyd View Post
How come I don't have to use a semicolon after the "i++" in the for loop, but if I were to write i++ after the console.writeline i would. Is it just just some kind quirky thing? or is there some deeper logic behind it.
Code:
    for (int i = 0; i < 12; i++ ) // doesn't need it, won't accept it here
            {
                Console.WriteLine(myClass.MethodA());
                i++;  // Would need semicolon here
            }
            
            Console.ReadLine();
The semi colon has always been used as a stopper at the end of a statement. A loop needs to be told to continue on, while a statement is the end of the line.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help..I don't understand hog Random Thoughts 4 01-22-2004 06:35 PM
I don't understand Mike Bailey Windows Forms 2 10-01-2003 09:49 PM
i dont understand starcraft Windows Forms 4 08-01-2003 04:33 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe