Control Statements in C, Part 2

By Surabhi Saxena

Fundamentals of C

In the previous article on control statements we discussed the decision-making statement if-else and the selection statement switch-case. In this article we will continue the discussion on control statements and discuss the iteration statements for, while and do-while.

Iteration Statements

Iteration statements are used to execute a particular set of instructions repeatedly until a particular condition is met or for a fixed number of iterations.

1. The for statement

The for statement or the for loop repeatedly executes the set of instructions that comprise the body of the for loop until a particular condition is satisfied.


for(initialization; termination; increment/decrement) { //statements to be executed }

The for loop consists of three expressions:

  • The initialization expression, which initializes the looping index. The looping index controls the looping action. The initialization expression is executed only once, when the loop begins.
  • The termination expression, which represents a condition that must be true for the loop to continue execution.
  • The increment/decrement expression is executed after every iteration to update the value of the looping index.

The following program uses the for loop to print the series: 0,1,1,2,3,5,8,13 … to n terms.

#include<stdio.h> int main() { int i,n, a, b, sum=0;  printf("Enter the number of terms:");  scanf("%d",&n);  a=0; b=1;  printf("%dn %d",a,b); for(i=2;i<n;i++)   { sum=a+b;  printf("n%d",sum);  a=b;  b=sum;  }  return 0; }

If the first two elements 0 and 1 are excluded, it is seen that each of the next elements in the series is the sum of the previous two elements. For example, the third element is a sum of the first two elements (0+1), the fourth element is a sum of the second and the third elements (1+1=2), the fifth element is a sum of the third and the fourth elements (1+2=3) and so on. Therefore, each time it is the sum of the previous two elements that is printed. The previous value of b becomes the new value of a and the previous value of sum becomes the new value of b. The newly calculated sum becomes the next element and is subsequently printed. These steps are executed repeatedly until the looping index i does not reach one less than the number of terms entered by the user. The looping index i begins from 2 as the first two terms have already been printed. It could also range from 0 to 1 less than n-2 as:


or from 1 to n-2 as:

for(i=1; i<(n-2);i++)

The for statement is very versatile and can be written in four different ways:

1. Omitting the Initialization Expression

In this case, the looping index is initialized before the for loop. Thus the for loop takes the following form:

i=0;/*initialization*/ for(; condition;increment/decrement expression)  {  //statements to be executed }

Notice that the semicolon that terminates the initialization expression is present before the condition expression.

2. Omitting the Condition

In this case the condition is specified inside the body of the for loop, generally using an if statement. The while or do-while statements may also be used to specify the condition. Thus the for loop takes the following form:

for(initialization; ; increment/decrement expression)  { condition //statements to be executed }

Again, it can be seen that the semicolon that terminates the condition is present in the for statement. The following program explains how the condition can be omitted:

#include<stdio.h> int main()  { int i,n, a, b, sum=0;  printf("Enter the number of terms:");  scanf("%d",&n); a=0; b=1;  printf("%dn %d",a,b);  for(i=2; ;i++)  {  if(i==(n-1)) //condition specified using if statement  break;  sum=a+b; printf("n%d",sum); a=b;  b=sum;  }  return 0; }

3. Omitting the increment /decrement Expression:

In this case the increment/decrement expression is written inside the body of the for loop.


#include<stdio.h>  int main()  { int i,n, a, b, sum=0;  printf("Enter the number of terms:");  scanf("%d",&n); a=0; b=1;  printf("%dn %d",a,b);  for(i=2;i<n;)  { sum=a+b; printf("n%d",sum);  a=b;  b=sum;  i++;  //increment expression }  return 0;  }

4. Omitting all Three Expressions:

It is also possible to omit all three expressions, but they should be present in the ways discussed above. If all three expressions are omitted entirely — ie, they are not mentioned in the ways discussed above — then the for loop becomes an infinite or never-ending loop. In this case, the for loop takes the following form:

for(;;)  { //statements }

2. The while statement

The while statement executes a block of statements repeatedly while a particular condition is true.

while (condition)  { //statement(s) to be executed }

The statements are executed repeatedly until the condition is true.

Example: Program to calculate the sum of the digits of a number (eg, 456; 4+5+6 = 15)

#include<stdio.h>  int main() { int n, a,sum=0;   printf("n Enter a number:");  scanf("%d", &n);   while(n>0)   { a=n%10; //extract the digits of the number  sum=sum+a; //sum the digits  n=n/10; //calculate the quotient of a number when divided by 10. }  printf("n Sum of the digits=t %d",sum); return 0; }

The above program uses the while loop to calculate the sum of the digits of a number. For example, if the number is 456, the while loop will calculate the sum in four iterations as follows. It would be helpful to remember that % gives the remainder and / the quotient.

Iteration 1: n>0  Condition is true(n=456)



n=n/10= 45;  New value of n is 45.

Iteration 2: n>0 Condition is true(n=45)



n=n/10= 4;   New value of n is 4.

Iteration 3: n>0 Condition is true(n=4)



n=n/10= 0;  ew value of n is 0.

Iteration 4: n>0 Condition is false(n=0).

After the fourth iteration control exits the while loop and prints the sum to be 15.

Example 2: Program to check whether the entered number is a palindrome or not.

A palindrome is a number which remains the same when its digits are read or written from right to left or vice versa, eg 343 is a palindrome, but 342 is not. The following program works on the logic that if the reverse of the number is same as the original number then the entered number is a palindrome, otherwise it is not.

#include<stdio.h>  int main()  { int a, n,m, reverse=0;  printf("n Enter a number:");  scanf("%d", &n); m=n; while(n>0) {  a=n%10; reverse=reverse*10 +a;  n=n/10;  }  if (m== reverse) {  printf(" n The number is a palindrome.");  } else  { printf("n The number is not a palindrome."); } return 0; }

The above program uses almost the same logic as the program concerning the sum of digits. As was seen in that program, n becomes 0 in the last iteration. However, we need to compare the original value of n to the reverse of the number to determine whether it is a palindrome or not. Therefore, the value of n has been stored in m, before entering the while loop. The value of m is later compared with reverse to decide whether the entered number is a palindrome or not.

The while loop works in the following way:

Let n=343;

Iteration 1: a= n%10=3;



Iteration 2: a= n%10=4;



Iteration 3: a= n%10=3;



Iteration 4: n>0 condition false(n=0).

Control exits from the while loop.

3. The do-while loop

The do-while statement evaluates the condition at the end of the loop after executing the block of statements at least once. If the condition is true the loop continues, else it terminates after the first iteration.

do { //statements to be executed;  } while(condition);

Note the semicolon which ends the do-while statement. The difference between while and do-while is that the while loop is an entry-controlled loop — it tests the condition at the beginning of the loop and will not execute even once if the condition is false, whereas the do-while loop is an exit-controlled loop — it tests the condition at the end of the loop after completing the first iteration.

For many applications it is more natural to test for the continuation of a loop at the beginning rather than at the end of the loop. For this reason, the do-while statement is used less frequently than the while statement.

Most programs that work with while can also be implemented using do-while. The following program calculates the sum of digits in the same manner, except that it uses the do-while loop:

            int main()
            int n, a,sum=0;
            printf("n Enter a number:");
            scanf("%d", &n);  
            printf("n Sum of the digits=t %d",sum);
            return 0;

However, the do-while statement should be used only in situations where the loop must execute at least once whether or not the condition is true.

A practical use of the do-while loop is in an interactive menu-driven program where the menu is presented at least once, and then depending upon the choice of the user, the menu is displayed again or the session is terminated. Consider the same example that we saw in switch-case. Without using an iteration statement like do-while, the user can choose any option from the menu only once. Moreover, if a wrong choice is entered by mistake the user doesn’t have the option of entering his choice again. Both these faults can be corrected by using the do-while loop.

#include<stdio.h>  int main() {  //create file &set file pointer .  int choice; char ch;   printf("n Main Menu ");  printf("n 1. Add a record at the end of the file."); printf("n 2. Add a record at the beginning of the file:"); printf("n 3. Add a record after a particular record:";  printf("nPlease enter your choice:(1/2/3)?");  scanf("%d",&choice); do  {   switch(choice)  { case 1: //code to add record at the end of the file  break; case 2: //code to add record at the beginning of the file break; case 3: //code to add record after a particular record  break;  default: printf("n Wrong Choice");  }  printf(" Do you want to continue updating records(y/n)?");  scanf("%c", ch);  }while (ch=='y'||ch=='Y');    return 0;  }

The next article will discuss jump statements: goto, break, continue and return.

Meet the author
My name is Surabhi Saxena, I am a graduate in computer applications. I have been a college topper, and have been awarded during college by the education minister for excellence in academics. I love programming and Linux. I have a blog on Linux at
  • Roger

    In “2. Omitting the Condition”, it’s an infinite loop. Maybe you forgot the else block and a break statement?
    Also the “do-while” example actually is equals to the “while” example, except for the comments.
    By the way, good post!

    • Surabhi Saxena

      Thank you for pointing out the errors. I have corrected both of them.
      Also, I am happy that you liked the post.


Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Front-end, once a week, for free.