Loops

Home Up Contents Job Links Download

Search this site(enter key word):  

Helping the mankind

Procedures

 

 

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:

int try = 0 ;
 
while ( try < 3 ) {
    try++ ;
    read num1 ;
    if ( num1 > 0 )
        break ;
}

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.    

NOTE:  {} is used for code block. Some languages use 'BEGIN' and 'END' and oters do not use any of them at all. Also note that how the code is indented properly to make it more readable.

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. 

while ( true_condition ) {
    statements_1  ;
    statements_2  ;
}

if the 'true_condition' evaluates to true then only the loop is entered.

loop {
    statements_1 ;
    statements_2 ;
} until ( true_condition )

Enters the loop at least once and checks for 'true_condition'

for( variable_declaration ; condition ; statements_0  ) {
    statements_1 ;
    statements_2 ;
}

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 Statement

It is a kind of 'if-elseif-elseif-else' statement.  It is used when action has to be taken for more than two choices.

switch ( variable ) {
    case value_1 :
        statements ;
        break ;
    case value_2 :
        statements ;
        break ;
    case value_n :
        statements ;
        break ;
    case else :
        statements ;
}

'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.

 

Home ] Up ] Procedures ]

Sign My Guestbook Guestbook by GuestWorld Sign My Guestbook

Join EITC's mailing list!
Enter your email address below,
then click the 'Join List' button:
Powered by ListBot

Send mail to arungomes@eircom.net with questions or comments about this web site.
Copyright © 1999 Empire Cyber IT Consultancy
Last modified: January 22, 2000