Branching Statements-Simple if, else and switch Statement

0




Branching Statements

Conditional statements enable the selective execution of portions of the program according to the value of some expressions. The Java programming language supports the if and switch statements for two-way and multiple-way branching, respectively.

Simple if, else Statements

The basic syntax for an if statement is:

if ( <boolean_expression> )

<statement_or_block>

For example:

if ( x < 10 )

System.out.println(“Are you finished yet?”);

However, it is recommended that you place all then statements into a block. For example:

if ( x < 10 ) {

System.out.println(“Are you finished yet?”);

}

Complex if, else Statements

If you require an else clause, then you must use the if-else statement:

if ( <boolean_expression> )

<statement_or_block>

else

<statement_or_block>

For example:

if ( x < 10 ) {

System.out.println(“Are you finished yet?”);

} else {

System.out.println(“Keep working…”);
}

If you require a series of conditional checks, then you can chain a

sequence of if-else-if statements:

if ( <boolean_expression> )

<statement_or_block>

else if ( <boolean_expression> )

<statement_or_block>

For example:

int count = getCount(); // a method defined in the class

if (count < 0) {

System.out.println(“Error: count value is negative.”);

} else if (count > getMaxCount()) {

System.out.println(“Error: count value is too big.”);

} else {

System.out.println(“There will be ” + count +

” people for lunch today.”);

}

The Java programming language differs from C and C++ because an if() statement takes a boolean expression, not a numeric value. You cannot convert or cast boolean types and numeric types. If you have:

if (x) // x is int

use:

if (x != 0)

The entire else part of the statement is optional, and you can omit it if no action is to be taken when the tested condition is false.

The switch Statement

The switch statement syntax is:

switch ( <expression> ) {

case <constant1>:

<statement_or_block>*

[break;]

case <constant2>:

<statement_or_block>*

[break;]

default:

<statement_or_block>*

[break;]

}

In the switch (<expression>) statement, <expression> must be expression-compatible with an int type. Promotion occurs with byte, short, or char types. Floating point, long expressions, or object references (including Strings) are not permitted.

Note – An enumerated type value may also be used in the <expression> and <constantN> elements. The optional default label specifies the code segment to be executed when the value of the variable or expression cannot match any of the case values. If there is no break statement as the last statement in the code segment for a certain case, the execution continues into the code segment for the next case without checking the case expression’s value. A return statement can be used instead of a break statement. Moreover, if the switch is in a loop, then a continue statement would also cause execution to exit out of the switch construct.

Note – Nine out of ten switch statements required breaks in each case block. Forgetting the break statement causes the most programming errors when using switch statements.

An example switch statement is shown in Code .

c1

Code configures a car object based on the carModel. If carModel is the integer constant DELUXE, then air conditioning is added to the car, as is a radio, and, of course, wheels and an engine. However, if the carModel is only a STANDARD, then only a radio, wheels, and an engine are added. Finally, by default, any car model will have wheels and an engine added.

A second example switch statement is shown in Code.

c2

Code solves the redundant method calls in the previous example by permitting the flow of control to descend through multiple case blocks. For example, if the carModel is DELUXE, then the addAirConditioning method is called, and then the flow of control falls through the next case statement and calls the addRadio method, and finally the flow of control falls through the default statement and calls the addWheels and addEngine methods.