Saturday 20 October 2012

Difference between C++ and Java

This is the 4th post in "Difference between C++ and Java" series.
Use of 'break' and 'continue' statement
Statement
'break' and 'continue' statements in Java can have labels and they can be used to 'break' out or 'continue' with nested loops.
Break statement is normally used, to exit the loop, when certain condition becomes true. Along with the keyword 'break', we can also specify that which loop must a program exit.
Example,
  • Java
int i=0;
System.out.println("Before the loop");
out:
for(i=1;i<=5;i++)
{
if(i==3)
break out;//specify the label of loop
System.out.println("Hello "+i);
}
System.out.println("Out of the loop now");
Output of the above snippet
Before the loop
Hello 1
Hello2
Out of the loop now
This was similar to what we did in C++.

Now,Consider the code
for(int i=1;i<=3;i++)   // outer loop
{
    for(int j=1;j<=3;j++) // inner loop
     {
        System.out.println("Hello "+j);
        break;// want to break out of outer loop
     }
}
 In this case, we are using multiple loops.In C++, we were only able to exit the inner loop by using break statement in inner  loop, but in Java, we can also exit the outer loop using break statement in inner loop. But  we need to mention label for the loop involves. Here label just acts as a bookmark. Now consider the code.


  • Java

int i=0;
System.out.println("Before the outer loop");
out:
for(i=1;i<=5;i++)
{
System.out.println("Inside outer loop");
System.out.println("Before inner loop");
inner:
        for(int j=1;j<=3;j++)
{
if(j==3)
break out;
System.out.println("Hello "+"i="+i+" j="+j);
}
System.out.println("After of inner loop");
}
System.out.println("After  outer loop ");
Output of above code snippet

Before the outer loop
Inside outer loop
Before inner loop
Hello i=1 j=1
Hello i=1 j=2
After  outer loop 
The problem has been eradicated by the use of labels analogous to bookmarks. Inside the inner loop we have used the label outer inside the inner loop to instruct the JVM (run-time environment) that from which loop control should break.
In C++, we can achieve similar effect by the use of flags as shown in the code below.
  • C++
int i=0;
int flag=0;
cout<<"Before the outer loop"<<endl;
for(i=1;i<=5;i++) //Outer Loop
{
cout<<"Inside outer loop"<<endl;
cout<<"Before inner loop"<<endl;
for(int j=1;j<=3;j++) //Inner Loop
{
if(j==3)
{
                  flag=1;
                  break;
                }
cout<<"Hello i="<<i<<" j="<<j<<endl;
}
if(flag==1)
          break;
cout<<"After of inner loop"<<endl;
}
cout<<"After  outer loop "<<endl;
Output of this code is same as above code. Here we have achieve the same effect but this code is obviously inefficient as compared to code in Java. This is because here we are checking the flag each time the body of outer loop is executed.
We use 'continue' statement when we want to make a loop skip rest of its statement for a particular value of loop variable.
By the use of labels in case of 'continue' statement,we make outer loop to skip rest of its statements for current value of loop variable by the use of labels. Following example will make you more clear about the same.
  • Java
int i=0;
out:
for(i=1;i<=3;i++)
{
        inner:
for(int j=1;j<=3;j++)
{
if(i==2)
continue out;
System.out.println("i="+i+" j="+j);
}
}
Output of above snippet
i=1 j=1
i=1 j=2
i=1 j=3
i=3 j=1
i=3 j=2
i=3 j=3
Here we have skipped the execution of loop when value of variable 'i' becomes 2.
Thanks all for your time. We will continue with 'Difference between C++ and Java' series in next post. Any queries and suggestions are invited in comments.



1 comment:

  1. Borgata Hotel Casino & Spa - JTM Hub
    Borgata Hotel Casino & 천안 출장안마 Spa is the premier destination for entertainment, dining, 창원 출장샵 and shopping. With more than 1,000 square feet 제이티엠허브출장안마 of meeting and 당진 출장샵 convention 1xbet login space,

    ReplyDelete

Java vs C++

THis is a basic blog that differentiates features of two of the widely used languages C++ and Java.