else ........ if Ladder

else ........ if Ladder


                else…..if  ladder statement is used to take multi way decision. This statement is formed by joining if ……..else statements in which each else contains another if ….else. The general form is

if(test condition-1)
{
Statement block-1;
}
else if(test condition-2)
{
Statement block-2;
}
-------------------------
-------------------------
else if (test condition-n)
{
Statement block-n;

}
else
default statement;
next statement;
  
                Computer executes this statement from top to bottom. if  a true test condition is found, the statement block associated with it is executed. Then the control is transferred to the next statement. When all the test conditions are false, then the final else containing the default statement will be executed.

Rules

1.       The brackets around the test condition are must.
2.       The test conditions must be a relational or logical expression.
3.       Statement blocks are called body of the if………else statement. It can contain one or more statements.
4.       The opening statement and closing brackets {} are must if the statement blocks contain more than one statement. Else optional
5.       No statements other than the statements in the statement blocks are allowed between if ….. else.
6.       Default statement is a must. If not present no action will take place if all conditions are false.

Flow diagram


No comments:

Post a Comment

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...