If we want computer do something based on different conditions, we can use the "IF" statement.
If the "If condition Then" statement finds the result is True then if will continue the next statement. Otherwise, it will find the next "ElseIf condition Then" statement to test another condition. If no condition is matched, it will find the "Else" statement to go. Or the "End If" statement, if there is no "Else" found.
Other languages use the "{...}" to define a black of statements. If the condition is True, it will perform all statements within the block.
BASIC:
' *******************************************
' Condition statement (If..ElseIf..Else..End If)
' *******************************************
Sub IfStatement()
Dim dPara1 as Double
Dim dPara2 as Double
dPara1 = 1.0
dPara2 = 2.0
' Condition statement (If..ElseIf..Else..End If)
If dPara1 > dPara2 Then
' Is Greater Than
Debug.Print " ";"dPara1 > dPara2";
Debug.Print
ElseIf dPara1 = dPara2 Then
' Equals to
Debug.Print " ";"dPara1 ==dPara2";
Debug.Print
ElseIf dPara1 < dPara2 Then
' Is Less Than
Debug.Print " ";"dPara1 < dPara2";
Debug.Print
End If
If dPara1 <> dPara2 Then
' not equal to
Debug.Print " ";"dPara1 <> dPara2";
Debug.Print
End If
' And Logic
If dPara1 > dPara2 AND dPara1 = dPara2 Then
Debug.Print " ";"dPara1 >=dPara2";
Debug.Print
' Or Logic
ElseIf dPara1 > dPara2 OR dPara1 < dPara2 Then
Debug.Print " ";"dPara1 <> dPara2";
Debug.Print
End If
End Sub
C++:
/* ******************************************* */
/* Condition statement (If..ElseIf..Else..End If) */
/* ******************************************* */
void IfStatement() {
double dPara1;
double dPara2;
dPara1 = 1.0;
dPara2 = 2.0;
/* Condition statement (If..ElseIf..Else..End If) */
if (dPara1 > dPara2) {
/* Is Greater Than */
printf(" %s" , "dPara1 > dPara2");
printf("\n" );
} else if (dPara1 ==dPara2) {
/* Equals to */
printf(" %s" , "dPara1 ==dPara2");
printf("\n" );
} else if (dPara1 < dPara2) {
/* Is Less Than */
printf(" %s" , "dPara1 < dPara2");
printf("\n" );
}
if (dPara1 != dPara2) {
/* not equal to */
printf(" %s" , "dPara1 <> dPara2");
printf("\n" );
}
/* And Logic */
if (dPara1 > dPara2 && dPara1 ==dPara2) {
printf(" %s" , "dPara1 >=dPara2");
printf("\n" );
/* Or Logic */
} else if (dPara1 > dPara2 || dPara1 < dPara2) {
printf(" %s" , "dPara1 <> dPara2");
printf("\n" );
}
}
JAVA:
// *******************************************
// Condition statement (If..ElseIf..Else..End If)
// *******************************************
public void IfStatement() {
double dPara1;
double dPara2;
dPara1 = 1.0;
dPara2 = 2.0;
// Condition statement (If..ElseIf..Else..End If)
if (dPara1 > dPara2) {
// Is Greater Than
System.out.print(" "+"dPara1 > dPara2" );
System.out.println("");
} else if (dPara1 ==dPara2) {
// Equals to
System.out.print(" "+"dPara1 ==dPara2" );
System.out.println("");
} else if (dPara1 < dPara2) {
// Is Less Than
System.out.print(" "+"dPara1 < dPara2" );
System.out.println("");
}
if (dPara1 != dPara2) {
// not equal to
System.out.print(" "+"dPara1 <> dPara2" );
System.out.println("");
}
// And Logic
if (dPara1 > dPara2 && dPara1 ==dPara2) {
System.out.print(" "+"dPara1 >=dPara2" );
System.out.println("");
// Or Logic
} else if (dPara1 > dPara2 || dPara1 < dPara2) {
System.out.print(" "+"dPara1 <> dPara2" );
System.out.println("");
}
}
C#:
/* ******************************************* */
/* Condition statement (If..ElseIf..Else..End If) */
/* ******************************************* */
public void IfStatement() {
double dPara1;
double dPara2;
dPara1 = 1.0;
dPara2 = 2.0;
/* Condition statement (If..ElseIf..Else..End If) */
if (dPara1 > dPara2) {
/* Is Greater Than */
Console.Write(" "+"dPara1 > dPara2");
Console.WriteLine("");
} else if (dPara1 ==dPara2) {
/* Equals to */
Console.Write(" "+"dPara1 ==dPara2");
Console.WriteLine("");
} else if (dPara1 < dPara2) {
/* Is Less Than */
Console.Write(" "+"dPara1 < dPara2");
Console.WriteLine("");
}
if (dPara1 != dPara2) {
/* not equal to */
Console.Write(" "+"dPara1 <> dPara2");
Console.WriteLine("");
}
/* And Logic */
if (dPara1 > dPara2 && dPara1 ==dPara2) {
Console.Write(" "+"dPara1 >=dPara2");
Console.WriteLine("");
/* Or Logic */
} else if (dPara1 > dPara2 || dPara1 < dPara2) {
Console.Write(" "+"dPara1 <> dPara2");
Console.WriteLine("");
}
}
PHP:
/* ******************************************* */
/* Condition statement (If..ElseIf..Else..End If) */
/* ******************************************* */
function IfStatement() {
$dPara1 = 1.0;
$dPara2 = 2.0;
/* Condition statement (If..ElseIf..Else..End If) */
if ($dPara1 > $dPara2) {
/* Is Greater Than */
echo " "."dPara1 > dPara2";
echo "";
} elseif ($dPara1 ==$dPara2) {
/* Equals to */
echo " "."dPara1 ==dPara2";
echo "";
} elseif ($dPara1 < $dPara2) {
/* Is Less Than */
echo " "."dPara1 < dPara2";
echo "";
}
if ($dPara1 != $dPara2) {
/* not equal to */
echo " "."dPara1 <> dPara2";
echo "";
}
/* And Logic */
if ($dPara1 > $dPara2 && $dPara1 ==$dPara2) {
echo " "."dPara1 >=dPara2";
echo "";
/* Or Logic */
} elseif ($dPara1 > $dPara2 || $dPara1 < $dPara2) {
echo " "."dPara1 <> dPara2";
echo "";
}
}