5.9. Review notes

if and if-else constructs:

  • The if statement enables you to execute a set of statements in your code based on the result of a condition, which should evaluate to a boolean or a Boolean value.
  • The multiple flavors of an if statement are if, if-else, and if-else-if-else.
  • The if construct doesn’t use the keyword then to define code to execute when an if condition evaluates to true. The then part of the if construct follows the if condition.
  • An if construct may or may not define its else part.
  • The else part of an if construct can’t exist without the definition of its then part.
  • It’s easy to get confused with the common if-else syntax used in other programming languages. The if-elsif and if-elseif statements aren’t used in Java to define if-else-if-else constructs. The correct keywords are if and else.
  • You can execute a single statement or a block of statements for corresponding true and false conditions. A pair of braces marks a block of statements: {}.
  • If an if construct doesn’t use {} to define a block of code to execute for its then or else part, only the first line of code is part of the if construct.
  • An assignment of a boolean variable can also be passed as an argument to the if construct. It’s valid because the resultant value is boolean, which is accepted by if constructs.
  • Theoretically, nested if and if-else constructs have no limits on their levels. When using nested if-else constructs, be careful about matching the else part with the right if part.

Ternary constructs:

  • You can use a ternary operator, ?:, to define a compact if-else construct to assign value to a variable depending on a boolean expression.
  • The parentheses enclosing a boolean expression are optional for better readability. The code will work without them.
  • You can assign a literal value or an expression to a variable using a ternary operator.
  • A method that returns a value can be used to initialize a variable in a ternary construct.
  • If the expression used to evaluate a ternary operator doesn’t return a boolean or Boolean value, the code won’t compile.
  • All three parts of a ternary operator are mandatory.
  • The value returned by a ternary operator must be assigned to a variable, or the code won’t compile.
  • Because a ternary operator must return values, which are assigned to a variable, it can’t include code blocks.
  • A method that doesn’t return a value can’t be used to initialize variables in a ternary construct.
  • The value returned by a ternary construct must be compatible with the variable type to which the value is being assigned.
  • Ternary operators can be nested to any level.

switch statements:

  • A switch statement is used to compare the value of a variable with multiple predefined values.
  • A switch statement accepts arguments of type char, byte, short, int, and String. It also accepts arguments of wrapper classes: Character, Byte, Short, Integer, and Enum.
  • A switch statement can be compared with multiple related if-else-if-else constructs.
  • You can pass an expression as an argument to a switch statement, as long as the type of the expression is one of the acceptable data types.
  • The case value should be a compile-time constant, assignable to the argument passed to the switch statement.
  • The case value can’t be the literal value null.
  • The case value can define expressions that use literal values; that is, they can be evaluated at compile time, as in 7+2.
  • One code block can be defined to execute for multiple case values in a switch statement.
  • A break statement is used to exit a switch construct once a matching case is found and the required code statements have executed.
  • In the absence of the break statement, control will fall through all the remaining case values in a switch statement until the first break statement is found, evaluating the code for the case statements in order.

for loops:

  • A traditional for loop is usually used to execute a set of statements a fixed number of times.
  • A for loop defines three types of statements separated by semicolons (;): initialization statements, termination condition, and update clause.
  • The definition of any of the three for statements—initialization statements, termination condition, and update clause—is optional. For example, for (;;); and for (;;){} are valid code for defining a for loop. Also, defining any one of these statements is also valid code.
  • An initialization block executes only once. A for loop can declare and initialize multiple variables in its initialization block, but the variables that it declares should be of the same type.
  • The termination condition is evaluated once, for each iteration, before the statements defined within the body of the loop are executed.
  • The for loop terminates when the termination condition evaluates to false.
  • The update block is usually used to increment or decrement the value of the variables that are defined in the initialization block. It can also execute multiple other statements, including method calls.
  • Nested for loops have no limits on levels.
  • Nested for loops are frequently used to work with multidimensional arrays.

Enhanced for loops:

  • The enhanced for loop is also called the for-each loop.
  • The enhanced for loop offers some benefits over the regular for loop, but it’s not as flexible as the regular for loop.
  • The enhanced for loop offers simple syntax to iterate through a collection of values—an array, ArrayList, or other classes from Java’s Collection framework that store a collection of values.
  • The enhanced for loop can’t be used to initialize an array and modify its elements.
  • The enhanced for loop can’t be used to delete the elements of a collection.
  • The enhanced for loop can’t be used to iterate over multiple collections or arrays in the same loop.
  • Nested enhanced for loops have no limits on levels.

while and do-while loops:

  • A while loop is used to keep executing a set of statements until the condition that it uses evaluates to false. This loop checks the condition before it starts the execution of the statement.
  • A do-while loop is used to keep executing a set of statements until the condition that it uses evaluates to false. This loop checks the condition after it completes the execution of all the statements in its loop body.
  • The levels of nested do-while or while loops have no limitations.
  • Both do-while and while loops can define either a single line of code or a code block to execute. The latter is defined by using curly braces {}.

Comparing loop constructs:

  • Both the do-while and while loops execute a set of statements until the termination condition evaluates to false. The only difference between these two statements is that the do-while loop executes the code at least once, even if the condition evaluates to false.
  • The regular for loop, though cumbersome to use, is much more powerful than the enhanced for loop.
  • The enhanced for loop can’t be used to initialize an array and modify its elements. The enhanced for loop can’t be used to delete or remove the elements of a collection.
  • The enhanced for loop can’t be used to iterate over multiple collections or arrays in the same loop.
  • You should try to use a for loop when you know the number of iterations—for example, iterating through a collection or an array, or executing a loop for a fixed number of times, say, to ping a server five times.
  • You should try to use a do-while or a while loop when you don’t know the number of iterations beforehand and the number of iterations depends on a condition being true—for example, accepting passport renewal applications until all applicants have been attended to.

Loop statements (break and continue):

  • The break statement is used to exit—or break out of—the for, for-each, do, and do-while loops and the switch construct.
  • The continue statement is used to skip the remaining steps in the current iteration and start with the next loop iteration. The continue statement works with the for, for-each, do, and do-while loops and the switch construct.
  • When you use the break statement with nested loops, it exits the corresponding loop.
  • When you use the continue statement with nested loops, it exits the current iteration of the corresponding loop.

Labeled statements:

  • You can add labels to a code block defined using braces, {}, all looping statements (for, enhanced for, while, do-while), conditional constructs (if and switch statements), expressions and assignments, return statements, try blocks, and throws statements.
  • You can’t add labels to declarations of variables.
  • You can use a labeled break statement to exit an outer loop.
  • You can use a labeled continue statement to skip the iteration of the outer loop.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.129.253