Thursday, 17 October 2013

Nested If Statement in Programming

Nested if Statement 

(nested if statement in c, nested if statement java, nested if statement example, nested if statement php, nested if statement c++ examples, nested if statement python, nested if statement in java example, nested if statement matlab)

The 'if statement' is computationally one of the most expensive statement in a program. We call it costly due to the fact that the process has to proceed through many cycles to execute an if declaration to assess a single conclusion. So to make a program more effective, try to use the smallest number of if statements. This will make the performance of the program better.

So if we have distinct conditions in which only one will be true as glimpsed in the demonstration of student’s grades, the use of if statement is very costly. To bypass this expensiveness, an alternate of multiple if declarations can be utilized that is if/else declarations. We can compose an if statement in the body of an if declaration which is known as nested if. We can write the cipher of switch statement in the following nested if/else form.
If ( grade == ‘A’ )
cout << “Excellent” ;
else if ( grade == ‘B’ )
            cout << “Very Good” ;
else if ( grade == ‘C’ )
cout << “Good” ;
else if ( grade == ‘D’ )
cout << “Poor” ;
else if ( grade == ‘F’ )
            cout << “Fail” ;
In the code, there is single statement with each ifstatement. If there are more statements with an if statement, then don’t forget the use of braces and make sure that they match (i.e. there is a corresponding closing brace for an opening brace). Proper indentation of the blocks should also be made.

No comments:

Post a Comment