|
|
|
|
Loops!It is never ending loop! Have you ever said that? Let's see how to break a loop. Loop is required whenever you need to perform a same task a number of times. For instance, there are 10 employees in a department and you want to calculate the total salary of all the employees. What will you do? Use a LOOP. Say in the last code we wrote for the conditional statement has to allow the user to try 3 times if the num1 (also for num2) is not a positive number. New code will look like the following:
Initially 'try' is 0, so while loop is entered. We assume we will allow user to try 3 times. First try++ is incremented 'try' by 1. num1 is input by the user.
If 'num1' is greateer than 0 ie a positive number is entered then we 'break' the loop. The program continues executing the next command in the sequence.
if the 'true_condition' evaluates to true then only the loop is entered.
Enters the loop at least once and checks for 'true_condition'
Variables can be declared in 'variable_declaration' and if the 'condition' is true the for loop is entered and executes from statements_1 onwards. Once the end of the loop is reached 'statements_0' is executed, which normally will be an incrementing counter and then the 'condition' is checked. It is repeated till the 'condition' fails. You can nest all the above loops within another loop. Switch StatementIt is a kind of 'if-elseif-elseif-else' statement. It is used when action has to be taken for more than two choices.
'break' is important without that the program continues executing the next immediate line. 'case else' says if everyelse is not satisfied this code is executed. 'case else' should be the last in the 'case' series and because it is at the end there is no need for a 'break'. A switch can be written inside a loop/if-else/switch construct. |
|
Send mail to arungomes@eircom.net with questions or comments about this web site.
|