Comparison between C + + and Java break statements and continue statements with and without labels __java

Source: Internet
Author: User

Today, while reviewing Java, I found a Java break statement and a continue statement and a c++/c language inside a little different.

First, let's introduce the break and Continue statements :

Both break and continue statements can skip part of the code.

You can use the break statement in a switch statement or any loop to cause the program to jump to the statement at the end of the switch or loop.

The continue statement is used in loops, allowing the program to skip the remaining code in the loop and start a new round of loops.

First of all, in two languages, the basic usage of break and continue is consistent.

We mainly talk about their differences here.

The break in Java contains both labeled break statements and no label break statements.

No label: Just jump out of the loop where the current break statement is, and use the same as C + +.

And with the label: not only can jump out of the current break in the loop, you can also jump out of the label refers to the cycle.

and in C + + it seems that can not be labeled.

Let's use an example to illustrate the following:

Let's take a look at the example of Java with a label:

Package Testonr;

public class One 
{public
	static void Main (string[] args) 
	{
	outer:for (int i = 0; i < i++)
			{
  
   system.out.print ("i=" +i+ "\t,j=");
				for (int j = 0; J <= i; j +)
				{
					System.out.print (j+ "\ t");
					if (i+j = =)
					{
						System.out.print ("Loop interrupted.") ");
						Break outer;
					}
				System.out.println ("The last sentence of the foreign circulation." ");
			}
			
	}
}
  
Enter the result:


The same code complains in C + +:



So let's get rid of the labels and try.

#include <iostream>
using namespace std;
void Main ()
{for
	 (int i = 0; i < i++)
		{
			cout << "i=" << I << "\t,j=";
			for (int j = 0; J <= i; j +)
			{
				cout << J << "T";
				if (i + j = =)
				{
					cout << "Loop interrupted. ";
					break;
				}
			}
			cout << "Outer loop last sentence" << Endl
		}
}

Output results:



As you can see from the diagram, the tagged break statement in Java jumps out of the loop of the current break statement, and also out of the circular statement specified by the label.

and C + + break is not as direct as Java and the label, so we can only jump out of the loop of the break statement, and can't jump out of the other loops, so we could see in C + + output statements inside more than Java output one line (that is, Java jumped out of the label refers to the large cycle, and c+ + does not jump out).


Let's look at the Continue statement:

Java Band Label:

Package Testonr;

public class One 
{public
	static void Main (string[] args) 
	{
	System.out.println (the prime number within "100 is:");
	Loop: for
		(int i = 2; i < i++) {for
			(int j = 2; J < I/2; J +)
			{
				if (i%j = 0)
					continue loop;
			}
			System.out.print ("" "+i);}}}


Output results:


C + + is still the same error:



Let's get rid of the markings behind the Contiune, try:

#include <iostream>
using namespace std;
void Main ()
{for
	 (int i = 2; i < m i++)
		{for
			(int j = 2; J < I/2; J +)
			{
				if (i%) j = = 0)
					continue
			;
			cout << "" << i;
		}
}

Output results:



Here we can see the big difference.

First, analyze the Java code:

The loop here is the label of the outer loop, when executed to if (i%j==0), if the IF condition is set up, execute Contiune, the program execution flow not only jumps out of the current continue in the loop (small loop) also jumps out the label loop callout's This cycle , because as long as the condition is established that it is not prime, there is no need to output, skip directly here.

and C + + statements inside, execution to if (i%j==0), if the conditions set up to execute continue, the program only jumped out of the current continue in the loop (inner loop), and did not jump out of the circulation, so, The output statement in the cycle is executed whether or not the condition of if is established.


Summary: The younger brother as a beginner is not to prove that a language is superior or inferior, but simply to discuss this inadvertently found the difference, if there is wrong place, I hope that the great God Welcome to point out, little brother grateful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.