Ads 468x60px

Monday, July 25, 2011

Flow of Execution | Loop | Arrays


 Now that you understand the basic structure and elements of the Java language, you'll see how statements flow through a Java program in this section. By default, the Java interpreter executes statements in sequential order. This section introduces you to some more complex types of statements that alter this flow of execution.

Conditionals

if, if...else, ?:, and switch are four conditional statements that are used often in code. They evaluate an expression and, based on the value of the expression, they control which statements are executed.
The if statements consist of if followed by a test expression. If the test expression returns a true value, the statement or block of statements after the expression is executed. They are structured as follows:
if (test expression) statement;
if...else statements are similar to if statements, but include a false statement that is executed if the test expression returns a false value. They are structured as follows:
if (test expression) true-statement;
else false-statement;
The ?: is a ternary operator that allows you to assign one of two values based on the evaluation of an expression as true or as false. Its form is as follows:
(test expression) ? true-value : false-value
The switch statement evaluates an expression's value and jumps to a statement identified by the literal value of the expression. It saves coding time if there are several values in your statement. Its form is as follows:
switch (expression) {
     case value: statements
     case value: statements
     default: statements
}

Loops

Loops provide you with the ability to repeat certain statements until a condition is met. Java provides three types of loop statements: while, do...while, and for. Three other statements commonly used with loops are break, continue, and labels, but these can be used with other types of Java statements (such as conditionals) as necessary.
The while loop evaluates a condition to see if it is true, and then executes statements and checks the condition again. If the condition is still true, while executes the statements and checks again. This process continues until the condition is evaluated as false. The form is this:
while (expression) true-statements
The do...while loop is similar to while, but do...while executes statements before the condition is evaluated. When you use do...while, loop statements are always executed at least once. The form is this:
do statements while (expression)
The for loop allows you to test a range of values in your expression. Its form is generally this:
for (initializer; expression; increment)
statements
The break statement is used to exit loop and conditional statements before meeting a test condition. The continue statement directs execution back to the beginning of a loop without completing all of the loop statements. A label is used to identify a statement so execution can be directed to it with a break or continue statement. A colon (:) must be appended to a label name.
An example of a label is as follows:
variable declarations;
Label:
     while (test expression) {
statement;
if(test expression)  {
continue Label;
}
statement;
     }

Arrays

Arrays create slots that allow you to store a list of variables. Arrays are allocated using the new operator to create the array and assigning it to a variable with =. Arrays are declared as follows:
datatype variable[] = new datatype[number-of-slots];
The first half of the statement declares the variable that holds the array. Your array declarations should end in square brackets. The second half creates the array and assigns it to the variable.
The new modifier automatically initializes your array to false for Boolean arrays, 0 for numeric arrays, \0 for character arrays, and null for all other types of arrays. You can choose to initialize the array on the same line as the declaration, as shown here:
datatype [] = {element1, element2, element3, etc.}


0 comments:

Post a Comment