0% found this document useful (0 votes)
35 views24 pages

3-Operators and Control Statment

Operators and control statements in Java allow programmers to manipulate data and control program flow. The main operators include arithmetic, relational, logical, and bitwise operators. Control statements include if-else, switch-case, loops (while, do-while, for), break, continue, and return. These statements allow conditional execution of code and repetition of tasks through loops. Proper use of operators and control statements is fundamental to writing efficient Java programs.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

3-Operators and Control Statment

Operators and control statements in Java allow programmers to manipulate data and control program flow. The main operators include arithmetic, relational, logical, and bitwise operators. Control statements include if-else, switch-case, loops (while, do-while, for), break, continue, and return. These statements allow conditional execution of code and repetition of tasks through loops. Proper use of operators and control statements is fundamental to writing efficient Java programs.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Operator and Control statements

Operators in JAVA
• Arithmetic Operators
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment (pre and post)
+= Addition assignment (a+=b ==> a=a+b )
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement (pre and post)
• Arithmetic operators cannot use on Boolean types,
but you can use them on char types, since the char
type in Java is, essentially, a subset of int.
• The modulus operator, %, returns the remainder of a
division operation. It can be applied to floating-point
types as well as integer types. (This differs from C/C+
+, in which the % can only be applied to integer
types.)
y = ++x;  x=x+1 and y=x;
y = x++;  y=x and x=x+1;
• Bitwise Operators : can be applied to the integer types, long, int,
short, char, and byte. These operators act upon the individual
bits of their operands.

~ Bitwise unary NOT & Bitwise AND


| Bitwise OR ^ Bitwise exclusive OR
>> Shift right >>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
• Java uses two’s complement to represent integers.(all signed
number except char in JAVA)
• The left shift operator, <<, shifts all of the bits in a value to the left a
specified number of times. value << num
• The right shift operator, >>, shifts all of the bits in a value to the
right a specified number of times. value >> num
• When we are shifting right, the top (leftmost) bits exposed by the
right shift are filled in with the previous contents of the top bit. This
is called sign extension and serves to preserve the sign of negative
numbers when you shift them right. For example, –8 >> 1 is –4,
which, in binary, is
11111000 –8 0001000 8 11111111 -1
>>1 >>1 >>1/2/3….
11111100 –4 0000100 4 11111111 -1
• The >> operator automatically fills the high-order bit with
its previous contents each time a shift occurs. This
preserves the sign of the value.
• The Java’s unsigned, shift-right operator, >>>, always shifts
zeros into the high-order bit. This is known as an unsigned
shift.
int a = -1;
a = a >>> 24;
11111111 11111111 11111111 11111111 –1 in binary
>>>24
00000000 00000000 00000000 11111111 255 in binary
• Relational Operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
• The result produced by a relational operator is a
Boolean value.
• Boolean Logical Operators
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
• Java includes a special ternary (three-way)
operator that can replace certain types of if-then-
else statements.
expression1 ? expression2 : expression3
max = a>b ? a:b;
if(a>b)
max=a;
else
max=b;
Operator precedence
() [] . Highest
++ –– ~ !
* / %
+ –
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= op= Lowest
a = 0011 3
b = 0110 6
a|b = 0111 7
a&b = 0010 2
a^b = 0101 5
~a&b|a&~b = 0101 5
~a = 1100 12

int a = 4;
int b = 1;
boolean c = a < b;
Control statements
• If else:
if (condition) statement1;
else statement2;
• The condition is any expression that returns a boolean value.
The else clause is optional.
• Nested if:
if(i == 10) {
if(j < 20) a = b; //here i==10 is true and this if is
else a = c; // associated with this else
}
else a = d;
• Switch case statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
• if-else-if Ladder
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
• The expression must be of type byte, short, int, or
char; each of the values specified in the case
statements must be of a type compatible with the
expression. Each case value must be a unique
literal (that is, it must be a constant, not a
variable). Duplicate case values are not allowed.
• The default statement is optional. If no case
matches and no default is present, then no further
action is taken.
• The break statement is used inside the switch to
terminate a statement sequence. This has the
effect of “jumping out” of the switch.
// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero."); break;
case 1:
System.out.println("i is one."); break;
case 2:
System.out.println("i is two."); break;
case 3:
System.out.println("i is three."); break;
Default: System.out.println("i is greater than 3.");
} } }
• Loops: entry control loops and exit control loops

while(condition) {
// body of loop }

int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--; }

int i = 100, j = 200;


// find midpoint between i and j
while(++i < --j) ; // no body in this loop, semicolon end loop
System.out.println("Midpoint is " + i);
do {
// body of loop
} while (condition);

int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);

//more efficient version of same code.


do {
System.out.println("tick " + n);
} while(--n > 0);
• The do-while loop is especially useful when we process a menu
selection, because we want the body of a menu loop to execute at least
once to show the menu.
• for loop:
for(initialization; condition; iteration) {
// body
}
int n;
for(n=10; n>0; n--)
System.out.println("tick " + n);

//Declaring Loop Control Variables Inside the for Loop


for(int n=10; n>0; n--)
System.out.println("tick " + n);
// Test for primes.
class FindPrime {
public static void main(String args[]) {
int num;
boolean isPrime = true;
num = 14;
for(int i=2; i <= num/2; i++) {
if((num % i) == 0) {
isPrime = false;
break;
}
}
if(isPrime) System.out.println("Prime");
else System.out.println("Not Prime");
}
}
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();

..........
.........
........
.......
......
.....
....
...
..
.
class BreakLoop2 {
public static void main(String args[]) {
int i = 0;
while(i < 100) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
// Demonstrate continue.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
} } }
This code uses the % operator to check if i is even. If it is, the loop continues without
printing a newline. Here is the output from this program:
01
23
45
67
89
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
• The output from this program is:
Before the return.

You might also like