C Programming Examples
-
C Program to Demonstrate the Use of a do-while Loop
Welcome, learners! In this blog post, we will explore the fascinating world of do-while loops in the C programming language. Whether you’re new to programming or have been coding for a while, understanding loops is crucial as they allow us to repeat a block of code multiple times, automating repetitive tasks and making our programs more efficient.
Introduction to Loops
In programming, loops execute a block of code repeatedly until a certain condition is met. They enable us to perform tasks that require repeating a fixed number of times or satisfying a particular termination condition. C provides different types of loops, including the
for
loop,while
loop, and thedo-while
loop. In this post, we will focus on thedo-while
loop.Overview of the do-while Loop
The
do-while
loop in C is a post-test loop, meaning that the condition is checked after the loop body is executed. It has the following structure:do { // code to be executed } while (condition);
Here’s how the
do-while
loop works:The code inside the loop body executes at least once, regardless of the condition.
After executing the loop body, evaluate the condition.
If the condition is
true
, the code inside the loop body is executed again.If the condition is
false
, the loop terminates, and the program continues with the next statement after the loop.
The key difference between the
do-while
loop and other loops like thewhile
loop is that thedo-while
loop always executes the code inside the loop body at least once.Why Use a do-while Loop?
The
do-while
loop is particularly useful in scenarios where we need the loop to execute at least once, regardless of the initial condition. We often use it to perform a task repeatedly until we meet a certain condition, evaluating the condition after each iteration.Now that we have a basic understanding of the
do-while
loop, let’s dive into some practical examples to see it in action.Example 1: Sum of Numbers
Let’s suppose we want to calculate the sum of numbers entered by the user until they enter a negative number. We can use a
do-while
loop to achieve this. Here’s the code:#include <stdio.h> int main() { int number; int sum = 0; do { printf("Enter a number: "); scanf("%d", &number); sum += number; } while (number >= 0); printf("Sum = %d", sum); return 0; }
In this example, we first initialize the
sum
variable to 0. Inside thedo-while
loop, we prompt the user to enter a number and scan it usingscanf()
. We then update thesum
variable by adding the entered number. The loop continues until the user enters a negative number. Finally, we display the sum of all the numbers entered by the user.Notice that the loop body executes at least once, even if the user initially enters a negative number. This behavior is what sets the
do-while
loop apart from other types of loops.Example 2: Guessing Game
Let’s explore another example to illustrate the use of the
do-while
loop. Suppose we want to create a simple guessing game, where the program generates a random number, and the user has to guess that number. The program gives hints like “Too high” or “Too low” until you guess the correct number. Here’s the code:#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int number; int guess; int attempts = 0; srand(time(0)); number = rand() % 100 + 1; printf("*** Guessing Game ***\n"); do { printf("Enter your guess: "); scanf("%d", &guess); attempts++; if (guess > number) { printf("Too high!\n"); } else if (guess < number) { printf("Too low!\n"); } } while (guess != number); printf("Congratulations! You guessed the number in %d attempts.", attempts); return 0; }
In this example, we generate a random number between 1 and 100 using the
rand()
function from thestdlib
library. Thesrand(time(0))
line is used to seed the random number generator with the current time, ensuring that we get a different sequence of random numbers each time we run the program.Inside the
do-while
loop, we prompt the user to enter their guess and scan it usingscanf()
. We then compare the guess with the randomly generated number and provide appropriate feedback. If the guess is higher than the number, we print “Too high!” and if it is lower, we print “Too low!”. The loop continues until the user guesses the correct number.After the loop terminates, we display the number of attempts it took the user to guess the correct number.
Conclusion
Congratulations on reaching the end of this blog post! We have covered the basics of the
do-while
loop and demonstrated its use through practical examples. Remember, thedo-while
loop is a powerful tool for automating repetitive tasks and ensuring that a block of code is executed at least once.To reinforce your understanding, I encourage you to experiment with the examples we discussed and try implementing your own programs using the
do-while
loop. The more you practice, the more comfortable you will become with loops and programming in general.Keep coding and exploring new concepts! Happy programming!