Difference between while loop and do-while loop in C?In this article, we learn comparison between the while loop and do-while loop constructs in C language. At the beginning of this article, we understand the concept of while loop and do-while loop in C. After that, we learn the comparisons of while and do-while loop in C programming. While loop statement:While loop statement is one of the easiest and most widely used looping constructs in the C programming language. In this statement, while it is a reserved word or keyword, the condition can be any constant, variable, expression, and statement can be a single statement or a group of statements. In the while loop, we have to perform three steps:
Initialization Value: In a while loop, the first step is initialization is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter in while loop. For Example: i = 1; Test Condition: After the initialization step, the next step in the while loop is to check the test condition whether the loop is executed or not. If the condition is true in the while loop, the loop's body is executed; otherwise, there is no loop execution. For Example: while (i<=10) Increment and decrement: After checking the test condition in the while loop, increment and decrement are used to increment and decrement the loop counter's value. For Example: i = i + 1; Syntax of while loop:In a while loop statement, while keyword is used and followed by any good condition that has to be tested enclosed in parentheses and it may contain any logical operator. Condition is followed by a pair of curly brackets that specifies the set of statements that are to be executed while the condition is true. Example:Flowchart of while loop statement:do-while loop statement:In the C programming language, the do-while loop statement is also similar to a while loop in the C programming language. In this, first of all, the loop's body is executed then the condition is checked. In the do-while loop, we have to perform three steps:
Initialization value: In a do-while loop, the first step is initialization and is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter. For Example: i = 1; Test Condition: After the initialization step, the next step in the do-while loop is to check the test condition whether the loop is executed or not. For Example: while (i<=10); Increment and decrement: After checking the test condition in the do-while loop, the next step is to increment and decrement the loop counter's value. For Example: i = i + 1; Syntax of do-while loop:Here, two keywords are used for the do-while loop statement is do and while. If the condition is true in the do-while loop, the statement executes again, and if the condition returns false, execution stops, and control passes to the next statement. Example:Flowchart of do-while loop statement:while loop vs do-while loop in CLet's see some Comparison between the while loop and do-while loop in the C language
Next TopicMemory Layout in C |