Unit 3. Loop control structure with Pavan Chavhan.

 




After long time connecting with all of you. I welcome here everyone to my "technical_spc". Before we start with our new topic let's have an overview of the previous topics. In the second unit we learnt the topic Decision Control Structure. In that unit the topics that we covered are:

 
        1. Decision making in "c".

        2. Algorithm & Flowchart.

        3. if statement.

        4. if.....else statement.

        5. if.....else if..... statement.

        6. Nested if statements.


In case you have missed any of the topics you can checkout my previous blogs. So now let's start with our new topic unit 3 i.e. loop control and structures.



Loops.





(a). A loop statement allows us to execute a statement or group of statements multiple times.

(b). Its ability to perform a set of instructions repeatedly.

(c). This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.

(d). Type of loops :-

                                     1. while loop
                                   2. do-while loop
                                   3. for loop

(e). Flowchart :-





while loop.




(a). A while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.

(b)Syntax :-

      initialise variable;
        while(condition) 
         { 
           statement(s); 
           increment/decrement;
         }

(c). Flowchart :-



(d). Example :-

  • Program :-



  • Output :-



  • Explanation :-

Step 1 :- The variable count is initialized with value 1 and then it has been tested for the condition.

Step 2 :- If the condition returns true then the statements inside the body of while loop are executed else control comes out of the loop.

Step 3 :- The value of count is incremented using ++ operator then it has been tested again for the loop condition.




do.....while loop.




(a). do...while loop in C programming language checks its condition at the bottom of the loop. 

(b). A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

(c). Syntax :-

        initialise variable;
        do
        {
             statement(s);
             increment/decrement;
          }while( condition );

(d). Flowchart :-




(e). Example :-

  • Program :-




  • Output :-






for loop.




(a). A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

(b). Syntax :-

       for ( init; condition; increment )
        {  
         statement(s); 
        }

(c). Flowchart :-





(d). Example :-

  • Program :-



  • Output :-




  • Explanation :-

Step 1 :- First initialization happens and the counter variable gets initialized.

Step 2 :- In the second step the condition is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop.

Step 3 :- After successful execution of  statements inside  the body of loop,  the counter variable is incremented  or decremented, depending on the operation  (++ or –).




Nested loops.




(a). C programming language allows to use one loop inside another loop.

(b). Syntax :-

       for ( init; condition; increment )
       { 
        for ( init; condition; increment )
            {
              statement(s); 
            } 
         statement(s); 
        } 


(e). Example :-

  • Program :-




  • Output :-






Break statement.




(a). When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.

(b). Syntax :-

               break;

(c). Flowchart :-






(d). Example :-

  • Program :-




  • Output :-








Continue statement.




(a). The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. 

(b). Syntax :-

               continue;

(c). Flowchart :-




(d). Example :-

  • Program :-



  • Output :-




  • Explanation :-

Value 4 is missing in the output, why? When the value of variable j is 4, the program encountered a continue statement, which makes the control to jump at the beginning of the for loop for next iteration, skipping the statements for current iteration (that’s the reason printf didn’t execute when j is equal to 4).



goto statement.



(a). A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function. 

(b). Syntax :-

goto label;
                 .. 
                 . 
                label: statement;

(c). Flowchart :-






(d). Example :-

  • Program :-



  • Output :-



  • Explanation :-

In this example, we have a label addition and when the value of i (inside loop) is equal to 5 then we are jumping to this label using goto. This is reason the sum is displaying the sum of numbers till 5 even though the loop is set to run from 0 to 10.



The infinite loop.





(a). A loop becomes infinite loop if a condition never becomes false.

(b). The for loop is traditionally used for this purpose.

(c). Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.



If you have any questions you may ask in comment below. If you enjoyed this article, share it with your friends and colleagues!





Post a Comment

1Comments

If you have any doubts, Please let me know

Post a Comment