Skip navigation

Lu's Notes

down to bottom of page

Java Conditionals

Warning: These notes have been collected from several different sources - each uses different terminology... they may even be contradictory...

Conditions... if,  while,  loops,  for,  switch,  case,  continue...
A single statement after a condition does not require surrounding curly braces {}. If two statements are made and the curly braces are forgotten, only the first statement will execute for the condition and the next will execute reguardless of the condition.

For standardization and simplicity I will always use curly braces, even if only one statement is made. Curly braces should align with/under the condition, the statements within are then indented.

 if  statements:
if  ( booleanExpression )
{
    one or more statements
}
if  ( booleanExpression )
{
    one or more statements
}
else
{
    one or more statements
}
if  ( booleanExpression )
{
    one or more statements
}
else if  ( booleanExpression )
{
    one or more statements
}
else
{
    one or more statements
}

A boolean expression; is something that evaluates to true or false.
    The boolean expression is always surrounded by parentheses.
A condition is a boolean expression.
    The condition is always surrounded by parentheses.
A condition can be very complicated, with relational operators and logical operators.
A statement is an expression followed by a semicolon, basically to "do something..."
    However it can be (and usually is) a block statement containing several statements.
The statement(s) is sometimes called the loop body.

An expression is a combination of variables, literals and operators.
(In Java method calls are also expressions.)
Expressions can be grouped in parentheses, so you don't have to worry about operator precedence.
(In Java method calls are also expressions.)

 while  statements:   (loops)
while ( condition )
{
    one or more statements;
}
while ( condition )
{
    loop body   // a statement or block statement
}
Non-conditional Statement after the loop
do
{
    one or more statements;
} while ( condition );

Three parts of a loop must be coordinated in order for the loop to work properly:
  1. The initial values must be set up correctly.                for example:   int  count = 1;
  2. The condition in the while loop must be correct.     for example:  while ( count <= 3 )
  3. The change in variable(s) must be set up correctly.   for example:  count = count + 1  or  count++
In a counting loop the variable  count  is  initialized, tested, and changed.
"For" loops do this, see below...

 for loops
for ( initialize loop's variable(s); test variable(s) condition; increment variable(s) value )
{
    one or more statements;
}
for ( i = 0; i < 5; i++ )
{
    one or more statements;
}
for ( i = 0, j = 0; i < 5 && j < 10; i++, j += 2 )
{
    one or more statements;
}

Use a switch statement when you are testing the same expression for several possible values.
Each possible value is placed in a case label. A switch statement compares any integer (byte,char,short,int or long) expression against a set of specific values (not a string!)

 switch and case statements
int choice = 1;      // or some other single int

switch ( choice )
{
    case 1 :
        one or more statements;
        break;
    case 2 :
        one or more statements;
        break;
    case 3 :
        one or more statements;
        break;
    default :                // optional, works like an else
        one or more statements;
        break;
}
int choice = 'a';      // or some other single char

switch ( choice )
{
    case 'a' :
    case 'A' :
        one or more statements;
        break;
    case 'b' :
    case 'B' :
        one or more statements;
        break;
    case 'c' :
    case 'C' :
        one or more statements;
        break;
    default :                // optional, works like an else
        one or more statements;
        break;
}

 continue statements
for ( i = 0; i < 10; i++ )
{
    if (i = = 7 || i = = 8)
    {
        one or more statements;
        continue;               // this continue works the same as the else below
    }
    one or more statements;
}
for ( i = 0; i < 10; i++ )
{
    if (i = = 7 || i = = 8)
    {
        one or more statements;
    }
    else                        // the continue above, works the same as this else
    {
        one or more statements;
    }
}

 try-catch  statements:   (for exceptions and errors)
try
{
    one or more statements;
}
catch ( SomeException exceptionName )   // may have several catch(s)
{
    one or more statements;
}
try
{
    one or more statements;
}
catch ( SomeException exceptionName )   // may have several catch(s)
{
    one or more statements;
}
finally       // "always" executes
{
    one or more statements;
}

A counting loop uses a loop control variable to count upwards or downwards (usually by an integer increment.) Counting loops are by far the most common type of loop. A counting loop increments a counter (loop control variable) after each input line is read. To control a counting loop, a special value may be assigned (a "sentinel") that is used to determine whether to execute or exit a loop. In a sentinel controlled loop the user may enter "some predetermined value" to tell the program that the iterations (loops) are complete. The special sentinel value must not be a value that could ever occur in the data. A sentinel controlled loop reads in lines until reaching a line that contains a special value (the "sentinel"). Another type of loop is the result-controlled loop also known as free loop or general loop. It keeps looping until the computation has reached a particular goal. It is like the instruction in a cookie recipe that says "keep stirring until the ingredients are thoroughly blended." You know when to quit only when the desired result has been achieved. A result-controlled loop reads in lines until a desired result has been achieved.

See Java Links for more info, Object & Classes , Data Types , Operators , Conditionals , Definitions , Keywords , File Structure , Code Examples , Class Skeleton , Java Notes

up to top of page   Return to Top of Page   up to top of page up to top of page