If Statement

If Statement is a powerful decision making statement and it is used to monitor the flow of execution of program.

Remark: if statement always return either true or false value. Here False means zero and true means non-zero value.

Simple if : When you want to execute only satisfied condition then this statement is very useful.

Syntax:

If (<condition>)
{
                                Body      //True block
}

If...else: When you want to execute one  satisfied condition means if condition satisfied the true code will be executed and if condition will not satisfied then false block will be executed at that time  statement is very useful.

Syntax:

If (<condition>)
{
                                Body      //True block
}
else
{
                               Body      //False block
}

 

Nesting if...else: When you want to check series of decisions are involved (condition within condition) at that time this statement is useful. It is same as if...else but it is any type of if statement or statements within if statement.  We can create n number of if statements within if statement.

Syntax:

If(<condition>)
{
                                If (<condition>)
                               {
                                                Body      //True block of inner statement
                               }
                               else
                               {
                                               Body      //False block of inner statement
                               }
}// True block of outer statement
else
{
                                If(<condition>)
                               {
                                               Body      //True block of inner statement
                               }
                               else
                               {
                                              Body      //False block of inner statement
                               }

}//False block of outer statement

else if leader: The way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs.

Remark: else if statement contain n number of else if block but at the end one else block is must required.
The conditions are evaluated from top to bottom.

If (<condition>)
{
                Body      //True block
}
else If (<condition>)
{
                Body      //True block
}
else If (<condition>)
{
                Body      //True block
}
else If (<condition>)
{
                Body      //True block
}
.
.
.
else
{
                Body      //False block
}

No comments:

Post a Comment