News Ticker

Unusual for and if-then-else constructions that ARE NOT valid.

Unusual loop/conditional constructions that ARE NOT valid.

[sourcecode language=”java” toolbar=”false”]
for (int x = 0; x<5; x++)
System.out.println(x);
System.out.println(x); // out of scope error
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
for (int x = 0; x<5; x++)
for (int y = 0; y<5; y++)
for (int z = 0; z<5; z++)
[/sourcecode]

There must be a line of code that is executed after the last for statment.

[sourcecode language=”java” toolbar=”false”]
for (int x = 0; x<5; x++)
for (int y = 0; y<5; y++)
for (int z = 0; z<5; z++)
System.out.println(z);
System.out.println(y); // out of scope
System.out.println(x); // out of scope
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
for (int i = 0; i < 3; i++)
System.out.println("i = " +i);
for (int j = 0; j < 3; j++)
System.out.println("j = " + j);
System.out.println("j = " + j); // out of scope
for (int k = 0; k < 3; k++)

System.out.println("k = " + k);
System.out.println(k); // out of scope
[/sourcecode]

In nested loops without braces there must be one line of code and optionally a for statement. If there are two or more lines of code then we have compile time problems.

[sourcecode language=”java” toolbar=”false”]
if (true) { break ; }
[/sourcecode]

Cannot have break or continue in an ‘if’ or ‘else’ block.

[sourcecode language=”java” toolbar=”false”]
if();
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
if (true ) else; // Else is not a valid statement
[/sourcecode]

Leave a Reply

%d bloggers like this: