Code is generally organized into statements and comments.
A statement is basically an instruction that expresses one kind of action, declaration, or condition. We use statements to tell the compiler what we want our code to do. We use comments to note what our script does.
A new statement usually begins on a new line and ends with a language-specific line-termination character. A line break ( or carriage return ) separates lines in VB, while C# and JScript use the semicolon ( ; ) to explicitly end each statement.
A statement may be as simple as assigning a value, or invoking a method. For example, the below statements assign the current system date and time to a variable named today, then writes the value of today to the page.
DateTime today = DateTime.Now;
Response.Write ( today );
Dim today as Date = Now
Response.Write ( today )
var today : Date = new Date ( );
Response.Write ( today ); |
|
C# |
VB |
JScript |
You can also include more than one statement on a line, separated by line terminators. In VB, you use a colon ( : ) between the statements.
A = A + 1; B = B + A; C = C + B;
A = A + 1 : B = B + A : C = C + B
A = A + 1; B = B + A; C = C + B; |
|
C# |
VB |
JScript |
You can also break a long single logical line and continue onto one ( or more ) physical lines. C# and JScript normally ignore line breaks in statements. VB uses a single underline ( _ ) to denote a line-continuation character.
OleDbConnection myConnection = new OleDbConnection
( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
Server.MapPath ( "~/app_data/pubs.mdb" ) );
Dim myConnection as new OleDbConnection _
( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & _
Server.MapPath ( "~/app_data/pubs.mdb" ) );
OleDbConnection myConnection = new OleDbConnection
( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
Server.MapPath ( "~/app_data/pubs.mdb" ) ); |
|
C# |
VB |
JScript |
While you can break at any part in the line, it is good programming practice to locate a break at reasonable points in your code. The plus ( + ) and the ampersand ( & ) in the above code, for instance, are used for string concatenation and are suitable places to break code.
IMPORTANT: Line continuation can introduce errors if not done properly. Use cautiously and only when necessary.
Statements are normally executed one after the other, in the order in which they are written. Exceptions to this rule are statements that are used to control program flow, such as conditional, loop, and jump statements, which are covered in later sections.