This is the simplest decision control structure. It includes a statement or block of statements on the “true” path only.

If the Boolean expression evaluates to true, the statement, or block of statements, of the structure is executed; otherwise, the statements are skipped.

The general form of the statement is

PHP

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

Notice: In PHP, a Boolean expression must be enclosed in parentheses.

When only one single statement is used in the if statement, you can omit the braces { }. Thus, the if statement becomes

if (Boolean_Expression)
  /* One Single Statement */ ;

or you can even write the statement on one single line, like this:

if (Boolean_Expression) /* One Single Statement */;

The braces are required only when more than one statement is used in the if statement. Without the braces, PHP cannot tell whether a statement is part of the if statement or part of the statements that follow the if statement. Many programmers prefer to always use braces even if the if statement includes only one single statement. In both of the following examples the echo $y statement is not part of the if statement.

if ($x == $y)
  $x++;
  echo $y;

=

if ($x == $y) {
  $x++;
}
echo $y;

Notice: A very common mistake that novice programmers make when writing PHP programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing if ($x = $y) when they actually want to say if ($x == $y).

Java

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

Notice: In Java, a Boolean expression must be enclosed in parentheses.

When only one single statement is used in the if statement, you can omit the braces { }. Thus, the if statement becomes

if (Boolean_Expression)
  /* One Single Statement */ ;

or you can even write the statement on one single line, like this:

if (Boolean_Expression) /* One Single Statement */ ;

The braces are required only when more than one statement is used in the if statement. Without the braces, Java cannot tell whether a statement is part of the if statement or part of the statements that follow the if statement. Many programmers prefer to always use braces even if the if statement includes only one single statement. In both of the following examples the System.out.println(y) statement is not part of the if statement.

if (x == y)
  x++;
  System.out.println(y);

=

if (x == y) {
  x++;
}
System.out.println(y);

Notice: A very common mistake that novice programmers make when writing Java programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing if (x = y) when they actually want to say if (x == y).

C++

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

Notice: In C++, a Boolean expression must be enclosed in parentheses.

When only one single statement is used in the if statement, you can omit the braces { }. Thus, the if statement becomes

if (Boolean_Expression)
  /* One Single Statement */ ;

or you can even write the statement on one single line, like this:

if (Boolean_Expression) /* One Single Statement */ ;

The braces are required only when more than one statement is used in the if statement. Without the braces, C++ cannot tell whether a statement is part of the if statement or part of the statements that follow the if statement. Many programmers prefer to always use braces even if the if statement includes only one single statement. In both of the following examples the cout << y statement is not part of the if statement.

if (x == y)
  x++;
  cout << y;

=

if (x == y) {
  x++;
}
cout << y;

Notice: A very common mistake that novice programmers make when writing C++ programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing if (x = y) when they actually want to say if (x == y).

C#

if (Boolean_Expression) {
  /* A statement or block of statements */
}

Notice: In C#, a Boolean expression must be enclosed in parentheses.

When only one single statement is used in the if statement, you can omit the braces { }. Thus, the if statement becomes

if (Boolean_Expression)
  /* One Single Statement */ ;

or you can even write the statement on one single line, like this:

if (Boolean_Expression) /* One Single Statement */ ;

The braces are required only when more than one statement is used in the if statement. Without the braces, C# cannot tell whether a statement is part of the if statement or part of the statements that follow the if statement. Many programmers prefer to always use braces even if the if statement includes only one single statement. In both of the following examples the Console.Write(y) statement is not part of the if statement.

if (x == y)
  x++;
  Console.Write(y);

=

if (x == y) {
  x++;
}
Console.Write(y);

Notice: A very common mistake that novice programmers make when writing C# programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing if (x = y) when they actually want to say if (x == y).

Visual Basic

If Boolean_Expression Then
  'A statement or block of statements
End If

When only one single statement is used in the If statement, you can write it on one single line, like this:

if Boolean_Expression Then 'One Single Statement

Python

if Boolean_Expression:
    #A statement or block of statements

Notice: Python was one of the first programming languages to enforce indentation. Python specifies that several statements are part of a group by indenting them. The indented group is called a “block of statements” or “code block.” Indentation is considered good practice in other languages, but in Python indentation is mandatory. Code that is part of a block must be indented. For example, all statements that appear inside an if statement must be indented to the right by the same number of spaces; otherwise they are not considered part of the if statement and you probably get an error message. There are two simple rules to remember about code blocks’ syntax:

  • The statement on the first line of a code block always ends with a colon ( : ) character.
  • The code underneath the first line must be indented.

Notice: Python’s official website recommends the use of 4 spaces per indentation level. If you need more information you can visit

https://www.python.org/dev/peps/pep-0008.

Notice: In computer languages other than Python, such as C, C++, C#, Java, or Visual Basic, indentation is not obligatory but it is quite necessary in order to make code easier to read. It also helps programmers to more easily study and understand code written by others.

When only one single statement is used in the if statement, you can write it on one single line, like this:

if Boolean_Expression: #One Single Statement

Notice: A very common mistake that novice programmers make when writing Python programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing if x = y when they actually want to say if x == y.