Hot Posts

if-then-else statements

The if condition must evaluate to a boolean. If it evaluates to a primitive type or String it will error.

Unusual loop/conditional constructions that ARE valid.

             for (int x = 0; x<5; x++)
                   if (true )
            System. out .println(x);

Exercise: What is the output of these code snippets:

if (8 == 81) {}
if (true) {}
if (bool = false) {}  //assume that bool is declared as a boolean
if (x == 10 ? true:false) { } // assume that x is an int

Unusual loop/conditional constructions that ARE NOT valid.

if (true) { break; }

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

if (x = 3) {} // assume that x is an int

Because the expression x = 3 does not return a boolean.

The ternary statement

result = testCondition ? value1 : value2
                           ^         ^
                         true      false

The ternary statement cannot have a void operator as the second and third:

System.out.println( i<20 ? out1() : out2() );

Assume that out1 and out2 have method signature:

  • public void out1(); and
  • public void out2();

This code snippet will not compile.

Type of the operation is ‘highest’ type of the second and third operands.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: