Controlling the Flow

Operators
The Logical Operators:
A statement is executed is determined by a combination of several conditions.You can use logical operators to combine these conditions. Logical operators are known as Boolean operators or bitwise logical operators. Boolean operator operates on boolean values to create a new boolean value.The bitwise logical operators are “&”, “|”, “^”, and “~” or “!”. The following table shows the outcome ofeach operation.
Assume Boolean variables A holds true and variable B holds false, then:
a b   a&b a|b a^B ~a or !a
TRUE(1) TRUE(1) TRUE TRUE FALSE FALSE
TRUE(1) FALSE(0) FALSE TRUE TRUE FALSE
FALSE(0) TRUE(1) FALSE TRUE TRUE TRUE
FALSE(0) FALSE(0) FALSE FALSE FALSE TRUE

* The NOT Operator: -
Also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of its operand. If applied on integer operand it will reverse all bits similarly if applied to boolean literal it will reverse it.
* The AND Operator: -
The AND operator “&” produces a 1 bit if both operands are 1 otherwise 0 bit. Similarly for boolean operands it will result in true if both operands are true else result will be false.
* The OR Operator: -
The OR operator “|” produces a 0 bit if both operands are 0 otherwise 1 bit. Similarly for boolean operands it will result in false if both operands are false else result will be true.
* The XOR (exclusive OR) Operator : -
The XOR operator “^” produces a 0 bit if both operands are same (either both 0 or both 1) otherwise 1 bit. Similarly for boolean operands it will result in false if both operands are same (either both are false or both true) else result will be true.
Example: -
package operatorsdemo;
public class BitwiseLogicalOpDemo {
public static void main(String[] args) {
//Integer bitwise logical operator
int a = 65; // binary representation 1000001
int b = 33; // binary representation 0100001
System.out.println("a & b= " + (a & b));
System.out.println("a | b= " + (a | b));
System.out.println("a ^ b= " + (a ^ b));
System.out.println("~a= " + ~a);
//boolean logical operator
boolean bool1 = true;
boolean bool2 = true;
boolean bool3 = false;
System.out.println("bool1 & bool2= " + (bool1 & bool2));
System.out.println("bool2 & bool3= " + (bool2 | bool3));
System.out.println("bool2 | bool3= " + (bool2 | bool3));
System.out.println("bool1 ^ bool2= " + (bool1 ^ bool2));
System.out.println("!bool1= " + !bool1);
}
}
a & b = 1
a | b = 97
a ^ b = 96
~a = -66
bool1 & bool2 = true
bool2 & bool3 = true
bool2 | bool3 = true
bool1 ^ bool2 = false
!bool1=false
The Assignment Operators:
Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the '= 'to the variable on the left. Below statement 1 assigning value 10 to variable x and statement 2 is creating String object called name and assigning value "Anuj" to it.
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Example: -
public class Test {
public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println("c = a + b = " + c );
c += a ;
System.out.println("c += a = " + c );
c -= a ;
System.out.println("c -= a = " + c );
c *= a ;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
System.out.println("c /= a = " + c );
a = 10;
c = 15;
c %= a ;
System.out.println("c %= a = " + c );
c <<= 2 ;
System.out.println("c <<= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= a = " + c );
c &= a ;
System.out.println("c &= 2 = " + c );
c ^= a ;
System.out.println("c ^= a = " + c );
c |= a ;
System.out.println("c |= a = " + c );
}
}
Output: -
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a = 0
c ^= a = 10
c |= a = 10
Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
Example: -
public class Test {
public static void main(String args[]){
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Value of b is : 30
Value of b is : 20
Auto increment and decrement
The increment and decrement operators (often referred to as the auto-increment and auto-decrement operators). The decrement operator is -- and means “decrease by one unit.” The increment operator is ++ and means “increase by one unit.” If a is an int, for example, the expression ++a is equivalent to (a = a + 1). Increment and decrement operators not only modify the variable, but also produce the value of the variable as a result.
There are two versions of each type of operator, often called the prefix and postfix versions. Pre-increment means the ++ operator appears before the variable or expression, and post-increment means the ++ operator appears after the variable or expression. Similarly, pre-decrement means the -- operator appears before the variable or expression, and post-decrement means the -- operator appears after the variable or expression. For pre-increment and pre-decrement, (i.e., ++a or --a), the operation is performed and the value is produced. For post-increment and post-decrement (i.e. a++ or a--), the value is produced, then the operation is performed. As an example:
public class AutoInc {
public static void main(String[] args) {
int a = 5;
System.out.println("Original value of a : " + a);
System.out.println();
System.out.println("After using pre-increment value of a : " + ++a);
System.out.println("After using post-increment value of a : " + a++);
System.out.println();
System.out.println("After completing post-increment operation value of a : " + a);
System.out.println();
System.out.println("After using pre-decrement value of a : " + --a);
System.out.println("After using pre-decrement value of a : " + a--);
System.out.println();
System.out.println("After completing pre-decrement operation value of a : " + a);
}
}
Output: -
Original value of a : 5
After using pre-increment value of a : 6
After using post-increment value of a : 6
After completing post-increment operation value of a : 7
After using pre-decrement value of a : 6
After using pre-decrement value of a : 6
After completing pre-decrement operation value of a : 5


Free Web Hosting