Skip to main content

C Programming while and do...while Loop

C Programming while and do...while Loop

Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn how to create while and do...while loop in C programming.








Loops are used in programming to repeat a block until a specific condition is met. There are three loops in C programming:

while loop

The syntax of a while loop is:
while (testExpression) 
{
    //codes 
}

How while loop works?

The while loop evaluates the test expression.
If the test expression is true (nonzero), codes inside the body of while loop is executed. The test expression is evaluated again. The process goes on until the test expression is false.
When the test expression is false, the while loop is terminated.

Flowchart of while loop


Example 1: while loop

  1. // Program to find factorial of a number
  2. // For a positive integer n, factorial = 1*2*3...n
  3. #include <stdio.h>
  4. int main()
  5. {
  6. int number;
  7. long long factorial;
  8. printf("Enter an integer: ");
  9. scanf("%d",&number);
  10. factorial = 1;
  11. // loop terminates when number is less than or equal to 0
  12. while (number > 0)
  13. {
  14. factorial *= number; // factorial = factorial*number;
  15. --number;
  16. }
  17. printf("Factorial= %lld", factorial);
  18. return 0;
  19. }
Output
Enter an integer: 5
Factorial = 120
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.

do...while loop

The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.

do...while loop Syntax

do
{
   // codes
}
while (testExpression);

How do...while loop works?

The code block (loop body) inside the braces is executed once.
Then, the test expression is evaluated. If the test expression is true, the loop body is executed again. This process goes on until the test expression is evaluated to 0 (false).
When the test expression is false (nonzero), the do...while loop is terminated.

Flowchart of do...while Loop



Example 2: do...while loop

  1. // Program to add numbers until user enters zero
  2. #include <stdio.h>
  3. int main()
  4. {
  5. double number, sum = 0;
  6. // body of loop is executed at least once
  7. do
  8. {
  9. printf("Enter a number: ");
  10. scanf("%lf", &number);
  11. sum += number;
  12. }
  13. while(number != 0.0);
  14. printf("Sum = %.2lf",sum);
  15. return 0;
  16. }
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
https://babuakash.blogspot.com/2019/06/c-programming-while-and-dowhile-loop.html

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

Unordered List, Nested lists, Description List,TUTE-8

                                                             Unordered List An unordered list can be created with the <ul> tag and each list item can be created with the <li> tag as shown by the example below: <ul>   <li>Item</li>   <li>Another Item</li>   <li>Yet Another Item</li>  </ul> This will produce a bulleted list (which is the default style): You should use ul to display a list of items, where the order of the items is not important. If changing the order of the items makes the list incorrect, you should use <ol>.   Nested lists You can nest lists to represent sub-items of a list item. <ul>   <li>item 1</li>   <li>item 2     <ul>    ...