Java Control Statements and Operators
Classified in Computers
Written at on English with a size of 5.46 KB.
◦ Performs an action if a condition is true and performs a different action if the condition is false.
◦ Double-selection statement—selects between two different actions (or groups of actions).
}switch statement
◦ Performs one of several actions, based on the value of an expression.
◦ Multiple-selection statement—selects among many different actions (or groups of actions).
Iteration Statements in Java
}Four iteration statements (also called iteration statements or looping statements)
◦ Perform statements repeatedly while a loop-continuation condition remains true.
}
while and for statements perform the action(s) in their bodies zero or more times
◦ if the loop-continuation condition is initially false, the body will not execute.
}The do…while statement performs the action(s) in its body one or more times.
}Chapter 7 presents the enhanced for statement.
}if, else, switch, while, do and for are keywords.
Dangling-else Problem
} Throughout the text, we always enclose control statement bodies in braces ({ and }). This avoids a logic error called the “dangling-else” problem.
Blocks
}The if statement normally expects only one statement in its body.
}To include several statements in the body of an if (or the body of an else for an if…else statement), enclose the statements in braces.
}Statements contained in a pair of braces (such as the body of a method) form a block.
}
A block can be placed anywhere in a method that a single statement can be placed.
}Example: A block in the else part of an if…else statement:
if (grade >= 60) {
System.out.println("Passed");
}
else {
System.out.println("Failed");
System.out.println("You must take this course again."); }
Conditional operator (?:)
}Conditional operator (?:)—shorthand if…else.
}Ternary operator (takes three operands)
}Operands and ?: form a conditional expression
}
Operand to the left of the ? is a boolean expression—evaluates to a boolean value (true or false)
}Second operand (between the ? and :) is the value if the boolean expression is true
}Third operand (to the right of the :) is the value if the boolean expression evaluates to false.
}Variables declared in a method body are local variables and can be used only from the line of their declaration to the closing right brace of the method declaration.
}A local variable’s declaration must appear before the variable is used in that method.
}A local variable cannot be accessed outside the method in which it’s declared.
Develop a class-averaging program that processes grades for an arbitrary number of students each time it is run.
}Sentinel-controlled iteration is often called indefinite iteration because the number of iterations is not known before the loop begins executing.
}
A special value called a sentinel value (also called a signal value, a dummy value or a flag value) can be used to indicate “end of data entry.”
}A sentinel value must be chosen that cannot be confused with an acceptable input value.
}Integer division yields an integer result.
}To perform a floating-point calculation with integers, temporarily treat these values as floating-point numbers for use in the calculation.
}The unary cast operator (double) creates a temporary floating-point copy of its operand.
}
Cast operator performs explicit conversion (or type cast).
}The value stored in the operand is unchanged.
}Java evaluates only arithmetic expressions in which the operands’ types are identical.
}Promotion (or implicit conversion) performed on operands.
}In an expression containing values of the types int and double, the int values are promoted to double values for use in the expression.
}Compound assignment operators abbreviate assignment expressions.
}Example:
c = c + 3;
can be written with the addition compound assignment operator, +=, as c += 3;
}
The += operator adds the value of the expression on its right to the value of the variable on its left and stores the result in the variable on the left of the operator.
}Statements like variable = variable operator expression;
where operator is one of the binary operators +, -, *, / or % can be written in the form
variable operator= expression;