4

Is this a loop, not a loop, or both a loop and not a loop? If you're counting the number of loops in a program, would you count this?

do {
   ...
}while(0);

I'm curious because I'm working with someone who has a project that requires him to count the number of loops in a program.

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46
  • 2
    It's a while loop, despite the false value, so I assume it is. – Jeremy Jul 14 '16 at 14:07
  • It's as much a loop as `for (int i = 0; i < 1; i++) { /* ... */ }` – pmg Jul 14 '16 at 14:09
  • 1
    @Richie obvoiusly a good link, but not a dupe here. – Sourav Ghosh Jul 14 '16 at 14:10
  • @KerrekSB please tell me more about the *errr...* – Szabolcs Dombi Jul 14 '16 at 14:14
  • @DombiSzabolcs: Seriously? What ever happened to just [trying it out yourself](http://ideone.com/RhGHca) before posting? – Kerrek SB Jul 14 '16 at 14:16
  • 1
    The compiler might completly optimize away the `while` construct, so all that would be left in the compiled binary is the `...` part. – Jabberwocky Jul 14 '16 at 14:16
  • BTW why would you need to know the number of loops in your program ? – Jabberwocky Jul 14 '16 at 14:18
  • 2
    @MichaelWalz - `re: "why would you need to know...."`, homework, for sure – KevinDTimm Jul 14 '16 at 14:29
  • 1
    You are effectively asking for a definition of "loop". To the extent that there is any uncertainty or room for disagreement, this is a matter of opinion, and thus not appropriate for an SO question. – John Bollinger Jul 14 '16 at 14:30
  • I don't see the point in counting the number of loops, a better approach IMHO would be counting the time complexities of the loops. This one in particular has O(1). – babon Jul 14 '16 at 15:09
  • 1
    This question does not ask about the usage of `do...while..`, as was in the marked dupes. As I already commented, IMHO, this is not a dupe, please let me know if anyone disagrees. – Sourav Ghosh Jul 15 '16 at 07:11

3 Answers3

7

In C standard, C11, chapter §6.8.5, Iteration statements, it is listed

iteration-statement:

       while ( expression ) statement
       do statement while ( expression ) ;
       for ( expression opt ; expression opt ; expression opt ) statement
       for ( declaration expression opt ; expression opt ) statement

The do statement while ( expression ) ; is mentioned, so yes, it is a loop (iteration) statement, irrespective of the number of iterations it make in a particular implementation/usage.

FWIW, this loops one time.

To add some more clarification, quoting from Wikipedia, (emphasis mine)

A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop, shown below as xxx) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

In a sense, yes it is a loop, but in reality no. It is technically a loop because you are using loop syntax, but what you're actually doing is just running the code in your "loop" once, and then breaking. I wouldn't really count it as a loop because you're not adding any algorithmic runtime to your program, so there's no point in counting it as a loop. Note: don't do this, it is pointless and bad coding practice.

EDIT: Credits to Jeremy in the comments below, there is at least one case where this construct can be useful in C: do { ... } while (0) — what is it good for?

Community
  • 1
  • 1
Andrew
  • 388
  • 2
  • 9
0
do {
   ...
}while(k);

Now the number of iterations depend on the value of k, now you cant say something like its a loop only if k!=0 or if k is True.

So its basically a loop.

shiva
  • 2,535
  • 2
  • 18
  • 32