Monday, July 27, 2009

How can i rewrite this using a do/while loop in C++ ?

How can i rewrite this using a do/while loop in C++ ?





int ctr;


cin%26gt;%26gt;ctr;


while (ctr %26lt;=20)


{


cout%26lt;%26lt;ctr;


ctr++;


}

How can i rewrite this using a do/while loop in C++ ?
int ctr;


cin%26gt;%26gt;ctr;


do


{


if(ctr%26gt;20)


break;


cout%26lt;%26lt;ctr++;


}while(1);





Above method is more fool-proof.. Another simpler way is..





int ctr;


cin%26gt;%26gt;ctr;


do


{


cout%26lt;%26lt;ctr++;


}while(ctr %26lt;= 20);





Problem in this method is, if you get ctr=21(say) in cin, then 21 will be printed.. In the first code, this is avoided.

sweet pea

No comments:

Post a Comment