10/02/2015

If statement is a branch that uses a control expression/statement which in turn permits us to select and execute specific block/blocks of code while skipping other block/blocks of code.

The if-construct has the following format:

if  control/logical_expression_1
    {
        ........... execute this block 1
        ........... execute this block 1
      }

elseif  control/logical_expression_2
      {
        ........... execute this block 2
        ........... execute this block 2
      }

else 
{
        ........... execute this block 3
        ........... execute this block 3
      }
 end

Some important rules/points about if-construct:

1-if statement is must to use and can occur only once i.e at the beginning.It also tells the position counter or controller that the if-construct has been started.
2-else statement may or may not occur according to our demand but it can occur only once i.e before  the end statement, if the if statements are not nested.
3-elseif statement can occur as many times as we require it.It will always occur between if and end statements.

How these statements work?

There is a simple reason behind working of these if, elseif and else statements.That will be explained in the following paragraph.
These if and elseif statements require a logical 1(in machine language it's mean true) for their code block to be executed and a logical 0(in machine language it's mean fasle) for their code block not to be executed.
These logical 1's and 0's are provided by control/logical expressions,these logical/control expressions are combinations of relational and logic operators which forms a condition, if this applied condition becoms true result will be logical 1 and if this a applied condition becomes false result will be logical 0, which will control the execution and non execution of code block related to if and elseif statements respectively.This is how these statements work basically.

Note: else statement don't require a control/logical expression.

If none of the code block is executed above else statement, then code block related to else statement is executed by program counter, if this statement and code block exists.

Where the use of end statement is just to mark the boundary or finish point of the if-construct.To tell the position counter or controller that after the execution of end statement the controller will be out of the if construct.

0 comments:

Post a Comment

Blog Archive

Popular Posts