Author Archives: bhuvans

Java – Jump Statements

break statement

In several instances, such as an error or incorrect input from the user, it may be necessary for the flow of control to exit from a loop statement. To accomplish this, Java provides a jump statement called break statement.

The break statement forces the control to come out from a switch statement, a while loop, a do-while loop or a for loop. After the control moves out of a switch statement or any loop statement, it moves to the statement after the loop or switch statement.

SYNTAX:

break;

The break statement is written as break; without any expression.

Example

Consider a program that prints the value stored in a variable until the specified condition is false. The program ends when the value becomes equal to the value stored in another variable.

'break' statement example

The break statement has been used to enable the program to end when the value in the i variable becoes equal to the value in the num variable. If the value in the i variable is less than 10, the flow of control enters the for loop. When the code is executed, the program displays the numbers from 0 to 3.

Labelled Brake statement

In a nested loop, the break statement used in an inner loop transfers the flow of control to the immediate outer loop. In certain situations, you may also want the flow of control to exit the outer loop. In this case, you use the break statement with the label identifier.

SYNTAX:

break label_name;

The label identifier is used to group a loop statement that contains another loop statement or a switch statement and to specify the name for the group. The label identifier is specified just before a loop statement without any expression like ‘label_name: ‘.

Example

label1:

For example, to name the loop statement label1, you write label1: in the line preceding the loop statement.

When the brake statement is used with the label_name, the flow of control is transferred to the statement after the labeled loop.

Labelled Brake Statement - Example

Consider the code displayed above. that displays the value stored in variable ‘i’ until the condition specified in either of the for loops becomes false. The program terminates if the value of ‘i’ becomes equal to that of ‘num’. In the code shown above, a label statement, ‘label1:’ has been used to group the two for loops.

When the value of the ‘i’ variable becomes equal to the value of the ‘num’ variable, the program executes the labeled brake statement. This transfers the flow of control outside label1. Therefore, the print statement outside the nested for statement is not executed.

HINT: You cannot use the break statement without a switch statement or a loop statement.

Advertisements

Java – Iterative Flow Control

While Statement

Java provides certain loop statements, which you can use to perform an action repeatedly. These statements are called Iterative Statements. One of the loop statements is the while statement.

SYNTAX:

while (condition) {
loopbody
}

The ‘while’ statement contains a condition and a loopbody. The condition is enclosed within parentheses. All the variables used in the condition of the while statement must be initialized before the while loop. The values of the variables used in the condition must be changed in the loopbody. Otherwise, the condition may always remain true and the loop may never end. You change the values of the variables by performing various operations on the variables. You can write a single statement or multiple statements in the loopbody. Multiple statements should be enclosed within braces. Otherwise, the code may generate an unexpected output.

How ‘While’ Works

How While Statement works

Before the loopbody is executed, the condition of the while statement is evaluated. The loopbody is executed only when the condition evaluates to true. Therefore, the while loop is also called a top tested loop. After the loopbody is executed, the while condition is evaluated again. The sequence of the evaluating the condition and executing the loopbody continues until the while condition becomes false.

Example

Lets see a example program that stores two numbers in the variables [Lets say num and n]. The output should display the numbers from 0 to the number stored in the num variable.

In the code displayed below, the while statement specifies the condition that the value of n should be less than or equal to the value of num. Line 1 and Line 5 contain the opening and closing braces of the while loop. In Line 3, the value of n is printed on the screen. In Line 4, the value of n is incremented by 1.

When the program is executed, it displays the numbers from 0 to 5.

In Line 1, the condition is that the value of n should be less than or equal to the value of the variable num. The value of the variable num is 5. The initial value of n is 0, which is less than the value of num. Therefore, the condition is true and the flow of control moves inside the while loop. Inside the loop, the value of the variable n is displayed in the first row. This value is 0. Next, the flow control executes Line 4. As a result, the value of the variable n becomes 1. The flow of control moves back to Line 1, where the value of the variable n is again compared with the value of num. The value of the variable n is 1. Therefore, it is still less than the value of num, which is 5. The condition in Line 1 evaluates again to true, and the flow of control again moves inside the while loop. The value of the variable n is printed in Line 3. This looping continues until the value of n becomes 6. The flow of control moves out of the while loop, and the program terminates.

Infinite While Loops

If a semicolon is added after the condition in the while statement in a program, the program may result in an infinite loop. This is because the control never comes out of the while statement.

For Example:

If we modify the Line 1 of example program like this

while (n <= num); {

the program will result in an infinite loop.

do-while statement

The do-while statement is a loop statement similar to the while statement provided by Java. It is used to perform certain repetitive actions depending on a condition.

SYNTAX:

do {
loopbody
} while (condition);

The do-while statement begins with the ‘do’ keyword followed by the loopbody. In the loopbody, you can write a single statement or multiple statements enclosed within braces. The while statement is written after the closing brace. The while statement contains the condition enclosed in parentheses. In the do-while statement, you add a semicolon after the condition. The semicolon indicates the end of the do-while loop.

How ‘do-while’ Works

The major difference between the do-while statement and the while statement is that in the while statement, the loopbody is executed only when the condition stated in the statement is true. In contrast, the do-while statement in the loopbody is executed at least once, regardless of the condition evaluating to true or false.

'do-while' flow

After the loopbody is executed once, the condition in the do-while statement is checked. If the condition evaluates to true, the loopbody is executed until the condition becomes false. Therefore do-while loop is also called a bottom-tested loop.

You can use the do-while loop in situations where an action must be performed at least once without evaluating the condition.

Example

Consider a program that stores a number in a variable. The program displays all the numbers in descending order starting from the number stored in the variable until it encounters the number 0.

do-while example

The program shown above displays the do keyword and further decrements the value of the count variable that is added. After the count variable is decremented, we specify the condition to check whether the value is equal to 0 like this [while (count != 0);]. The closing brace of the main function and the class declaration is added.

Upon compilation and execution, the output displays all the number from 1 to 4 in descending order.

For Statement

Java provides the for statement to manage iterative actions as an efficient alternative to the while statement. The for statement creates a loop in which a set of statements is repeatedly executed until the specified condition becomes false.

SYNTAX:

for (initialization expressions; test condition; update expressions) {
loopbody;
}

The for statement starts with the ‘for’ keyword. This statement has three elements enclosed within parentheses. The first element of the for statement consists of initialization expressions. This element is an assignment statement. The assignment is done only once before the for statement begins execution. The second element, test condition, is evaluated before each iteration of the for statement. This element determines whether the execution of the for loop should continue or terminate. The third element in the for statement consists of the update expressions. These expressions change the values of the variables used in the test condition. Update expressions are executed at the end of each iteration after the loopbody is executed. The elements of a for statement are seperated by semicolons. You can use multiple statements in the initialization and update expressions of the for statement by seperating them with the comma operator. The multiple statements are evaluated from left to right.

EXAMPLE: Multiple initialization and update expression

for (x = 1, y = 2; test condition; x++, y++) {
loopbody;
}

Another Form

In the for statement, you can omit the initialization expressions and update expressions elements but you must provide two semicolons in the parentheses.

If you are not including the initialization and update expressions elements in the for statement, you must specify the initialization expressions element before the for statement. The update expressions element must then be specified inside the for loop.

SYNTAX:

initialization expressions;

for (; test condition;) {
update expressions;
loopbody;
}

Infinite For Loop

The test condition must be provided in the for statement. If you omit the test condition from the for statement, the program results in an infinite loop when executed. The program will also result in an infinite loop if none of the expressions is specified in the for loop.

SYNTAX:


for (initialization expressions; ; update expressions) {
loopbody;
}

for (;;) {
loopbody;
}

How ‘For’ Works

for (j = 2; j <= 10; j++) {
System.out.println(j);
}

How 'For' Works

In the for loop, the variable j is initialized to 2. Next, the value of j is compared with 10. If the test condition is true, the value of j is displayed on the screen. Next, the value of j is incremented by one. After the value of j is incremented, the test condition is evaluated again. If the test condition evaluates to true, the print function is executed again. If the test condition evaluates to false, the flow of control comes out of the for loop. The for statement creates a loop that is identical in function to the while statement. For loops are more useful when you have a fixed number of iterations and you need a counter. The for loop is also more compact. You can use the for statement to reduce the size of code.

HowTo: Personal Folders in Outlook Express

Outlook Personal Folders can be used to provide additional storage for emails, in order to free up space in your Exchange mailbox on the server.

Remember that the Personal Folders file is stored on your hard disk, so it is your responsibility to back it up regularly, in the same way that you would back up any other file on your computer. They can get very large, so you will probably need to backup to CD.

Installing the Java Development Kit

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;
}