Exit Control Loop || do while loop in c language

Exit Control Loop || do while loop in c language

Do while loop in c Language

The do-while loop is a type of control loop in the C language that repeatedly executes a block of code until the loop condition becomes false. The do-while loop is similar to the while loop, but the key difference is that the do-while loop guarantees that the loop block will be executed at least once, even if the loop condition is false initially. In this article, we will discuss the do-while loop in detail and how to exit the loop using control statements.

Syntax of the do-while loop:


The syntax of the do-while loop is as follows:

do while syntax



The loop block is enclosed in curly braces and is executed repeatedly until the loop condition becomes false. The loop condition is checked at the end of each iteration, so the loop block is always executed at least once.

Example of the do-while loop:


Let's take a simple example of a do-while loop that calculates the factorial of a number:

do while porgram in c

 

In this example, we prompt the user to enter a positive integer and calculate its factorial using a do-while loop. We initialize the loop counter i to 1 and the factorial fact to 1. We then execute the loop block, which calculates the factorial by multiplying fact with i and incrementing i. The loop continues until i is less than or equal to n. Finally, we print the factorial of n using the printf statement.

Exiting the do-while loop using control statements:

There are two control statements that we can use to exit the do-while loop: break and continue.

Break statement:


The break statement is used to immediately terminate the loop and transfer control to the statement immediately following the loop. For example, let's modify the previous example to calculate the factorial of a number until the factorial becomes greater than 1000:

do while program in c




In this example, we use the break statement to exit the loop when the factorial becomes greater than 1000. We check the value of fact inside the loop using an if statement. If fact becomes greater than 1000, we immediately terminate the loop using the break statement.

Watch Video :



All C  Topics : -

  1. C Language Intro

  2. Structure of C Language 

  3. Variable & data Types 

  4. Printf and Scanf Function

  5. Operator in c Language 

  6. If Statement in c Language 

  7. If else and Nasted if in C

  8. Switch Case In C 

  9. While loop in C Language 

  10. Exit Control Loop (Do While)

  11. For Loop in c 

  12. C Language Pratice Program 

  13. Goto Statement in c Programming 

  14. Array in c

  15. Pointer in  c Language 

  16. String in c Language 

Post a Comment (0)
Previous Post Next Post