IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From Guides   Print This     

Code Blocks

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

Access VBA:   End Xxx

Access VBA code blocks are surrounded by statement ending keywords that all use End such as End Sub, End If, and WEnd.

Syntax Example:
Sub x
End Sub
 
If x Then
End If
  
While x
WEnd
ASP Classic:   End Xxx

In .ASP html pages, you embed ASP code between <% and %>.

ASP Classic code blocks are surrounded by statement ending keywords that all use End such as End Sub, End If, and WEnd.

Syntax Example:
<%
Sub x
End Sub
 
If x Then
End If
 
While x
WEnd
%>
C#:   { }

C# uses braces {} to indicate a code block of more than one line. For one line of code, the braces are optional.

I prefer to put the opening { and the closing } on their own line only because most of the examples I see do this. As opposed to C++, Java, and JavaScript where I put the opening bracket at the end of the first line (which I actually prefer).

Syntax Example:
int DoSomething()
{
 int a = 1;
 int b = 2;
 return a + b;
}
C++:   { }

For C++, Java, JavaScript, and PHP, I will use Berkeley indent style only because I see more C++ code formatted that way.

Syntax Example:
int Dog::Bark() {
cout << "My first class method!" << endl;
return 0;
};
C++/CLI:   { }

Same as standard C++. For C++, Java, JavaScript, and PHP, I prefer to put the first { at the end of the first line of the code block as in the example above because I see more C++ formatted that way.

Syntax Example:
class Cyborg {
public: System::Void IntroduceYourself() {
MessageBox::Show("Hi");
}
};
Corel Paradox:   endXxxx

ObjectPAL code blocks are surrounded by statement ending keywords that all use End with camel caps such as endMethod, endVar, endIf, endSwitch, and endTry.

Syntax Example:
method
endMethod
 
var
endVar
 
if
endIf
 
switch
endSwitch
 
try
endTry
Delphi:   begin..end

Object Pascal requires the semi-colon after the "declaration" part.

Syntax Example:
function DoSomething : integer;
var
  a, b : integer
begin
  a := 1;
  b := 2;
  
  result := a + b;
end;
Delphi Prism:   begin..end

Same as in Delphi for Win32 but Prism also supports inline variable declaration.

Syntax Example:
function DoSomething : integer;
var
 a, b : integer;
begin
 a := 1;
 b := 2;
 var c : integer; //Prism allows inline (local) variables.
 c := a + b;
  
 result := c;
end;
Java:   { }

Curly braces are used to bracket code blocks including classes and the methods within a class.

For Java, JavaScript, PHP, and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see moreJava codeformatted that way.

Syntax Example:
public class Dog {
  public bark() {
    System.out.println("Ruff");
  }
}
JavaScript:   { }

In html pages, you embed JavaScript code between <script> and </script> (see example). Also it's tradtional to put an HTML comment around your code too so that really old browsers don't crash (probably not all that important these days).

For JavaScript, Java, PHP, and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see moreJavaScript codeformatted that way.

Syntax Example:
<script language="JavaScript" type="text/javascript">
<!--
function DisplayDialogLg(StrURL) {
}
if (x == -1) {
}
-->
</script>
Perl: 

In Perl, you create the entire HTML page within your .PL script file using print commands.

For Perl, PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in this example because I see morePeal codeformatted that way.

Syntax Example:
$x = "Yes";
 
If ($x == "Yes") {
print "Hello world";
  print "I am a Perl coder.";
}
PHP:   { }

In .PHP html pages, you embed PHP code between <?PHP and ?>.

For PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see morePHP codeformatted that way (and on PHP.Net).

PHP Alternative Syntax

Although I don't like to use it, PHP offers an alternative syntax for if, while, for, foreach, and switch. These code blocks are surrounded by statement ending keywords that all use End with camel caps such as endif, endwhile, endfor, endforeach,and endswitch.

Syntax Example:
<?PHP
$x = "Yes";
//Simple if
If ($x == "Yes")
echo "hello world";
 
//If with a block of code.
If ($x == "Yes") {
echo "Hello world";
  echo "I am a PHP coder!";
}
?>
VB Classic:   End Xxx

VB Classic code blocks are surrounded by statement ending keywords that all use End such as End Sub, End If, and WEnd.

Syntax Example:
Sub x
End Sub
 
If x Then
End If
  
While x
WEnd
VB.Net:   End Xxx

VB.Net, like VB Classic code blocks, are surrounded by statement ending keywords that all use End such as End Class, End Sub, End Function, and End If.

Syntax Example:
Public Class Dog
End Class
 
Protected Sub MySub
End Sub
  
Public Shared Function MySharedFunction() As Integer
MySharedFunction = 12345
End Function
 
If x Then
End If




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]