Loop

LOOP's :-

                   In programming sometime we require to execute a set of instruction more from one time . For this purpose " C " provide loops . In loop , we can execute set of instruction with  or  without any changes .  It also prevent the repetition of the same instruction & make compact code .

 C provide three type of Loops :-


while Loop :-

                 while loop is generally use in problem where it is not known in advance that how many time the loop execute.while loop is a Pre-test loop ie. Firstly the condition is check , if it give Non-zero result so-only the statement block is executed . Otherwise it leave the statement block .

 Syntax :-  
while(condition)
{

     statement block

 } 

 Working :-  The while loop follow the following steps .

 First it check the condition .
If the condition give Non-Zero result so the statement-block execute & repeat the first step .
If the condition give Zero as result so it leave the statement-block inside the loop & execute rest of the instructions .

 Þ In while loop we can use any type of valid expression & the statement-block execute only if the expresssion return a Non-Zero result .
while(expression)
{
   statement block
}

                                     EXPRESSION    

              Non-Zero = TRUE

                   Zero = FALSE       

1)

while( 1 )

{

}

1)

while( 0 )

{

}

2)

int i=5 ;

while(i)

{

}

2)

int i=0 ;

while(i)

{

}

3)

int i ;

while(i = 8)

{

}

3)

int i ;

while(i = 0)

{

}

4)

int i ;

while( i = 1 + 4)

{

}

4)

int i ;

while( i = 5 - 5)

{

}

5)

int i=8;

float j=8.0;

while(i == j)

{

}

5)

int i=8 ;

float j=8.5 ;

while(float (i == j))

{

}

6)

int i=8 ;

while( i >=5 && i <= 10 )

{

}

6)

int i=8 ;

while( i >= 1 && i <=5 )

{

}


for Loop :-
                  This is most popular Pre-test loop . It is popular because it provide three benifits to the user . It allow us To 
®Initialise the Counter variable .
®Test the condition 
®Increment/Decrement the Counter variable .

             For this reason , " for " loop is more popular among user .

Syntax :-

for(initialise ; condition ; Increment )
{
    statement-block
}

for              ---->  Keyword (Reserve word)
initialise     ---->   here we give Initial Value to the Counter variable
condition   ---->   here we can use Condition or any type valid expression
Increment ---->   here we can Increment or Decrement the Counter variable

Working :- The for loop work as .

Firstly it initialise the counter variable .
The loop check the Condition .
If the condition is true ie. give Non-Zero result , it execute the statement-block , Increment the Counter variable , & repeat the second step .
If the condition is false ie. give Zero as result , it leave the statement-block inside the loop & execute the rest of the instruction .

Þ In for loop we can use it in any way , for leaving the space Blank . BUT the syntax must be same .

for( ; ; )
{
   statement-block
}

Þ In for loop we can also use any type of valid expression & the statement-block execute only if the expresssion return a Non-Zero result .

for(initialise ; expression ; increment)
{
   statement block
}

                                      EXPRESSION    

             Non-Zero = TRUE

                   Zero = FALSE       

1)

for( ; 1 ; )

{

}

1)

for( ; 0 ; )

{

}

2)

int i=5 ;

for( ; i ; )

{

}

2)

int i=0 ;

for( ; i ; )

{

}

3)

int i ;

for( ; i = 8 ; )

{

}

3)

int i ;

for( ; i = 0 ;)

{

}

4)

int i ;

for( ; i = 1 + 4 ; )

{

}

4)

int i ;

for( ; i = 5 - 5 ; )

{

}

5)

int i=8;

float j=8.0;

for( ; i == j ;)

{

}

5)

int i=8 ;

float j=8.5 ;

for( ; float (i == j) ; )

{

}

6)

int i=8 ;

for( ; i >=5 && i <= 10 ; )

{

}

6)

int i=8 ;

for( ; i >= 1 && i <=5 ; )

{

}


do while loop :-
                          This is a Post-test loop ie. In this loop the condition is check after the execution of the statement-block .Due to this feature , The statement-block must execute at-least one time not checking that the condition is true or false .

Syntax :-

do
{
     statement-block     

}while( condition )

Working :-   The working of " do while " loop is .

Firstly it execute the statement-block .
It check the condition .
If condition give Non-Zero result , it repeat the First step .
I condition give Zero value ie. False , it leave the statement-block and execute the rest  of the instructions . 
                           So it must execute the statement-block at least one time , not seeing that the condition is true or false .
Þ In do-while loop we can use any type of valid expression & the statement-block execute only if the expresssion return a Non-Zero result .
do
{
   statement block

}while(expression)

                                                                EXPRESSION

               Non-Zero = TRUE

                     Zero =FALSE

1)

do

{

}while( 1 ) ;

1)

do

{

}while( 0 );

2)

int i=5 ;

do

{

}while( i );

2)

int i=0 ;

do

{

}while(i) ;

3)

int i ;

do

{

}while( i == 8 );

3)

int i ;

do

{

}while(i = 0) ;

4)

int i ;

do

{

}while( i = 1 + 4 ) ;

4)

int i ;

do

{

}while( i = 5 - 5) ;

5)

int i=8;

float j=8.0;

do

{

}while( i == j ) ;

5)

int i=8 ;

float j=8.5 ;

do

{

}while(float (i == j)) ;

6)

int i=8 ;

do

{

}while(i >=5 && i <= 10) ;

6)

int i=8 ;

do

{

}while( i >= 1 && i <=5 ) ;


 COUNTER  VARIABLE :-
                                        Counter Variable is a simple Variable which control that how many time the Loop will execute . The counter variable is Incremented   or Decremented with some fix amount as required .

How to use COUNTER :-

Firstly Declare the Counter_Variable and Initialise it .
Set  appropriate condition according to program need .
Put Increment or Decrement instruction for the counter , in the loop .

Example :-

int i ;                        /*  Declaration of  Counter Variable  */
i = 1 ;                     /*  Initialising Counter  Variable  */
while( i <= 10 )        /*  Condition  */
{
printf("\n %d ", i ) ;
i = i + 1 ;                 /*  Incrementing the Counter Variable  */
}


 

Who's online

We have 40 guests and one member online

About Us

Harsimran Singh

Hello

This website is a special gift for Programming Lovers , which want to learn basic Programming Languages and want to develop there skills Read More....

Our work

Find Us

This email address is being protected from spambots. You need JavaScript enabled to view it.

Harsimran Singh(Facebook)

Harsimran Singh(about.me)

Harsimran Singh(G+)