do while loop in CA loop is a programming control structure that allows you to execute a block of code indefinitely if a specific condition is met. Loops are used to execute repeating activities and boost programming performance. There are multiple loops in the C programming language, one of which is the "do-while" loop. A "do-while" loop is a form of a loop in C that executes the code block first, followed by the condition. If the condition is true, the loop continues to run; else, it stops. However, whether the condition is originally true, it ensures that the code block is performed at least once. do while loop syntaxThe syntax of the C language do-while loop is given below: The components are divided into the following:
Working of do while Loop in CLet us look at an example of how a do-while loop works in C. In this example, we will write a simple program that questions the user for a password and keeps asking until the right password is input. Example: The program runs as follows:
Output: Let us walk through a possible scenario: Enter the password: 123 Enter the password: abc Enter the password: secret Access Granted! Explanation: In this example, the user initially enters the wrong passwords, "123" and "abc". The loop prompts the user until the correct password "secret" is entered. Once the correct password is provided, the loop terminates, and the "Access granted!" message is displayed. Example of do while loop in C:Example 1: Here is a simple example of a "do-while" loop in C that prints numbers from 1 to 5: Output: 1 2 3 4 5 Explanation: In this example, the code block within the do loop will be executed at least once, printing numbers from 1 to 5. After each iteration, the i value is incremented, and the condition i<= 5 is checked. If the condition is still true, the loop continues; otherwise, it terminates. Example 2: Program to print table for the given number using do while Loop Output: Enter a number: 5 5 10 15 20 25 30 35 40 45 50 Enter a number: 10 10 20 30 40 50 60 70 80 90 100 Example 3: Let's take a program that prints the multiplication table of a given number N using a do...while Loop: Output: Let us say you enter the number 7 as input: Please enter a number to generate its multiplication table: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 The program calculates and prints the multiplication table for 7 from 1 to 10. Infinite do while loopAn infinite loop is a loop that runs indefinitely as its condition is always true or it lacks a terminating condition. Here is an example of an infinite do...while loop in C: Example: In this example, the loop will keep running indefinitely because condition 1 is always true. Output: When you run the program, you will see that it continues printing "Iteration x", where x is the iteration number without stopping: Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on) To interrupt an infinite loop like this, you generally use a break statement within the loop or some external condition you can control, such as hitting a specific key combination. In most desktop settings, the keyboard shortcut Ctrl+C can escape the Loop. Nested do while loop in CIn C, we take an example of a nested do...while loop. In this example, we will write a program that uses nested do...while loops to create a numerical pattern. Example: In this program, we use nested do...while loops to generate a pattern of numbers. The outer loop controls the number of rows, and the inner loop generates the numbers for each row. Output: Let us say you input five as the number of rows: Enter the number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Explanation: In this example, the program generates a pattern of numbers in a triangular shape. The outer loop iterates over the rows, and the inner loop iterates within each row, printing the numbers from 1 up to the current row number. Difference between while and do while LoopHere is a tabular comparison between the while loop and the do-while Loop in C:
While Loop: The loop body is executed before the condition is checked. If the condition is initially false, the loop may not execute. Do-while Loop: The loop body is executed at least once before the condition is checked. This guarantees that the loop completes at least one iteration. When you want the loop to run based on a condition that may be false at first, use the while loop, and when you want the loop to run at least once regardless of the starting state, use the do-while loop. Features of do while loopThe do-while loop in C has several fundamental characteristics that make it an effective programming technique in certain situations. The following are the significant characteristics of the do-while loop:
Next Topic C while loop |