(2) Operators and statements

¡Supera tus tareas y exámenes ahora con Quizwiz!

If-then-else statement.

Curly braces are optional for single statements, but required for multiple. If... if... if checks all statements, even if one of the first ones is true. If... else if... else stops checking as soon as one condition is met.

While statement.

Curly braces are optional for single statements, required for blocks of multiple statements.

Logical operators.

& is only true if both operands are true | is only false if both operands are false ^ is only true if the operands are different The short-circuit operators consist of &&, || and ^^. The only difference with the logical operators is that the right-hand side of the expression may never be evaluated if the result can be determined by the left-hand side of the expression.

Unary operators.

A unary operator requires exactly one operand to function: + (indicate that a number is positive) - (the number is negative, OR negates and expression) ++ (increment by one) -- (decrement by one) ! (inverses boolean) Know the difference between pre- and post-increment operators. ++expression updates first and then outputs the expression, whileas expression++ first returns and THEN updates. This becomes really important when you have multiple operators on the same line.

Equality operators.

The equality operators (== and !=) are used to: - compare two numeric primitive types (there's automatic promotion when there're different data types) - compare two boolean values - compare two objects of the same type, including null and String (this means: REFERENCING to the same object)

Compound assignment operators.

These are really just glorified forms of the simple assignment operator, with a built-in arithmetic or logical operation that applies the left- and right hand sides of the statement and stores the resulting value into the left-hand side. The target variable must already have been initialized! int x = 2, z = 3; x *= z; In compound assignment operators the compiler will automatically cast to the target data type. This way you could save an explicit down cast when you're playing around with short's.

Arithmetic operators.

These operators are often encountered in early mathematics and include +, -, *, / and %. They also include unary operators ++ and --. They follow the strict operator precedence rules unless encapsulated in parentheses. They may only applied to primitives, although + and += may also be applied to String. Dividing (/) only results in the lowest whole number.

Assignment operators.

This is a binary operator, meaning it uses two operands. The simplest assignment operator is =, which assigns one value with the other. Casting of a primitive is needed every time you try to go from a larger numerical data type to a smaller numerical data type, or when converting from a floating point number to an integral value. Remember: the result of an assignment is the value of that assignment! So this is legal: long x = 5; long y = (x=3);

Do-while loop.

This is basically the same as a while-loop, but with one big difference: the statement or block inside the loop is guaranteed to run at least once. Curly braces are optional for single statements, required for blocks of multiple statements. do { // body } while (boolean);

Ternary statement.

This is the only operator that takes three operands. It basically works the same as an if-then-else statement. int x = boolean ? expression : expression; Make sure that the outcomes of both expressions match the target's data type, or the compiler will throw an error. Only one of the right-hand expressions will be evaluated at runtime. Be wary of this when variables are being manipulated in ternary operations on the exam.

For loop.

for (initialization; booleanExpression; updateStatement){} Each of those three components are optional, so it's really easy to write an infinite loop. The data types of the variables used in the initialization block must be the same. You can however use (and update) multiple variables. Curly braces are optional for single statements, required for blocks of multiple statements. The initialization and update sections may contain multiple statements, separated by commas.

For-each statement.

for(datatype instance : collection) {} Curly braces are optional for single statements, required for blocks of multiple statements. The right hand side must be a built in Java array or an object whose class implements java.lang.Iterable, which includes most of the Java Collections framework. The left side must include a declaration for an instance of a variable, whose type matches the type of a member of the array or collection on the right hand side.

Switch statement.

switch(variableToTest) { case constantExpression: return 1; break; case expressionTwo: return 2; break; default: return 3; } The parenthesis and curly braces are required. The default option (which can be placed anywhere in the switch) and break statements are optional. However, without break statements the code will just keep running along all other case statements in order once one condition is met!!! Data types supported by switch statements are: - int and Integer - byte and Byte - short and Short - char and Character - String - enum values Note that boolean and long are excluded! You can only use literals, enum constants or final constant variables of the same data type in the switch statement. Final constant means that the variable must be marked with the final midifier and that it's initialized with a literal value in the same expression in which it is declared.

Optional labels & breaks.

For-loops, switch statements and if-statements can have one-word optional labels at their heads. This is especially handy when you're using nested statements and want to use a break statement. OUTER_LOOP: for(int i = 1;.... INNER_LOOP: while(int x > 3).... break INNER_LOOP; or continue OUTER_LOOP; If you don't provide a break parameter the nearest inner loop is broken. Continue works more or less the same way as breaks with one big difference: while the break statement transfers control to the enclosing statement, the continue statement transfers control to the boolean expression that determines if the loop should continue. In other words, it ends the current iteration of the loop.

Overflow / underflow.

Overflow occurs when a number is so large that it will no longer fit within the data type, so the system "wraps around" to the next lowest value and counts up from there. Underflow is the other way around.

Order of operator precedence.

Post-unary operators (expression++, expression--) Pre-unary operators (++expression, --expression) Other unary (+, -, !) Multiplication/Division/Modulus (*, /, %) Addition/Substraction (+, -) Relational operators (<, >, <=, >=, instanceof) Equal to (==, !=) Logical operators (&, ^, |) Short-circuit logical (&&, ||) Ternary operators (boolean ? boolean : boolean) Assignment operators (=, +=, -=) When two operators have the same precedence Java reads them from left to right.

Relational operators.

Relational operators compare two expressions and return a boolean. If two numeric operands are not of the same data type, the smaller one is promoted.

Numeric promotion.

Remember these rules when applying operators to data types: 1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types. 2. If one of the values is integral and the other is floating point, Java will automatically promote the integral value to the floating point value's data type. 3. Smaller data types (byte, short, char) are FIRST promoted to int any time they're used with a Java binary arithmetic operator, even if neither of the operands is int. If you want to store the result into a smaller data type again after the operation , you need to cast explicitly. Unary operators (++, --) are excluded from this rule! 4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.


Conjuntos de estudio relacionados

Chapter 29: Communication, History, and Physical Assessment

View Set

Human Anatomy & Physiology KP Final exam

View Set

ECE-12 SPED 161 practice exam 1.1

View Set

Chapter 7: Production and Growth

View Set

Series 66: Portfolio / Fixed Income Basics (Portfolio Management Styles)

View Set