[cs23021] block or statement in do-while look

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Sep 16 13:18:20 EDT 2010


Sections 1 and 2,

We had a discussion last time whether there needs to be a block in a
do-while loop. No, a single statement is possible (even though it
would probably be rarely used. The below program compiles and runs as
expected


----------------------------
// demonstrates a single statement do-while loop
// Mikhail Nesterenko
// 9/16/2010

#include <iostream>

using namespace std;

int main(){
  int i=5;
  do
    cout << "i = " << i-- << endl;
  while( i > 0);

}
--------------------------

producing the following output

---------------------------
i = 5
i = 4
i = 3
i = 2
i = 1
---------------------------

Thanks,
--
Mikhail


More information about the cs23021-2 mailing list