The language is C++ and I am trying to use system("cls") in a do{} while loop, but I don't want it to run this statement the first time or the second. I want this statement to run the 3rd time. is there any way to run this code inside a do-while loop after every 3 iteration of the loop? Thanks
Asked
Active
Viewed 2,589 times
0
-
2Instead of `system("cls");`, I'd use something like [this](http://stackoverflow.com/a/7911218/179910). Much faster, and about the same degree of portability (i.e., none in either case). – Jerry Coffin Dec 03 '14 at 18:31
3 Answers
2
Instead of i%3
as others have suggested, I'd avoid the (usually fairly slow) division operation and just reset the counter when it fires:
int i=0;
do {
if (++i == 3) {
clear_screen();
i = 0;
}
} while (whatever());
Granted, when you're clearing the screen (especially via a system
call) it's so slow the difference from avoiding division is a drop of water in the ocean, but cultivating good habits is a good thing, and in this case you gain speed with no loss of readability, so it's clearly a net gain.

Jerry Coffin
- 476,176
- 80
- 629
- 1,111
1
int i=0;
do
{
i++;
if(!(i % 3))
system("cls");
}while(True)

Ari0nhh
- 5,720
- 3
- 28
- 33
-
1There is no reason to increase and check separately - `if( !( ++i % 3 ) )` IMHO more readable or even `if( ++i % 3 == 0 )` – Slava Dec 03 '14 at 18:52
1
Yes, you can add a counter and provide an if conditon like as follows :-
int c=0;
do{
c++
....
....
if (c%3==0)
system("cls");
...
...
}while();
adjust this code as desired :-)

3Demon
- 550
- 3
- 9