Java – Conditional Flow Control

If Statement

In certain solutions, it may be necessary to write code to perform various actions based on a user’s inout or certain criteria. To perform these actions, Java provides certain decision-making statements such as the if statement.

You may want to write a program to accept numbers from 0 through 9 from a user. If the number typed is from 0 through 9, it is to be displayed on the screen. Otherwise, a message stating that the typed number is invalid is to be displayed. To perform these actions, you use the if statement.

SYNTAX:

if (expression) {
if_body;
}

The if statement is used to enable the selective execution of statements in a program. The if statement has three forms: the simple if form, the if-else form.

The execution of the if statement involves the evaluation of the specified condition. The evaluated result of the condition is a boolean expression. If the condition evaluates to true, the if_body part of the if statement is executed.

If you want more than one statements to be executed in the if_body part of the if satement, enclose the statements within the braces. You should always use braces on conditional statements even when there is only a single statement. If you use multiple statements in the if_body part without braces, the intented output is not generated. The compiler treats the first statement as the if_body part. The remaining statements are not executed as a part of the if statement.

if (expression); {
statement;
}

Placing a semicolon on the if statement, terminates the statement. The line following the if statement will not be included in the if statement when this is done. Therefore, even if the condition is false, the statement after the if statement will still be executed.

If-else Construct

The simple form of the if statement is inefficient when actions are performed depeneding on whether the result of an expression is true or false. This is why two if statements are needed to handle the two states. In such situations, you use the if-else construct in the code.

SYNTAX:

if (expression) {
if_body;
} else {
else_body;
}

The if-else construct is another form of the if statement. This form can handle both the true and false values of an expression. The syntax of the if-else construct is displayed on the screen. If the expression in the syntax evaluates to true, the if_body part of the statement is executed, otherwise the else_body part is executed.

Fig: Flowdiagram


The flow diagram displayed above illustrates the flow of control in an if-else statement. If the expression evaluates to true, the control executes the if_body. If the expression evaluates to false, the control executes the else_body.

The if-else construct is more efficient than the simple if statement. In the if-else construct, only the if_body part or the else_body part of the code is executed at a time. Both parts are never executed together.

Within the if-else construct, you can use other if statements. The if statements contained within other statements are called nested statements. You can use nested if-else statements to perform actions based on multiple conditions at one time.

Using many nested if-else statement complicates nesting of if statements and can make code difficult to follow. In addition, you should always use braces even when only a single statement is contained in the if clause. This reduces bugs and makes code easier to read.

If-else-if Construct

To perform various actions based on multiple conditions at one time you can use the if-else-if construct. This construct is another form of the if statement.

SYNTAX:

if (expression1) {
body1;
} else if (expression2) {
body2;

….

} else {
bodyN;
}

The body1 and body2 parts of the if-else-if construct indicate the statements that will be executed if the respective conditions evaluate to true. If neither of the conditions evaluates to true, the bodyN part of the if-else-if construct will be executed.

Conditional Operator

The Java language provides an operator to handle situations, where different actions are performed, based on the evaluation of a condition. The conditional operator is used for this task. It is also called the ternary operator, and it is represented by the question mark sign (?) and the colon sign (:). This operator is an efficient alternative to a simple if-else construct.

SYNTAX:

expression1 ? expression2 : expression3;

The expression1 must be a boolean expression. If this condition is true, the expression to the right of the question mark, expression2 is evaluated. If the condition is false, the expression to the right of the colon, expression3 is evaluated.

EXAMPLE:

x=(a>b)?a:b;

Equivalent If-else Construct

if (a>b) {
x = a;
} else {
x = b;
}

switch statement

The switch statement is a conditional statement that branches to various alternative actions. You can use this statement to perform various actions based on the evaluation of a single expression. The switch statement can be used instead of the if-else-if construct if the condition evaluates to an integer value. This integer value must evaluate one of the 32-bit or smaller integer types, such as byte, char, short or int.

SYNTAX:

switch (expression) {
case value 1:
statement1;
break;

case value 2:
statement2;
break;

case value N:
statementN;
break;

default:
statement1;
break;
}

Advertisements

3 responses to “Java – Conditional Flow Control

  1. In what way the switch statement’s execution flows.
    Suppose I have 10 switch cases and the case10 is correct then, will switch identifies case10 directly or will it search from begining to end to findout for case10?

  2. The switch statement is quite efficient, probably more so than the equivalent if statements for many compilers. Switch allows the program to jump directly to the associated case statement without doing any kind of comparisons.

  3. Thanks for this information. I very quickly found exactly what I needed to know because of how well organized and structured your presentation is. Thanks again.

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