![](/uploads/1/2/5/8/125836309/844419665.jpg)
Switch Case Statement Example Program In C Programming Language; C Character Set; Convert a Floating-point value to an Integer in C; Data Input and Output gets and puts Example Program In C; Special Operators In C; Pointer Representation and Pointer Example Programs; C Data Input and Data Output; Simple While Loop Example Program In C.
In this C programming class, we’ll cover the C while and do-while loop statements. The loops are the main constructs to implement iterative programming in C.
Learn C Loops: While and Do-While
Now that you have started this journey of learning C programming, there will be instances where you may need to run a particular statement block more than once. It may be for input, processing or output.
There are mainly three types of loops in C. In this tutorial, we will see the first two loops in detail.
- Programming Exercises
1. What are Loops In C Programming?
To perform a particular task or to run a specific block of code several times, the concept of LOOP comes in picture.
A loop is an instruction given to the computer that it has to run a specific part of the code for a given number of times.
2. While Loop
The while loop begins by first checking the terminal condition and then decides whether to enter the loop or not.
If the underlying condition is true, then it goes ahead and executes the block of code in the loop. After the first iteration, it again checks with the changed (increased/decreased) values of the variables (the condition operands) and decides the further course of execution.
2.1. Flowchart
The below flowchart will help you understand the functioning of the while loop.
2.2. Syntax
2.3. While Loop Example
Here is a simple example of how a while loop works? This program prints numbers from 1 to 10 without actually using the ten printf statements but a while loop.
Here, the “n” in the printf call is used to move to the next line.
Its output should look something like this-
3.Do-While Loop
In this loop, the statement block gets executed first, and then the condition is checked.
If the underlying condition is true, then the control returns to the loop otherwise exit it.
![Example programs in c using do while work Example programs in c using do while work](/uploads/1/2/5/8/125836309/627971090.jpg)
3.1. Flowchart
The below flowchart will help you understand the functioning of the do-while loop.
3.2. Syntax
3.3. C Do-While Loop Example
Here is a simple example to find the sum of 1 to 10 using the do-while loop
Its output should be something like this-
Generally, the do-while loop is not preferred in applications as it first executes the block of statements and then checks the condition. It risks the security which is like allowing an unauthorized person into a facility and then asking for his ID.
Above was the explanation of the while and do-while loops. We will see the for loop in detail in the next chapter.
There is an exercise you can perform on the next page which will help you understand these two loops nicely.
4. C Loops Exercises for Practice
Write a program in C to multiply two numbers without actually using the * operator but have to use both the while and do-while loops.
4.1. Program using the while loop
4.2. Program using the do-while loop
Keep in mind that in a do-while loop, the semicolon always comes after while statement but not in while loop.
The output for both the following programs is same, check from below screenshot.
![](/uploads/1/2/5/8/125836309/844419665.jpg)