break keyword
break statement neglect the statement after it and exit compound statement. in the loop and transfer the control outside the loop
Break it's sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.
break statement neglect the statement after it and exit compound statement. in the loop and transfer the control outside the loop
Break it's sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.
Example :
while(a)
{
while(b)
{
if(b == 10)
{
break;
}
}
// break will bring us here.
}
continue keyword
Example :
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
while(a < 20)
{
if( a == 15)
{
// skip the iteration
a = a + 1;
continue;
}
cout<<"value of a: "<<a<<endl;
a++;
}
return 0;
}
Output :
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
goto
- goto statement transfer the control to the label specified with the goto statement
- label is any name give to particular part in program
- label is followed with a colon (:)
Syntax :
label1:
-
-
goto label1;
Example :
#include <iostream>
using namespace std;
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
cout<<"Outer loop executing. i = "<<i<<"\n";
for ( j = 0; j < 3; j++ )
{
cout<<" Inner loop executing. j = "<<j<<"\n";
if ( i == 5 )
{
goto stop;
}
}
}
// This message does not print.
cout<<"Loop exited. i = "<<i<<endl;
stop:
cout<<"Jumped to stop. i = "<<i<<"\n";
}
Output :
Outer loop executing. i = 0 Inner loop executing. j = 0 Inner loop executing. j = 1 Inner loop executing. j = 2 Outer loop executing. i = 1 Inner loop executing. j = 0 Inner loop executing. j = 1 Inner loop executing. j = 2 Outer loop executing. i = 2 Inner loop executing. j = 0 Inner loop executing. j = 1 Inner loop executing. j = 2 Outer loop executing. i = 3 Inner loop executing. j = 0 Inner loop executing. j = 1 Inner loop executing. j = 2 Outer loop executing. i = 4 Inner loop executing. j = 0 Inner loop executing. j = 1 Inner loop executing. j = 2 Outer loop executing. i = 5 Inner loop executing. j = 0 Jumped to stop. i = 5
0 Comments