The "if" Statement

The "if" Statement 

The "if" statement is used to execute (or ignore) a set of statements after testing a condition.
      The if statement evaluates a condition . If the given condition is true, the statement (or a set of statements) following the "if statement" is executed. If the given condition is false, the statement(or a set of statement) following the " if statement" condition is ignored and the control transfers to the next statement.

The syntax of the "if statement" is

if (condition)
    statement-1;
    statement-2;


Condition:   specifies a condition or a relational expression.

When the condition is true, the statement following the "if statement" is executed. If the given condition is false, the statement following the "if statement" is ignored and the control transfers to the next statement.

In the above syntax, only statement-1 will be executed if the given condition is true otherwise the control shifts to statement-2 that comes after the statement-1.

To execute a set of statements following the "if statement", the set of statement is enclosed in curly braces, i.e. within { }. The statements in braces are also known as compound statements.

The syntax of the "if statement" for executing a set of statements is:

if (condition)
      {
           statement-1
           statement-2
           statement-3
           statement-m
      }
statement-n

In the above syntax, the set of statements (from statement-1 to statement-m) has been enclosed in curly braces. These are called compound statements.

If the condition given in the "if statement" is true, the compound statements given in the curly braces are executed. If the condition is false, the control shifts to the statement-n and the set of statements that follows the "if statement" is ignored.

The flowchart for the "if statement" is shown below.
if statement flowchart


Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.