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 GuidesStatements  Print This     

If Statement (Cross Ref > Statements)

If Statement

Access VBA:   If..ElseIf..Else..End If

The End If is optional if you put your code on a single line.

Syntax Example:
//Single line example.
If X = True Then MsgBox "hello" 
  
//Complete example. 
If X = True Then
'>>>do something.
ElseIf Y = "ABC" Then
'>>>do something.
Else
'>>>do something.
End If
ASP Classic:   If..ElseIf..Else..End If

The End If is optional if you put your code on a single line.

Syntax Example:
//Single line example.
If X = True Then Response.Write "hello" 
  
//Complete example.
If X = True Then
  '>>>do something.
ElseIf Y = "ABC" Then
  '>>>do something.
Else
  '>>>do something.
End If
C#:   if..else if..else

Use () around evaluation with no "then".

Syntax Example:
Boolean x = true;


if (x)
{
MessageBox.Show("hello");
}
else if (x==false)
{
MessageBox.Show("goodbye");
}
else
{
  MessageBox.Show("what?");
}
C++:   if..else if..else

Same as standard C.

Syntax Example:
//C++Builder example using the VCL ShowMessage.
int x;
  
x = 8;
  
if (x == 10) {
ShowMessage("x is 10.");
} else if (x < 10) {
ShowMessage("x is less than 10.");
} else {
ShowMessage("x must be greater than 10.");
}
C++/CLI:   if..else if..else

Same as standard C.

Syntax Example:
int x;
  
x = 8;
  
if (x == 10) {
MessageBox::Show("x is 10");
} else if (x < 10) {
MessageBox::Show("x is less than 10");
} else {
MessageBox::Show("x must be greater than 10");
}
Corel Paradox:   If..Else..EndIf, or switch

ObjectPAL supports a simple If...Else...EndIf statement.

Notice ObjectPAL does not support an ElseIf feature as part of an if statement. Instead use a switch statement

Syntax Example:  
'Does ObjectPAL evaluate the math correctly? No!
If (.1 + .1 + .1) = .3 Then
  msgInfo("", "Correct")
Else
  msgInfo("", "Not correct")
EndIf
 
'Switch statement example.
switch
case x = "Nate": MsgInfo("", "Hi Nate")
case x = "Felicia": MsgInfo("", "Hi Felly")
  otherwise: MsgInfo("", "Who are you?")
endSwitch
Delphi:   If..Else If..Else

Notice in the more complete example that the semicolon for the begin..end block after end is not included. That tells the compiler something else is coming (the statement is not finished). Also note the semicolon is missing right before the final "else" statement.

Note: The following example uses floating point literals. In Delphi, to specify a fractional floating point literal between 1 and -1, you preceed the decimal with a 0; otherwise, you will get a compiler error (i.e. .1 + .1 does not work).

Syntax Example:
if (0.1 + 0.1 + 0.1) = 0.3 then
  ShowMessage('Correct')
else
  ShowMessage('Not correct');
 
//Complete example:
if x = true then
begin
  ShowMessage('x is true');
end
Else If y = 'Mike' Then 
  ShowMessage('hello mike')
Else 
  ShowMessage('last option');
Delphi Prism:   if..else if..else

Notice in the more complete example that the semicolon for the begin..end block after end is not included. That tells the compiler something else is coming (the statement is not finished). Also note the semicolon is missing right before the final "else" statement.

Syntax Example:
//Complete example:
if x = true then begin
  ShowMessage('x is true');
end
Else If y = 'Mike' Then 
  ShowMessage('hello mike')
Else 
  ShowMessage('last option');
Java:   if..else if..else

Syntax template:

if (expression) {
  expression1_true_code;
} else if (expression2) {
  expression2_true_code;
} else {
  otherwise_code;
}
Syntax Example:
if ((.1 + .1 + .1) == 0.3) {
  System.out.println("Correct");
} else {
  System.out.println("Not correct");
}
JavaScript:   if..else if..else

Same as C/C++ but, as usual, the semicolons are optional.

Syntax Example:
var x = 8;
  
if (x == 10) {
 document.write("x is 10.");
} else if (x < 10) {
 document.write("x is less than 10."); 
} else {
 document.write("x must be greater than 10.");
}
Perl:   if..elsif..else

Notice Perl is different from most other languages in it's spelling of elsif (else is not spelled correctly).

Syntax Example:
$x = 8;
  
if ($x == 10) {
 print "X is 10.";
} elsif ($x < 10) {
 print "X is less than 10.";
} else {
 print "X must be greater than 10.";
}
PHP:   if..elseif..else

The PHP if statement consists of using if, elseif, and else.

Syntax Example:
$x = 8;
  
if ($x == 10) {
 echo "x is 10."; 
} elseif ($x < 10) {
 echo "x is less than 10.";
} else {
 echo "x must be greater than 10."; 
};
VB Classic:   If..ElseIf..Else..End If

The End If is optional if you put your code on a single line.

Syntax Example:
//Single line example.
If X = True Then MsgBox "hello" 
 
//Complete example.
If X = True Then
MsgBox "hello"
ElseIf Y = "ABC" Then
MsgBox "goodbye"
Else
MsgBox "what?"
End If
VB.Net:   If..ElseIf..Else..End If

Same as VB classic.

Syntax Example:  
If x Then
MessageBox.Show("hello")
ElseIf Not x Then
MessageBox.Show("goodbye")
Else
MessageBox.Show("what?")
End If




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


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