You are on page 1of 11

Chapter 4

Operators Conditions and


Loops
Arithmetic Operator

Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
++ Increment
+= Addition Assignment
-= subtraction Assignment
*= multiply Assignment
/= division Assignment
%= modulus Assignment
Bitwise Operator

Operator Description
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise EX-OR
>> Shift Right
>>> Shift Right zero fill
<< Shift Left
&= Bitwise AND assign
|= Bitwise OR assign
^= Bitwise EX-OR assign
>>= Shift Right Assignent
Bitwise Operator

Operator Description
>>>= Shift Right zero fill
assignment
<<= Shift left assignment
Bitwise Operator
Class BitwiseOper
{
public static void main(String args[])
{
char c=‘A’; byte b= 100; short s= 100;
int i=-100; long lo= 100;
System.out.println(c & oxf);
System.out.println(b | 1);
System.out.println( s ^ 1);
System.out.println(~i);
System.out.println(lo | 1);
System.out.println(I >> 2);
System.out.println(s >>> 2);
System.out.println(I << 2);
}
}
Relational Operator

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Control Statements
if statement: It is one of Java’s selection statement or
conditional statement. It's operation is governed by the
outcome of a conditional test that evaluates to either
true/false.
Syntax: if(expr) statement;

if-else statement: else statement will be executed


when condition of if expression is false.
Syntax: if(exprn)
statement 1;
else
statement 2;
Control Statements
for statement: It is one of Java’s looping statement.
It allows one or more statements to be repeated.
Syntax: for (initialize; test; incrememt/decrement)
statements;

while statement: Statements after while are executed


only when condition is true.

Syntax: while (condition)


statement 1;
Control Statements
break statement:
break statement allows programmer to exit a loop from any point within its body,
bypassing its normal termination expression.
when break statement is encountered inside the loop ; loop is immediately terminated
and program control resumes at next statement following the loop.

Example:
class Breakdemo
{
public static void main(String args[ ])
{
int i;
for(i=1;i<100;i++)
{
System.out.println(i);
if(i==10) break;
}
}
}
Control Statements
continue statement:
Continue statement is somewhat apposite to break statement .It forces the
next iteration of the loop to take place.

Example:
class Continuedemo
{
public static void main(String args[ ])
{
int i;
for(i=1;i<100;i++)
{
if(i < 95 ) continue;
System.out.println(i);

}
}
}
Control Statements
switch statement:
switch statement is a multiway branching statement. It tests the value of a
variable against a list of case values and according to values a block of
statements will be executed
3. The expression must be of type byte,short,int,char.
4. Break statement is optional,else execution will continue on into next
case.

Syntax:
switch (expression)
{
case value1: statements;
break;
case value 2 : statements;
break;
|
|
case value n: statements;
break;
default : statements;
}

You might also like