Monday, 23 November 2015

Loops Of Programming Language...

Loops Constructs                      Written By Rana Sarmad

Loop:
            A statement of a set of statements that is executed repeatedly is called Loop. The statement of a set of statements in a loop is executed for a specified  number of time until given condition remains true.

Types of Loops In Programming language:
                        The are three types of loops in programming language. These are given below:
1: While Loop
2: Do-While Loop
3: For loop

While loop:
            It’s  a Conditional loop statement. It is used to execute a statement(s) as long as the given condition remains true.

Syntax:
while(Condition)
{
            Statement(s)... ;
}

Condition: Condition consist of a relational expression. If it is true, the statement  or the set of statements given in the while loop is executed.

Statement: It represents the body of the loop. The compound statement(s) are written within the curly braces {}.

Flow Chart of The While Loop:

Do While Loop:
            It is same as the while lOop but in this loop the statement must be executes at least once.  After then the condition of the loop will be checked.
Simply in this loop the body of the loop executed before the condition.

Syntax:
do
{
Statement(s)... ;
}
while(Condition);
do : It’s simply meaning is do(Karo) in Simply word we can use  the word__Execute__ here.  the body of the loop execute first.

Statement : It’s represents the body of the loop.

Condition: it is the condition . if it is true the body of the loop execute again otherwise it’ll not.

Flow Chart of the Do-while loop

The Difference between While and do-while loop


               ( while Loop )                     ( do-while Loop )

         ·   The test condition comes before the body of the loops. First condition check and then body execute if the condition is true.
          ·  The while statement does end with the semicolon (;).
              
        ·  The test condition comes after the body of the loop. First body executes and then condition is checked. The body will executes if conditon become true.otherwise not.
           · The while statement ends with semicolom.


                                                     


For Loop:
            The “For Loop” statement or loop is used to execute a set statement repeatedly  for a specified number of times. It is also know as the counter loop.

Syntax:
            for(initialization; condition; increment/decrement)
_______________it’s the syntax and after then it’s body.
{
Statements ;
}

Initialization: in initialization the variable assigned to an initial value. More than one value may be initialize. For example:
for(a = 6, b = 5, a<10; a++)
Condition , Increment , Decrements  are we read previous.
Flow Chart of for Loop

          

The Nested Loop : 
                        A Loop  structure completely inside the body of the another loop structure is called nested loop. The following points should be considered while writing the nested loop 

  • Nesting can be done up to any level . However, as the level of nesting increased, the loop structure becomes more complex.
  • Any loop structure can be placed within any other loop structure. for example, a "do while Loop"  may be placed within the "for loop" or "while loop"
The concept of nested loops is explained in the following program which will print asteriks (*) According to the pettern shown below:


* * * * *
* * * * 
* * *
* * 
*

#include <stdio.h>
void main (void)
{
     int       inner, outer ;
    for (outer = 5 ; outer >= 1 ; outer --)
        {
            ineer = 1 ;
            while (inner <= outer)
                     {
                         printf("*") ;
                         inner ++ ;
                      }
            printf("/n") ;
          }
}


In the above program... 
  • The while loop is used inside the body of the for  loop. the while loop is, therefore, the nested loop. the for loop is the outer loop. 
  • the outer loop is executed 5 times. 
  • For each iteration (Repeatation) of the outer loop, the inner loop executes until the value of the variable becomes outer. thus with each iteration of the outer loop, the number of iteration of the inner loop decreases....

Click here for Download

No comments:

Post a Comment