What are Loops in C?
Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. A loop in C consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false. The purpose of the C loop is to repeat the same code a number of times. the do-while loop is one of the loop statements that we are going to discuss in this article.
Do While loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop in C programming checks its condition at the bottom of the loop.
A do-while loop is similar to a while loop, except for the fact that it is guaranteed to execute at least one time.
Using the do-while loop, we can repeat the execution of several parts of the statements.
The do-while loop is mainly used in cases where we need to execute the loop at least once. The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end user.
A do-while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
Syntax of the do-while loop in C programming language is as follows:
Syntax of Do-While Loop in C:
do { statements } while (expression);
In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.
The critical difference between the while and do-while loop is that in the while loop the while is written at the beginning. In the do-while loop, the while condition is written at the end and terminates with a semi-colon (;)
How do-while loop works?
- The body of the do-while loop is executed once. Only then, the condition is evaluated.
- If the condition is true, the body of the loop is executed again and the condition is evaluated once more.
- This process goes on until the condition becomes false.
- If the condition is false, the loop ends.
Program to add numbers until the user enters zero
#
int main()
{
double number, sum = 0;
do
{
printf(“Enter a number: “);
scanf(“%lf”, &number);
sum += number;
}
while(number != 0.0);
printf(“Sum = %.2lf”,sum);
return 0;
}
Output, do-while loop
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
The Break Keyword in C
The break in C is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.
The statement ends the loop immediately when it is encountered. It is almost always used with the if…else statement inside the loop.
Its syntax is:
break;
When the break keyword is encountered within a loop, the loop is immediately terminated and the program control goes to the next statement following the loop.
Continue in C, do-while loop
This works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, the continue statement causes the conditional test and increment portions of the loop to execute.
It is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.
A continue statement has the form: 1 continue; A continue statement can only appear within the body of an iterative statement, such as do, for, or while.
Reference
http://www.freebookcentre.net/Language/Free-C-Books-Download.html
Conclusion of do-while loop
The explanations are easy to understand and simple. Try to understand, you will get it easily. Read the explanation for more clarification and stay tuned for the upcoming blogs.
Also, read
https://curledmark.in/the-largest-number-program-in-an-array-in-c-programming/
https://curledmark.in/armstrong-number-and-arrays-in-c-programming/
https://curledmark.in/prime-reversepower-programs-in-c-programming/
The Book Of Five Rings Summary
How to Talk to Anyone by Leil Lowndes
Jonathan Livingston Seagull Book Summary
Your Brain on PORN Book Summary by Gary Wilson
THE IMMORTALS OF MELUHA By Amish Tripathi full summary
Eleven Minutes by Paulo Coelho Summary
Hyperfocus by Chris Bailey Summary
Black Beauty by Anna Sewell Summary
You Can Heal Your Life by Louise Hay Summary
ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY
THE BIOGRAPHY OF SWAMI VIVEKANANDA
More Stories
File Handling implementation in C language
What are loops in C programming?For,While,Do-While
What are Functions in C Programming