while loop in C – Part 2

Read previous tutorial while loop in C – Part 1.

In the last tutorial we have learnt about the while loops. Loops are used very frequently while writing big programs. And in the last tutorial we have learnt only the basics of the loop control structure. In this tutorial I will not write any program. Instead of it I will tell you about some tips and traps of using while loop in C.

If you have write any program using while loop for the first time. Then it is quite possible that you will get many errors. This tutorial mainly focuses to make you confident while writing loop control structure in programs.

Tips and traps of while loop

1. As I told you earlier the general form of while loop is.

initialise loop counter; 
while(condition)
{  do this; 
  and this;  
  increment loop counter; 
}

We can add any valid condition or expression with while keyword. Any expression which evaluates as non zero is said to be true and the expression which evaluates zero is said to be false.

2. We can use logical operators to describe condition. This is perfectly fine for all loops.

while(x<=60) 
while(x>=50&&y<=75)

3. If you want to execute only one statement in while loop then you can also drop the curly braces { }. Default scope of while loop is one statement below the while keyword.

while(x<=20)
i=i+1;

is same as

while(x<=10)
{  
i=i+1; 

}

However if you want to execute multiple statements in while loop then it is compulsory to use curly braces.

4. Remember to increment or decrement the loop counter. Otherwise it will result in an infinite loop. One common mistake is given below.

main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
}
}

The above program will result in an infinite loop because we are not changing the value of loop counter. Condition is always true as value of loop counter remains same and the loop will get executed infinitely.

Correction of above code is given below.

main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
i=i+1;
}
}

5. Never write a semicolon after while keyword. It will result in an infinite loop. A common error code is given below.

main()
{
int i=1;
while(i<=10);
{
printf(“%d”, i);
i=i+1;
}
}

Checkout I have given semicolon after while keyword. So do not make this mistake.

Few more operators

Post Increment/Decrement Operator

This operator is commonly used with loops. You must have noticed that most of the time we use expression i=i+1 to increment the loop counter. To make this a bit easy to write we can use post increment and decrement operator.

So i=i+1 is same as i++ (post increment operator).

and i=i-1 is same as i—(post decrement operator).

Pre Increment/Decrement Operator

This operator is similar to post. But with a small difference. This operator gives priority to incrimination in the function.

i=i+1 is same as ++i (pre increment operator).

i=i-1 is same as –i (pre decrement operator).

Sounds confusing? As both are same.

Well write one program with below function to understand difference between them.

i=1;
j=10;
prinf(“%d %d n”, ++i, i++);
prinf(“%d %d n”,–i, i–);
prinf(“%d %d n”, ++j, j++);
prinf(“%d %d n”,–j, j–);

Compound Assignment Operator

It is also similar to the above operators. The best way to understand them is by syntax.

i=i+5 is same as i+=5.

Or

i=i-5 is same as i-=5.

Or

i=i*6 is same as i*=6.

This operator can be used with arithmetic operators like +, -, *, / and %.

Leave a Reply

Your email address will not be published. Required fields are marked *