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 -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?