how do you write a c++program using the do while and while do loop to list prime numbers between one and thirty

#include <iostream>

int main()
{
  do
  {
    std:: cout << "2,3,5,7,11,13,17,19,23,29" std::endl;
  } while (false);
}

Your example will output the data and then return (you forgot the return statement, btw) because the condition will always evaluate to false. If you stated it as a while()... statement instead it would have to have been while(true) {/*output;*/ break; } which is somewhat uglier. There are times for while loops (usually), but there are times for do...while loops as well. So, you have written a most elementary do...while single pass loop. Show how you would write a simple while loop to do the same thing.

the condition will always evaluate to false.

Gosh, will it really?

commented: From the department of sarcasm department? :-) +13

@rubberman According to the standard 3.6.1.5 main has an implicit return 0; if one is not provided

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
return 0;

@Nathan - Ok, but I prefer to never leave things to chance. Not all compilers are so compliant...

Not all compilers are so compliant...

That sounds reasonable on the surface, but has an undertone of irrationality if you think about it. Where do you draw the line for features that are hard requirements in a conforming compiler? If the bar is as low as this, it can be argued that it's impossible to write a valid C++ program because some compiler somewhere will fail to be compliant enough in all of the right arbitrary places.

I'll also ask if you can name one C++ compiler that claims to support any ISO standard, yet doesn't support this particular "feature".

Ok deceptikon. I don't know since I ALWAYS return a value from main(). In many cases it is an error condition that occurred that the shell needs to know about. Just returning 0 by default does no one any good, IMO.

I almost never (code in a)

return 0;

at the end of main ...
IN a C++ program

Wheres as,

IN C ...

one ALWAYS needs to return an int value in main, and to code that in explicitly, if the compiler conforms to any standard from C89/C90 forward ...

BUT ...

in C++,

with every standard C++ (since at least C++98? ... maybe earlier?) ...

the (standard conforming) compiler WILL ALWAYS supply code to return 0 for the program, if the programmer did not code it in ...

This is similar to how the C++ compiler will always supply code for a default ctor... if NO ctors were coded in a class ... or a struct ... in C++

So, please feel very comfortable NOT being concerned about the programmer adding code,

return 0;

at the end of main, in a C++ program, because the compiler will then always code it in for you.

The only place(s) where you may need to code return 0 (or some other int value) ...

IS ...

if you are returning from some other place(s) BEFORE the end of main is reached.

Please note also, a trend in C++ ...

with C++11 to C++14 ...

it seems to be the trend to have the compiler DO MORE (behind the scenes) supplying code ...

so that the programmer can do less mundane typing ... just to do various routine jobs ...

for example:

for( auto item : someCppContainer ) cout << item << endl;
// replaces using iterators or indexed loops //

Intentional programming. Always do what you intend. NEVER depend on some other service to do it for you if at all possible. Not explicitly returning 0 at the end of main() in a C++ program may not be technically necessary, but it IS being lazy! And in my 30+ years of experience, lazy programmers cause a LOT more problems than those who are thinking about what they are doing, and act accordingly.

I'm also now thinking ... a very good case could be made, for 'being lazy' ... by NOT keeping up with modern trends in C++ coding, and thus NOT using new coding tools to save your employers time and money :)

Unless that is, he/she is getting paid for your hours worked ...

and thus is ok with you 'padding' the hours ...

coding in unnessary things,

that the compilers are now designed to do for you :)))

Wheres as,

IN C ...

one ALWAYS needs to return an int value in main, and to code that in explicitly, if the compiler conforms to any standard from C89/C90 forward ...

The clause for omitting a return from main was added in C99 and retained in C11.

Thank you, @deceptikon, for that update ...

The clause for omitting a return from main was added in C99 and retained in C11.

I was not aware of that, so thank you very much for updating me there.

(When I code in C, my habit has been to code to the old C89/C90 standard ... just to keep in practice there ... and it seems a lot of 'C beginning student code' ... still needs to conform to that old C89/C90 standard, also.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.