Loops

  • Looping is process of performing some task repeatedly till certain condition is met
  • loops are very best way to perform repeated task.
  • looping are very efficient
  • lets see what will be the approach, of  writing
    your name 10 times, without using looping function.
  • #include<stdio.h>
    
    void main()
    {
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");
    printf("Roy\n");              //this will print your name 10 times.
    printf("Roy\n");
    printf("Roy\n");
    }
    

    Output:

    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
  • You saw, how was the approach ,it took 10 to 12 lines of code to do that work.
    this work will be done very easily by looping function.(while loop)
  • #include<stdio.h>
     void main()
    {
    int i;
    while(i >= 10)
    {
    printf("Roy\n");
    i++;
    }
    }
    

    Output:

    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy
    Roy

    you saw that how easy it was to write, through looping functions.

  • Using this approach is good as well as efficient.

while Loop

Syntax:

#include<stdio.h>
 void main()
{
while(condition)
{  

 // body of while loop.
//code written in this block will run repeatedly till the condition evaluated to  false.
}
}

  • while loop is also called entry control loop, because condition is checked first.
  • First the Condition is evaluated if, condition evaluated is true then the body of loop is executed.
  • After execution again condition will be checked and body of loop going to be executed again.
  • It will perform same task repeatedly until the condition gets false.
  • if you want to stop loop at certain point then
    You can terminated(stop) the loop by using break keyword.
  • Soon we will going to cover break keyword in our further modules.

Lets see a program to understand it better

Program: Print numbers from 1 to 5.

#include<stdio.h>

void main()
{
int i=1;  //Initializing  the variable 

while(i  <= 5)    //condition is checked for every  iteration or repetation. . 
{            // if satisfies the condition  code execute again. and variable get increment by one.
                                           
//block of while loop
printf("%d\n",i);
i++;                                 //incrementing the variable.
                                                                              
}                             

Output:

1
2
3
4
5

Let's Trace The Output and Understand code.

  • Condition is that 'i' should be less than or equal to 5,if the value of 'i'
    becomes greater than 5 which is '6' than,
    while loop will get terminated(stop),and control will go to the next piece of code in the program .
  • In this case i should be '1 ,2,3,4,5' But not greater than 5.
  • Step by Step Explanation.
  • The variable i was 1,it came in the while loop as condition is checked before so here condition was checked and
    it evaluated true. so, it printed the value of i which is 1 and incremented that value by 1 now value of 'i' has become 2
  • with 'i' value '2' condition is checked another time ,and this time also it evaluated true, so it printed the value of i which is 2 and incremented that value by 1 which is 3 now.
  • The checking the value of i and printing of i variable and incrementing it by 1,this process was repeated till the value of i had reached to 6.
  • Once the condition gets false, which in this case at the value of i = 6,then loop will get terminated and control will not stay within the will loop.
  • In below photo you can get clear idea of how variable changed and get loop got terminated at the end. 
  •    
  • At i =6 loop got terminated because while checking the condition was evaluated as false, because i had reached to 6,which is greater than 6.





Further Concepts






  Practice Programs



  • Programs Regarding If-else Statements
  • Program Regarding Nested If - statements
  • Programs Regarding loops





  Further Topics