logo of SA Coder
        Menu
sign in

      References




In C programming, loops are used to execute a set of statements repeatedly until a certain condition is met. There are three types of loops in C: the while loop, the for loop, and the do-while loop. In this article, we will focus on the do-while loop and the continue statement.


Do-While Loop



The do-while loop is similar to the while loop, but the difference is that in the do-while loop, the condition is checked at the end of the loop. This means that the statements inside the loop will be executed at least once, even if the condition is false. The syntax of the do-while loop is as follows:


copy

do {
   // statements
} while (condition);



Let's take an example to understand this better:


copy

#include <stdio.h>

int main() {
   do {
      printf("Hello World!\n");
   } while (0);

   return 0;
}



output


Hello World!



Even though the condition is false, the loop is executed once, and the output is "Hello World!". Now let's take a more practical example:


copy

#include <stdio.h>

int main() {
   int num;
   char flag;

   do {
      printf("Enter a number: ");
      scanf("%d", &num);
      printf("Square of %d is %d\n", num, num * num);
      printf("If you want to enter another number, enter y/n: ");
      fflush(stdin);
      scanf("%c",&flag);
   } while (flag == 'y');

   return 0;
}



output:


Enter a number: 5 Square of 5 is 25 If you want to enter another number, enter y/n: y Enter a number: 7 Square of 7 is 49 If you want to enter another number, enter y/n: n



In this example, the user is asked to enter a number, and the program calculates its square. Then the user is asked if they want to enter another number. If the user enters 'y', the loop continues, and if the user enters 'n', the loop terminates.


Continue Statement



The continue statement is used to skip a particular iteration of a loop. When the continue statement is encountered, the control passes to the beginning of the loop, and the next iteration is started. The syntax of the continue statement is as follows:


copy

continue;



Let's take an example to understand this better:


copy

#include <stdio.h>

int main() {
   int i = 1, j = 1;

   while (i <= 5) {
      while (j <= 5) {
         if (i == j) {
            j = j + 1;
            continue;
         }
         printf("(%d %d) ", i, j);
         j = j + 1;
      }
      j = 1;
      i = i + 1;
   }

   return 0;
}



output


(1 2) (1 3) (1 4) (1 5) (2 1) (2 3) (2 4) (2 5) (3 1) (3 2) (3 4) (3 5) (4 1) (4 2) (4 3) (4 5) (5 1) (5 2) (5 3) (5 4)



Conclusion:


The do-while loop is similar to the while loop, but the difference is that the while loop is a pre-test loop, which means that the condition is tested at the beginning of the loop. If the condition is false, the loop ends without executing any statements within the loop. The continue keyword is used to skip a particular iteration of the loop and move on to the next iteration. When the continue statement is encountered, the control automatically passes to the beginning of the loop. The continue statement is usually used with the if statement. In the example given, the continue statement is used to skip the same pairs in the loop. When the same pair is encountered, the j variable is incremented, and the continue statement skips to the next iteration without executing the printf statement. The loop then continues with the next value of j. Overall, do-while loops and the continue keyword are important constructs in C programming that allow for more flexible and efficient control flow in programs.




Please login first to comment.