C Command Line Tutorial 6 - Code indentation, increment/decrement operators, do-while and for loops, and more
We have covered a total of 5 C programming tutorials so far. Each tutorial focused on something specific. In process of remaining close to the topic, some generic concepts remained untouched. Some of those concepts we'll be discussing here in this tutorial.
So let's begin.
1. Code indentation
You should always properly indent your code. For starters, indentation is a practice where-in you put spaces/tabs/newlines to make the code look more readable. Usually, lines of code associated with a block or loop are positioned at same gap. For example, here's an unindented code:
#include <stdio.h>
int main (void)
{
int c =0, counter=0;
c = getchar();
while(c != EOF)
{
c = getchar();
if(c == '\n')
counter = counter+1;
}
printf("The input contains %d lines", counter+1);
return 0;
}
And here's the same code with indentation applied:
#include <stdio.h>
int main (void)
{
int c =0, counter=0;
c = getchar();
while(c != EOF)
{
c = getchar();
if(c == '\n')
counter = counter+1;
}
printf("The input contains %d lines", counter+1);
return 0;
}
So you can see, the indented code looks sorted out and easy to read and review.
2. Increment and decrement operators
You may have noticed in previous tutorials, we have used the following way to increment a variable:
a = a+1;
While there's nothing wrong with this way, there's another way which is popular and used a lot. It's to use an increment operator.
a++
Using this way, the value of 'a' increments by 1. Note that this is a post-increment. There's a pre-increment operator as well:
++a
The difference between the two is in post-increment, the variable is first used and then its value if increment. On the other hand, in pre-increment, the value is first increased and then the variable is used.
The following piece of code will better explain the difference.
#include <stdio.h>
int main (void)
{
int a =0, b=0;
printf("a = %d", a++);
printf("\n b = %d", ++b);
return 0;
}
The output of this program is:
a = 0
b = 1
So you can see, while post increment didn't reflect immediately, pre-increment did.
The same logic applies to decrement operators as well.
#include <stdio.h>
int main (void)
{
int a =1, b=1;
printf("a = %d", a--);
printf("\n b = %d", --b);
return 0;
}
The output is:
a = 1
b = 0
3. Comments
There are two ways in which you can comment code in C. One is using '//'. This way, you can only comment a single line at a time.
// int a = 10;
// a = 9;
The other way is to put /* .... */ around the lines. This allows you to comment multiple lines at once.
/* int a = 10;
a = 9; */
3. For and do-while loop
Up until now, we have only touched upon the while loop, wherein the code execution only enters the while block if the condition is true.
while(condition)
{
/*
line of code
line of code
...
*/
}
There also exists a do-while loop, wherein the block of code is executed once for the first time and then the while condition is checked.
do
{
// few lines of code
} while (condition);
Now, the question is when to use do-while? I'll give you an example. Suppose you want the user to input a value which is greater than 10, and the requirement is to keep asking the user to enter such a value until the program gets one. Then here's how the do-while loop will be helpful in that case:
do
{
printf("Please enter a number greater than 10: ");
scanf("%d", &n);
}while(n<=10);
Next up is 'for' loop, which like 'while' has a condition to check, but also has an increment statement or decrement statement that's helpful in many cases.
for(initialization; condition; increment/decrement)
{
// lines of code here
}
Here's an example code that prints all even numbers between 0 and 20.
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=20;i=i+2)
printf(" %d ", i);
return 0;
}
Of course, there are many tips and tricks related to 'for' loop, but we'll discuss them slowly and steadily in our future tutorials.
Conclusion
Here, in this tutorial, we learned some good tips as well as new concepts. We'll be expanding upon these in tutorials to come. Until then, practice whatever we discussed here today, and drop in a comment in case you have any doubt or query.