Loops



Loops are one of the most important part in c programming , loops are a piece of coding that repeats itself again and again until a condition or a terminating condition is meet , there are different kinds of loops and we will look at each of them
In order to better understand loops let us take an example and see how we can use different loops to solve it

So let us consider that we want to find the sum of all the number from 1 to 100 in a program , In this type of condition we use loop

While Loop

The syntax of while loop is like

While ( condition )
{
Coding
}

So to better understand this let us look an example of finding the sum of 1 to 100

main()
{

int total = 0;
int counter = 1;
      
while(counter <= 100)
{
                     
total = total + counter;
counter = counter + 1;
                    
}
                    
                     
printf("%d",total);
                    
getch();
}

So as you can see in the program the coding inside the while loop will continue until the value of counter becomes 100 , that means the loop will run for 100 times and within this time total will be incremented to find the sum of all the number from 1 to 100

How about writing the multiplication table of two

main()
{
      int num =2;
      int total = 0;
      int counter = 0;
      int val ;

while(counter < 10)
{
             
              counter = counter +1;
             
              val = num * counter;
             
              printf("%d * %d = %d\n",num,counter,val);
             
              }

    getch();


}

These are some of the thing we can do with loop , but there are many more



For Loop

For loop is another kind of loop , its syntax is different from while loop but we can do almost the same thing with for loop also

for( initialization ; condition ; increment )
{
}

Initialization is much like declaration , here we define the starting point , the condition defines the ending point and increment defines how we will go from initial point to end , the following example will make more sense

Lets do the above examples using for loop

main()
{
     
     int total = 0;
     int  counter = 1;
     int i;
     
      for(i = 1 ; i<=50 ; i++)
      {
      total = total + i;
      }
     
    printf("%d\n\n",total);
    getch();
}

above program also finds the sum of numbers from 1 to 50
now let us do the multiplication table of 2 in for loop

main()
{
     
      int total = 0;
      int  counter = 1;
      int I , j = 2;

      for(i = 1 ; i<=10 ; i++)
      {
 
      total = j * i;
 
      printf("\n%d * %d = %d\n",j,i,total);

      }

getch();

}

We have made a program to display multiplication table of two using for loop



Do while Loop

Do while loop is like while loop but the difference is that the code inside the do while loop execute once even if the condition is not meet
The syntax for do while loop is

do
{
}
while (condition );

the sum of 1 to 100 with do while loop looks like this

main()
{
       int total = 0;
       int counter = 1;
      
      
     do  {
                     total = total + counter;
                     counter = counter + 1;
                    
                     }while(counter <= 100);
                    
                    

                     printf("%d",total);
                     
                     getch();
}



No comments:

Post a Comment