VisualBasic
Does not provide a generic block construct,
instead, each type of block conditional requires
a specific terminating Statement.
For instance, For ... Next and if ... End if
Pascal
Begin
Statements
End;
C++ and Java
Use curly braces { .. }
It is a good idea to always include curly braces
if more than one line is used in an if statement
(ie, when the if is on one line and the command is on the next).
It is a common programming error to add a command and
to forget the curly braces. If they are always there
you should make fewer errors.
VisualBasic
if a = b then ' Any type of variable
if str_a = "HI" then
if isNull(a) then ' if a = Null always fails
Pascal
if (X in [1..80]) and (Y in [1..25]) then
if fsHidden in File1.FileType then
C++ and Java
if (a == b) then // Any non-string variables
if strcmp(str_a, "HI") then // Strings are arrays of char
| Basic | C++ & Java | Pascal | |
|---|---|---|---|
| Equal | a = b | a == b | a = b |
| Not Equal | a <> b | a != b | a <> b |
| Inequality | < <= >= > | < <= >= > | < <= >= > |
| Boolean | and or not xor | && || ! and or not | and or not xor |
| Bitwise Logical | and or not xor | & | ~ ^ | and or not xor |
| Range Test | n/a | n/a | X in [1..80] |
In C++, and, or, and not are defined as macros which are equivalent to && || and ! respectively.
VisualBasic
Single if a>5 then a=5 : b=7 else b=2
a = IIf(a>5, 5, a) ' IIf(expr, truepart, falsepart)
Block if a>5 then
a=5
b=7
end if
If condition Then single statement variable = IIf(expr, truepart, falsepart) If condition Then block of statements End If If i=5 Then 'do this ElseIf i = 2 Then 'do this Else 'statements End If |
VisualBasic
Select Case rtfData.SelColor
Case vbBlack
cmbFontColor.ListIndex = 0
Case vbBlue
cmbFontColor.ListIndex = 1
Case vbRed
cmbFontColor.ListIndex = 2
Case Else
cmbFontColor.ListIndex = 3
End Select
Select Case SuggestedSize
Case Is > 15
Size = 15
Case Is < 3
Size = 3
Case Else
Size = SuggestedSize
End Select
Pascal and Delphi
The 1st example is from the Delphi SelAttributes example,
the other 2 are based on examples from the case syntax help
Searching the help for case may provide no data
Must search delphi.hlp to get syntax
case Ord(Alignment) of
0: LeftAlign.Down := True;
1: RightAlign.Down := True;
2: CenterAlign.Down := True;
end;
case MySelector of
1, 2: Writeln('One or Two');
3..5: Writeln('Three, Four, or Five');
else Writeln('More');
end;
case Ch of
'A'..'Z', 'a'..'z': WriteLn('Letter');
'0'..'9': WriteLn('Digit');
'+', '-', '*', '/': WriteLn('Operator');
else
WriteLn('Special character');
end;
Only ordinal constants and ranges are allowed in Delphi 2.0.
Greater than or less than conditions are not allowed
Strings are not allowed, but single characters are
C++ and Java
// Switch matches the expression results with a case constant
// Warning: If break; is omitted, the next line is executed!
switch ( someExpression )
{
case 1:
doCase1( );
break;
case 2:
doCase2( );
break;
case 3:
doCase3A( );
doCase3B( ); // Fall thru to default
// (always comment an intentional fall thru
// so that the reader will know that it is
// not an accident)
default:
doDefault( );
} // end of switch (repeat expression here)
VisualBasic
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next [counter]
for i = 0 to 10
...
next i
Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]
Loop
Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]
While Flag = 1
[statements] ' There is no Exit While statement
Wend
Pascal
For i := 1 to 10 do // For i := 10 downto 1 do
Begin // There is no way to vary the step size
break;
End;
while not Eof(InputFile) do
begin
Readln(InputFile, Line);
Process(Line);
end;
Repeat
Any number of statements
Until False;
C++ and Java
for( [init-expr]; [cond-expr]; [loop-expr] )
statement
for ( int i = 0; i < 10; i++ )
{
code goes here
}
// Do loop, always executes at least once
do
{
// Place code here
} while ( i <= 10 ); // The parentheses are required
// While loop never executes if condition is not initially true
while ( i < 10 ) // The parentheses are required
{
// Place code here
}
break;
transfers control out of the innermost enclosing
while, do, for, or switch statement.
Java supports an optional label to detrmine the target loop.
continue;
passes control to the next iteration of the innermost enclosing
while, do, or for statement.
Java supports an optional label to detrmine the target loop.