cs201 Nested if: An if statement within an if statement is called - TopicsExpress



          

cs201 Nested if: An if statement within an if statement is called nested if statements. In nested structure the control enters into the inner if only when the outer condition is true. Only one block of statements are executed and the remaining blocks are skipped automatically. The user can use as many if statements inside another if statement as required. The increase in the level of nesting increases the complexity of nested if statement. Syntax: If (condition) { If (condition) { Statement(s); } Else { Statement(s); } } Else { Statement(s); } Switch statement: The switch statement is another conditional structure. It is a good alternative of nested if-else. It can be used easily when there are many choices available and only one should be executed. Nested if becomes very difficult in such situations. Syntax: Switch (expression) { Case1: Statements; Break; Case 2: Statements; Break; Case 3: Statements; Break; Default: Statements; } Working of “switch” statement: Switch statement compares the result of a single expression with multiple cases. Expression can be any valid expression that results in integers or character value. The expression is evaluated at the top of the switch statement and its result is compared with different cases. Each case label represents one choice. If the result matches with any case, the corresponding block of statements is executed. Any number of cases can be used in one switch statement. The default label appears at the end of all case labels, It is executed only when the result of expression does not match with any case label. Its use is optional. The position of default label is not fixed. It may be placed before the first case statement or after the last one. The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of the expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If the break is not used, all case blocks that come after the matching case, will also be executed. [Areeba]
Posted on: Thu, 29 Aug 2013 07:56:23 +0000

Trending Topics



Recently Viewed Topics




© 2015