The IF Statement

The IF Statement

Published by: Nuru

Published date: 21 Jun 2021

The IF Statement Photo

The IF Statement

'The IF statement' is a two-way decision statement and is used together with an expression. That means the test condition. We use it to express conditional expressions. The ‘if statements’ allow us to control if a program enters a section of code or not based on whether a given condition is true or false.  One of the important functions of the 'if statements' is that it allows the program to select an action based upon the user's input.

There are 3 arguments to the IF statements function. They are:

  • TEST statements, such as value in a cell
  • Specify what should happen if the test result is TRUE.
  • And, specify what should happen if the test result is FALSE.

The general syntax of the IF statements is,

If (test expression)

{

Statements-block;

}

Statements-x;

 

For only one statement we don’t need braces.

If (condition)

statements;

 

For more than one statements we need braces;

If (condition)

{

statements1;

statements2;

……………………..

statements N;

}

The if statements evaluate the test expression first and then if the value of the expression is true, it executes the statements within its block. Otherwise, it skips the statements within its block. And, continues from the statements outside the if block. Therefore, there is no execution of statements inside the body of ‘if’.

Expression is true

Int test = 5;

If (test < 10)

{

……………

}

If ……….

 

Expression is false

If (test > 10)

{

……………

}

If ……….

The flowchart of the IF statement is:

The IF Statement

Now, for instance, let us see a program,

The IF Statement

The IF Statement