break cont

#include <iostream>
using namespace std;
int main ()
{
int i=400;
do
{
i++;
cout<<"before the break\n";
break;
cout<<"after the break,should never print\n";
}
while(i<3);
cout<<"after the do loop\n";
system("pause");
return 0;
}

the output of this code:
before the break
after the do loop

my question is why the sentence "before break" is printed although the while statement is <3 ?
Last edited on
How is the condition i < 3 connected with the break?! Can you explain?
The break statement means unconditional exit from a loop.
Last edited on
I mean the while statement is less than 3 and i = 400
how the sentence before break is printed if the while statement is i<3
Do-while loops always run at least one iteration because the loop condition is tested at the end of the loop.
Actually I don't understand :(
can someone explain me more please
Last edited on
Don't you know how to test ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
using namespace std;
int main ()
{
int i=400;
do
{
cout << i << endl;    
i++;
cout << i << endl;
cout<<"before the break\n";
break;
cout<<"after the break,should never print\n";
}
while(i<3);
cout << i << endl;
cout<<"after the do loop\n";
system("pause");
return 0;
}
http://www.cplusplus.com/doc/tutorial/control/

The while() and do-while() loops are described in the tutorial.
Topic archived. No new replies allowed.