Decision Making and Looping in C

In this post we are going to discuss the basics of loops. This loops are very helpful for Programming languages Not only C but all of them.


To execute a segment of a program up to given terminating condition repeatedly is called looping in a programming language.


  • for e.g. adding numbers from 1 to 10
                    int i=0,sum=0;
                     while(i<=10)
                                    {
                                     sum=sum+i;
                                      i++;
                                      }

  • The above program adds the numbers 1 to 10, but here using a loop each and every time it enters the loop with an incremented value of 'i' and checks the condition weather it is satisfied or not.
  • We will discus the functioning of while loop later in this post.

We can execute a segment of a program repeatedly by declaring a counter kind of variable. and testing it in a IF statement.
This method is one of the way to make a loop of given condition

                  sum=0;
                  n=1;
                 loop:
                 sum=sum+n;
                 if(n==10)
                          goto print;
                 else
                        {
                           n=n+1;
                           goto loop;
                         }
                   print:
                     //statements//



In the above method the IF ELSE condition is used to make a loop to add numbers from 1 to 10.
But in this case a function goto used to make loop possible which uses a point, where the programs jumps to, when the goto statement is encountered

A program loop consist of two things one is called the body of the loop and the other thing is called the control statement of the loop.

  • The body of a loop consist of the statements which are repeatedly executed, with (may be/optional) some value increasing or decreasing.
  • The Control statement section consist of one terminating condition of the loop if that condition is not satisfied than the loop will end.
Depending on the position of the control statement in the loop a control structure is said to be entry-controlled loop or Exit controlled loop.

The main difference between Entry controlled and Exit controlled loop is that Entry controlled loop may not run at least once if the terminating , in the beginning of the loop, terminates the loop. But the exit controlled run at least once once irrespective of the test condition because it checks the condition after completing one loop.

If the test condition is as such that it never gets the loop to terminate, then the loop will never end and keep on running , this situation is called infinite loop.

A looping process needs to have some basic things needed to have a working and correct loop they are:

  1. setting and initialization of condition variable
  2. Execution of the statements in the loop.
  3. Test for a specified value of the condition variable for the execution of the loop.
  4. Incrementing or updating the condition variable.

C has three types of loops they are:

  • The while loop statement
  • The do while statement
  • the for loop statement

The While Statement
The while loop is the simplest of all loop in C. The basic syntax of While loop is :

   while(test condition)
   {
       body of loop
   }

It is wiser to note that there is no semi colon after the while statement but the statements inside the loop i.e, body of the loop, must have regular syntax as they have normally.

The while loop is an entry-controlled loop statement. The test condition is checked and if the condition is true, then the body of the loop is executed once and then again test condition is checked, in this way the loop continues to run until the test condition becomes false.


The Do While Statement
The while loop is the simplest of all loop in C. The basic syntax of While loop is :


do {
       body of loop
   }while(test condition)


The do-while loop is an exit-controlled loop statement. The loop is executed once and then the test condition is checked and if the condition is true, then the body of the loop is executed once again , in this way the loop continues to run until the test condition becomes false.



The FOR Statement
FOR loop is another entry controlled loop and its syntax is as below
   for(initialization ; test condition ; increment)
        {
          body of the loop
         }
The execution of FOR loop takes place as:

  • First in for(initialization ; test condition; increment) part :
    • The initialization section includes the initialization of a variable which is also called as control variable of the loop.
    • Test condition includes the terminating condition tested with the control variable of the terminating variable.
    • Increment part include the incrementation of the value of control variable, here in this section a value can also be decremented according to the requirement.
  • for example to make a loop of adding first 10 numbers as 1+2+3....+10 then we can do that with a for loop as well as while loop as below:
    • for(i=0;i<=10;i++)
                         {
                            sum=sum+i;
                            }
    • Here we have only included the main for loop part neglecting the declaration of variable.
    • i=0;
                     while (i<=10)
                      {
                        sum=sum+i;
                        i++;
                        }
    • In both of the loop the control variable( here 'i') needs to be initialized, for loop provides the syntax to initialize it while initializing the loop, while using while loop we need to initialize the variable externally before starting the loop.

Nesting of for loops

The way of nesting of for loops is the same as nesting of if  statement. Nesting means that one for loop inside another for loop.
If you face any problems in programming using the nested for loop, you can tell me in the comments.
Just one thing to keep in mind is that you have to keep your logic clear and make sure to use loop control variable carefully. Because it can take you to the infinite loop.

Comments

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 2: Line Chart