July 25, 2018

The while Loop in C


   Loops are used when we want to execute a part of the program or a block of statements several times. 


For example, we want to print "Hello World" 10 times. One way to get the desired output is to write 10 printf statements, as shown below :



/* C program to illustrate need of loops */

#include <stdio.h>

int main(void)
{
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
    printf("Hello World\n");
   
    return 0;
}


But what if we want to print it 100 or 1000 times. As you can see this is not ideal way to solve this problem. Using loops we can solve this kind of problem easily. We can write one loop statement and only one printf statement, and this approach is definitely better than the first one. With the help of loop we can execute a part of the program repeatedly till some condition is true.

There are mainly two types of loops:

  1. Entry Controlled loops : In this type of loops the test condition is checked before entering the loop body. for loop and while loop are entry controlled loops.
  2. Exit Controlled Loops : In this type of loops the test condition is checked after executing the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. do – while loop is exit controlled loop.

The while Loop

The while loop provides a mechanism to repeat one or more statements while a particular condition is true.

The syntax of while loop is:

while(test condition)
{
    /* body of the loop */
    statement block;
}


In the above syntax, we have written statement block. A statement block may include one or more statements. The test condition may be any expression. The loop statements will be executed till the condition is true i.e., the test condition is evaluated and if the condition is true, then the statement block is executed. When the condition becomes false the execution will be out of the loop.

Note : The braces are needed only if the body of the loop contains more than one statement. However, it is a good practice to use braces even if the body of the loop contains only one statement.


Flow Diagram:



while loop in C



Steps of while loops are as follows:
  1. The test condition is evaluated and if it is true, the statement block is executed.
  2. On execution of the body, test condition is repetitively checked and if it is true the statement block is executed.
  3. The process of execution of the statement block will be continued until the test condition becomes false. Therefore you must always include a statement which alters the value of the condition so that it ultimately becomes false at some point. Note that if the condition is never updated and the condition never becomes false, then the computer will run into an infinite loop which is never desirable.
  4. The control is transferred out of the loop.


Example 1:

The following program uses while loop to print "Hello World" 10 times.

#include <stdio.h>

int main(void)
{
    int i = 1;
   
    /* while loop execution */
    while(i < 11)
    {
        printf("Hello World \n");
       
        i++; /* statement that change the value of condition */
    }
   
    return 0;
}


 Output :

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World



Explanation:

Here initially i=1 and is less than 11, i.e., the condition (i < 11) is true, so in the while loop "Hello World" is printed and the value of i is incremented using expression i++. As there are no more statements left to execute inside the body of the while loop, this completes the first iteration. Again the condition (i < 11) is checked, if it is still true then once again the body of the loop is executed. This process repeats as long as the value of i is less than 11. When i reaches 11, the loop terminates and control comes out of the while loop.



Example 2:

The following program uses while loop to calculate the sum of individual digits of an entered number.

#include <stdio.h>

int main(void)
{
    int n, num, sum = 0, remainder;

    printf("Enter a number : ");
    scanf("%d", &n);

    num = n;

    while( num > 0 )
    {
        remainder = num % 10; /* take out the last digit of a number */
        sum = sum + remainder; /* add the remainder to the sum */
        num = num / 10;
    }

    printf("\nSum of digits of %d = %d", n, sum);

    return 0;
}


 Output :

Enter a number : 437

Sum of digits of 437 = 14


Explanation:

Here we are extracting the digits of the number from right to left and then these digits are added one by one to the variable sum. Note that the variable sum is initialized to 0. This is because we are adding some numbers to it, and if not initialized then these numbers will be added to garbage value present in it.


Working of Program:


Sum of Digits



Example 3:

The following program uses while loop to check whether the entered number is palindrome or not.

#include <stdio.h>

int main(void)
{
    int n, num, reverse = 0, remainder;

    printf("Enter a number : ");
    scanf("%d", &n);

    num = n;

   
   
    while( num > 0 )
    {
        remainder = num % 10;
        reverse = reverse*10 + remainder;
        num = num/10;
    }

    /* palindrome if n and reverse are equal */
   
    if(n == reverse)
        printf("%d is a palindrome.", n);
    else
        printf("%d is not a palindrome.", n);

    return 0;
}


 Output :

Enter a number : 16461
16461 is a palindrome.


Explanation:

Palindrome is a number that is equal to it's reverse, for example 16461. The program above first simply reverses the number and stores it in reverse. Then it compares reverse with n (original number) to tell you whether or not the number is a palindrome.



Working of program:


Palindrome Number in C



next    The for Loop in C
top    Index
prev    Decision Making in C





No comments:

Post a Comment