Statements in Java are similar, but not identical, to C/C++.  

The Java Statements

Here is a list of Java statements, with some details on how they differ in some cases from C/C++:

STATEMENTIN C?IN C++?COMMENTS
The empty (null) statementYesYes 
Labeled statementsYesYesMay be referred to from break or continue
Assignment expressionYesYes 
Increment and decrement expressionsYesYes 
Method invocationNoYesJava does not support global functions
C does not support objects/classes
Java only allows certain forms of expressions to be used as expression statements
Class instance creation expressionNoYesJava is richer in this area than C++C does not support objects/classes
ifwhile, and do/while statementsYesYesC and C++ are very cavalier about what is allowed in the conditional expression.
Java requires the condition to be of type boolean;
for example, the following is invalid in Java:
int i = 10; while (i--) { /*...*/ }
switch statementYesYesJava has eliminated some very dubious behavior in switch statements that is present in C and C++
for statementYesYesJava allows comma-separated expressions in the for loop initialization and increment sections, but not in the test sectionJava allows the declaration of local loop variables, as in ISO C++ (but not in C or earlier versions of C++):
for (int i = 0; i < 10; i++) { /* ... */ }
“foreach” statement (enhanced for loop)NoNoAdded in Java 5.0
break statementYesYesCan take an optional label
continue statementYesYesCan take an optional label
No goto statement!YesYesJava has eliminated the notorious goto statement!
return statementYesYes 
trythrowcatch and finally statementsNoYesJava supports exceptions somewhat like C++, except the exception models are rather different.
synchronized statementNoNoJava has thread support built into the language.C and C++ do not
package and import statementsNoNoJava supports modularization into packages.C does not.C++ has namespaces to accomplish the same thing, but they are a very recent addition to the language and so are not used by many traditional C++ programmers.

Differences from C/C++

Here are some other ways that Java statements differ from C/C++ statements:

FeatureIn C?In C++?Comments
Local variable declarationsNoYesJava supports C++-style local variable declarations.
(C only supports local declarations are the start of a block.)
Forward referencesYesYesJava supports flexible forward references.It does not have the concept of declarations and definitions as in C/C++.
Method overloadingNoYesJava uses overloading similar to C++.Java does not support global functions.
The void return typeYesYesJava uses void for a return type, but does not support (void) casts
The void argument listYesOptionalJava does not use void to indicate no parameters
The void * typeYesYesJava has no pointer types
Old modifiersYesYesJava does not support long, short, signed, unsigned, or const as modifiers.
Java does support the volatile modifier.
New modifiersNoNoJava supports modifiers: finalnativesynchronized, and transient
No structs or unionsYesYesJava does not support structs or unions
Enumerations (enums)YesYesJava did not support enums prior to JDK 1.5 (Java 5). 
As of Java 5, it now does.
No method typesYesYesJava does not support function/method addresses
No bitfieldsYesYesJava does not support bitfields
Variable-length argument listsYesYesJava did not support variable-length argument lists.  However, in JDK 1.5 (Java 5), it does support variable-length argument lists.

The “foreach” Loop

Java 5.0 added a very useful, new version of a for loop:  a “foreach” loop, which is also present in a number of other languages.  Java calls this new version an “enhanced for loop”.

The “foreach” loop allows you to iterate through a collection of objects in a way that is much simpler than with a normal for loop.  

For the moment, we have only encountered one kind of collection: an array.  Later, we’ll see how the “foreach” loop may be used with other kinds of collections.

Pre-Java Version 5

First, let’s look at how we typically would have done this prior to Java 5.0:

package examples;

public class ForEachTest
{
  public static void main(String[] args)
  {
    String[] herbs = { "Basil", "Sage", "Rosemary", "Oregano" };
    for (int i = 0; i < herbs.length; i++)
    {
      String herb = herbs[i];
      System.out.println(herb);
    }
  }
}

which outputs:

Basil
Sage
Rosemary
Oregano

This form of a for loop uses a very standard Java idiom (it’s also a standard idiom for C/C++ and other related languages) for iterating through the elements of an array.  Note how picky you have to be with the termination condition:

i < herbs.length

“Off by one” errors (also known as “fencepost” errors) are very common, even for experienced Java programmers.

Post-Java Version 5

Now, let’s see how the “foreach” loop makes this much simpler (and safer!):

package examples;

public class ForEachTest
{
  public static void main(String[] args)
  {
    String[] herbs = { "Basil", "Sage", "Rosemary", "Oregano" };
    for (String herb: herbs)
    {
      System.out.println(herb);
    }
  }
}

which outputs exactly the same thing as the former example.

Notice how much simpler this version is!  Not only does it save several lines of code, it also eliminates the error-prone termination condition of the conventional for loop construct.

The compiler is doing more work for you!