This type of decision control structure includes a statement or block of statements on both paths.

If the Boolean expression evaluates to true, the statement or block of statements 1 is executed; otherwise, the statement or block of statements 2 is executed.

The general form of the statement is

PHP, Java, C++, C#

if (Boolean_Expression) {
  /* A statement or block of statements 1 */ ;
}
else {
  /* A statement or block of statements 2 */ ;
}

Notice: In PHP, Java, C++, and C#, the Boolean expression must be enclosed in parentheses.

Single statements can be written without being enclosed inside braces { }, and the if-else statement becomes

if (Boolean_Expression)
  /* One Single Statement 1 */ ;
else
  /* One Single Statement 2 */ ;

Visual Basic

If Boolean_Expression Then
  'A statement or block of statements 1
Else
  'A statement or block of statements 2
End If

Python

if Boolean_Expression:
    #A statement or block of statements 1
else:
    #A statement or block of statements 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!