Decrements in a loop in C Programming, step by step guide with an easy explanation and detailed source code.
Introduction: Decrements in a loop in C Programming
Loops are used to perform the same task again and again. Execute the same logic multiple times.
But in many cases, we required a decrement loop where we need 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 decrement loop initialization always start from 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 Decrements in a loop in C Programming. To know more about C Programming and it’s history please read this blog first “C Programming Complete Guide 2020“
You can watch the video over here which explains the blog in simple video with VFX animation.
Well, you have understood loops and how to use them. But we also have a decrement loop. Where the loop goes from top to bottom. Here, initialization, condition, and increment are different. Let’s see how they work.
Now suppose we get the requirement that we need to print 10 to 1 i.e. in reverse order. Such loops are called as 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 program or business logic. Though we have used it to printf statement 9 times in C Programming.
Business Logic to Decrement in a loop in C Programming
Let’s execute the program.
If you want to copy-paste the code please refer 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 printf statement
Instead of printf, we can write any logic that we want So What we understand is for Loop execution.
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 behaviors, 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, 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 about increment and decrement operator.
How to view all topics on C Programming
Watch 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