While Loop Decrement in C Programming, step by step guide with an easy explanation and detailed source code. Loops are used to perform the same task again and again. Execute the same logic multiple times. But in many cases, we required a decrementing loop where we needed to start from the top and go till 0. Factorial programs are such examples. This helps in solving many complicated mathematical problems.
The 3 important things to write any loops apart from business are
- Initialization, in a decrement loop, initialization always starts from the highest value, unlike increment loop where values start from 0 or 1
- Condition, the condition is generally reverse like i > 0
- Decrement it’s i– or i = i – 1
If you miss anything while writing a loop, it could be an infinite loop. This blog demonstrates loop Decrements in C Programming. To learn more about C Programming and its history, please read this blog first, “C Programming Complete Guide“.
Well, you have understood loops and how to use them. But we also have a decrementing loop. Where the loop goes from top to bottom. Here, initialization, condition, and increment are different. Let’s see how they work.
Table of Contents
Write a program to decrement loop in C
Now suppose we get the requirement that we need to print 10 to 1, i.e. in reverse order. Such loops are called decrement loops. In the decrement loop, we decrease the value. Let’s write an example. We declared a variable i. We set its value to 10. In the decrement loop, we are starting from the top. Now write the WHILE statement. Here I am writing i > 0 inside WHILE we will write printf. Now here we will give the decrement i=i-1.
Declaring Variables in C Program
Here, to start, we will create a program that will add numbers. Here we will create integers using the keyword int variables i. Now we put value 10 in i. Initialization is a process in C Programming where we initialize variables at the start of the program or business logic. Though we have used it to printf statements 9 times in C Programming.
Program Logic to Decrement loop in C Programming
Let’s execute the program.
main() { int i = 10; while (i > 0) { printf("%d ", i); i=i-1; } }
Output :
10 9 8 7 6 5 4 3 2 1
If you want to copy-paste the code, please refer to our Newtum Github repository link over here. Now you see, we got the output 10 to 1. In the while statement, we will write a condition that is i less than 0; remember, we have used this condition in IF and ELSE statements. Here we are telling our program to run the print value statement till 1. But the value of i is only 0. Here we have initialized the variable i equal to 10. This is called “initialization.” Next, we have given the condition and after that, we have given the decrement. Now, the only thing is the printf statement. Instead of printf, we can write any logic that we want, So What we understand is for Loop execution.
You can watch the video over here, which explains the blog in a simple video with VFX animation.
3 important parts of any loop
As you know, there are 3 important parts of any loop,
- 1st is an initialization,
- second is condition and
- third is the increment.
When you change any of the parameter’s behaviours, the loop changes. When we created the decrement loop, we changed the initialization because, as per the requirement, we wanted to print 10 to 1, so initialization should start from 10. Look at the condition now; Earlier, we have written conditions like counter i < 10, so that when i reaches to value 10, the loop breaks. But in this case, our loop starts from 10 and goes on reducing. So we write the condition, i > 0. Here, i start with 10, then it will become 9, 8, 7, and finally 0. As soon as it reaches 0, the loop breaks.
Next is decrement operators; here, we have written i = i – 1, it’s because we want to decrement the value of i every time it is printed. So the initial value of i was 10, now 10-1 will become 9 and so on. Until it reaches 0. So this was about the decrement loop now, we will study increment and decrement operators.
How to view all topics on C Programming
Watch the full programming series on Amazon Prime free with Amazon Prime Membership.
- Amazon Prime link for the USA: https://www.amazon.com/dp/B07WFYJW6G
- Amazon Prime link for the UK: https://www.amazon.co.uk/dp/B07WFYQZ1G
- For Other Countries: To watch C Programming Content in another country, please visit the URL:https://newtum.com/course-details/c-programming-online.