Tuesday, December 19, 2017

What is the role of Loop in c programing language

Loop Define in c programing

Loop is Within the context of C programming, a loop is that programming function that enables iteration of a statement, or of a condition, on the basis of specific boundaries.
                   Incidentally, the way loops work is more or less similar across different programming languages.
         A certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number. 
          If it hasn't, the next instruction in the sequence is an instruction to return to the first instruction in the sequence and repeat the sequence. 
             If the condition has been reached, the next instruction "falls through" to the next sequential instruction or branches outside the loop. 
     A loop is a fundamental programming idea that is commonly used in writing programs.
                     Notably, when you are working with a given statement, or with a set of instructions, execution continues until such a time as a specified boundary condition – the anticipated loop body – is met. 
                Thereafter, repetitions of that initial cycle of operation can continue, involving the whole loop body, until execution of the entire code block is accomplished as required.
                     Still, on the overall, you get statements being executed in a sequential order. In short, if you have more than one statement in the same function, the program executes the first statement first; the second one follows; and so on.
            It is worth noting that you can have different execution paths even when they are complex.
        The main reason is programing in the loop it is the statement they are facilities execution other statement are severally.

Different types are loop how  works in c language programing system:- 

  1. The while loop system 
  2. The for loop system
  3. Do...while loop system
  4. Nested loop system
  5. The infinite loop system

1. The while loop system :

The while loop operates while a certain condition happens to be true, repeating a statement or set of statements. It actually performs the evaluation of the test expression. It then ensures the condition is tested before it can execute the loop body. However, if the test expression proves to be false, what follows is the termination of the while loop.

 While loop in C language is used to execute a block of code several times until the given condition is true. The while loop is mainly used to perform repetitive tasks. Unlike for loop, it doesn't contains any initialization and update statements.

Syntax of while loop :-
           While  (Condition ) {statement;}

Digram of while loop 

Note that the statement needs not be one; there could be more.

2. The for loop system:-

                 In this loop, execution of the initialization statement occurs just once. What follows immediately after is the testing of the expression.
                    If ever it is found false, returning zero (0), the loop gets terminated. However, in case the result returned is non-zero, meaning the test has proven the expression to be true, the codes within for loop are automatically executed. 
                   Also the expression gets an update. After several repetitions of this process, the expression being tested finally turns false.
  Syntax of for loop system :-
          (init; condition;increment)
           Statement; s
Diagram for loop 

3. Do... While loop system:- 

     This loop is works almost similarly to how the while loop works but for one difference. 
            Its body gets executed even before it can check the test expression. It is only after that initial execution that the evaluation of the test expression takes place. In the event that the test expression is proven to be true, the do…while loop’s body gets executed once again. 
               The repetition process then continues till such a time as the test expression returns a zero (0) value; meaning its turns false.
                         The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
Syntax for do... While system 

    do { statement; }
   while ( condition );
Diagram of do...while loop

                    need to take note of the position of the statement – not at the end as in other loops, but right at the beginning; preceding the condition. Reason…? As pointed out earlier, the statement within the do…while loop must, of necessity, execute at least one time before it is time to test the condition.

4. Nested loop system :- 

nested loops when you find yourself with loops within the other loops already mentioned – the while; the do…while; or even the for loop.

Various Syntax of nested loop system 

a) This is how a nested for loop syntax looks like: for ( init; condition; increment ) { for ( init; condition; increment) { statement; } statement }

b) This is how a nested while loop syntax looks like: while (condition) { while (condition) {statement;} statement; }

c) This is how a nested do…while loop syntax looks like: Do { statement; do { statement; } while (condition);

5.  The Infinite loop system:- 

where a given condition never has a chance of turning out false? Such are the situations that call for the use of the for loop. Of course, of the three for expressions, none will be required.
                 So, what you end up with is an endless loop simply because your conditional expression will, certainly, be empty.
               That is how you end up with an endless – or infinite – loop.
                      And just in case you feel like terminating your infinite loop, simply press the keys, Ctrl + C simultaneously.






Please comment and follow this site blog inbox