Header Ads

Do – while loop:C Programming Language - Part 4.3

Do – while



Do something, as long as one condition is true. We use do while writing programs.
The normal form of do while loop is:

Do statement while (expression);

A condition is given as a condition. As long as this condition is true, this do while loop will continue and this statement will be executed. We do not have to use our second bracket to execute a statement. But if we want to execute more than one statement, then we have to use our second bracket. Then it will be written as:

1
2
3
4
5
6
7
Do {
 Statement 1;
 Statement 2;
 Statement 3;
....
} While (expression);
Write a small program
#include <stdio.h>
Int main ()
{
    Int number = 0;
    Do {
        Printf ("% d \ n", number);
        Number ++;
    } While (number <= 9);
Return 0;
}
View rawdo-while-1 hosted with ❤ by GitHub
In the above program we have got an integer variables called number. The primary value of which is 0. Then we're writing the do while loop. After doing the second bracket we have written the statements that we have to execute. Then while writing (number <= 9) this is the condition. As long as the condition is true, the {{...} code inside the bracket will be executed. In the bracket we first printed the number. Next line number ++; We have increased the number one by one. Then the value of one time number is 10. Then came to the number (number <= 9) and the condition was gone. That means the value of the number is greater than 9, then the do's do not enter inside and its internal code will not execute. The above program can write a little bit longer. Will work the same way. Just removing the brochure.
#include <stdio.h>
Int main ()
{
    Int number = 0;
    Do
    Printf ("% d \ n", number ++);
    While (number <= 9);
Return 0;
}
View rawdo-while-c-2 hosted with ❤ by GitHub
If we want to execute a single Statement, then we will not be using our second bracket.

By using do while we will find the average of some numbers. The program will ask the first user, want to use the average of the number. Then one by one will take all the number inputs. Then the average will show.

#include <stdio.h>
Int main ()
{
    Int total_no, count = 1;
    Float number, average, sum = 0;

    Printf ("how many numbers?");
    Scanf ("% d", & total_no);


    Do {

        Printf ("Enter number% d:", count);
        Scanf ("% f", & number);
        Sum + = number;
        Count ++;

    } While (count <= total_no);

    Average = sum / total_no;
    Printf ("Average is:% f \ n", average);

Return 0;
}
View rawaverage-do-while hosted with ❤ by GitHub
We've declared many variables. Total_no is the average number of quotes, for that. Then we entered the do while loop. The loop will continue until all of our numbers are taken in input. For that we took a count variable. The first value is 1. After that we will take a number of inputs each time, once we increase the count's value.

With the variable called number, we are taking input of every number. I've added all of those in input. With a small line. Sum + = number; Which means sum = sum + number;

When the value of count has been greater than total_no, it means that all our numbers have been taken in input. So we left the do while. Then we average. Then print it.


No comments

Theme images by sebastian-julian. Powered by Blogger.