Operators and Assignments and Flow Control

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Operators and

assignments
Operators and assignments

 Increment/decrement
 Arithmetic operators
 Concatenation
 Relational operators
 Equality operators
 Bitwise
 Short circuit
 Assignment
 Conditional operators
Increment and decrement operators

 Pre increment(int x=++y)


 Post increment(int x=y++)
 Pre decrement(int x=--y)
 Post decrement(int x=y--)
exp Initial value of x Value of y Final value of x
Y=++x; 10 11 11
Y=x++; 10 10 11
Y=--x; 10 9 9
Y=x--; 10 10 9
exp Initial value of x Value of y Final value of x
Y=++x; 10 11 11
Y=x++; 10 10 11
Y=--x; 10 9 9
Y=x--; 10 10 9
1) we can apply inc and dec only for variables but not for constant values
2)Nesting of inc and dec operators is not allowed
final Int x=4;
Int y=++x);
3)can’t apply on final variables
4)Can apply to all primitive data types except boolean.
5) In case of inc and dec operator typecasting is automatically performed
Difference between b++ and b=b+1

byte b=10; byte b=10;


b++; b=b+1;
S.O.P(b); S.O.P(b);

Output c.e.PLP
Output=11
byte b=10;
b=(byte)(b+1)
SOP(b);//11
ARITHMATIC OPERATOR

 +,-,*,/,%

 If we apply any arithmetic operator between two variables a and b the result
type is always

Max(int, type of a, type of b)

 Byte+byte=int
 Byte+short=int(int,byte,short)
 Int+long=long(int,int,long)

Long+float=float
Char+char=int
 Incase of integral arithmetic(byte,short,int,long) there is no way to represent
infinity and undefined result.
 Incase of floating point arithmetic there is way to represent infinity and
undefined result.
 Output?
 10/0=A.E
 10/0.0=infinity
 0/0=A.E.
 0/0.0=NaN
 The only operators that cause A.E. are / and %
String concatenation operator(+)

 Only overloaded operator in java


 Sometime act as arithmetic addition operator and sometimes as string
arithmetic operator
 Output?
 int a=10,b=20,c=30;
 String d=“shanti”
 SOP(a+b+c+d); ………………60shanti
 SOP(a+b+d+c);……………….30shanti30
 SOP(d+a+b+c);……………shanti102030
 SOP(a+d+b+c);……………..10shanti2030
 If atleast one operand is string type then’+’ act as concatenation otherwise as
arithmetic operator
 *note SOP is evaluated from left to right.
 Output?
 int a=10,b=20;
 String c=“shanti”
 a=b+c; -------output?(20+shanti)=20shanti string type
 c=a+c;-------output?(10shanti)
 b=a+b;-------output?30
 c=a+b; -------output?ce
Relational operators(>,<,>=,<=)

 Can be applied to every primitive datatype except boolean


 Can not be applied to object types.
Equality operator (==,!=)

 Can be applied to every primitive datatype including boolean


 Can be applied for object reference also.(meant for address comparison)
 To apply equality operator between the object reference compulsorily there shold
be some relationship between argument types(either parent or child or child to
parent or same type)
 Ex- t3
t1
thread t1=new thread();
Thread t2=new thread();
Thread t3=t1;
t2
SOP(t1==t2);//false
SOP(t1==t3);//true
 In general == operator is meant for reference comparison where as
.equals(method is meant for content comparison)
 String s1=new String(“hello”);
 String s2=new String(“hello”);
 SOP(s1==s2);………………………output?//false
 SOP(s1.equals(s2)); ………………………output?//

s1 hello

s2
hello
 SOP(~4)
 Output -5

4-----0000 0000 0100


~4------111111 1011
+1
000000000 0101
Bitwise operator(&,|,^)
X-or
and
or

 Can be applied to integral datatypes and boolean as well


 SOP(T&T)
 SOP(4&5);
 SOP(4|5);

Bitwise complement operator(~)


 Can be applied to integral datatypes but not for boolean
Short circuit operators(&&,||)

 They are exactly same as the bitwise operator(&,|) except


&,| &&,||
Both operands are evaluated always Second operand is optional
Relatively Low performance Relatively high performance
Applicable for integral and boolean Only applicable for boolean types

 They are used to improve the performance of the system.


Typecasting operators implicit

explicit

 implicit
 Compiler is responsible
 Upcasting
 No loss of information
 Following are various possible implicit typecasting

byte short
int floa
long double
t
char

 Ex-int x=‘a’;
 SOP(x)
 Explicit
 Programmer is responsible
 Bigger to smaller datatype variable
 Downcasting
 Chance of loss of information

byte short
int floa
long double
t
char

 byte b=(byte)130;
 SOP(b); -126
 130----00000000 ……..10000010
 ……….10000010(8 bit)======= -
 1111101+1=1111110=-126
Assignment operators

 Simple
 int x=20;
 Chained
 int a,b,c,d;
 a=b=c=d=20;
 Int a=b=c=d=20;
 int b,c,d;
 int a=b=c=d=20;

 Compound
 When assignment operator is mixed with some other operator (a+=30;)
 10101110 x>>2
 --101011

 In compound assignment operator the required typecasting is automatically


done.
 Byte b=10
 B+=1;
 SOP(b);
Conditonal operator(?:)

 The only ternary operator in java


 Ex- int a=10,b=20;
int x=(a>b)?40:50;
SOP(x);
Flow control
Selection Iterative Transfer
statements statements statements
1)If else 1)While 1)Break
2)switch 2)do-while 2)Continue
3)For 3)Return
4)For each 4)Try
5)Catch
6)final
If else
 Argument to the if statement should be of boolean type
 Curly braces are optional
 Ex-
 If(true);
Switch case
 Used when we have several cases are possible
 Ex
 switch(x)
 {
 Int x=10;
 case 10+20:
 }

 Curly braces are mandartory


 Both case and default are optional
 within switch every statement shold be under some case or default,independent statements are not allowed
 Until 1.4v the allowed datatypes are byte,short,int,char
 In 1.5v……..enum was added
 In 1.7……….string was added
 Example-
 Every case label should be within range of switch argument type otherwise we
will get C.E.
 Ex-
byte b=10;
Switch(b) max(int,byte)=int
{
case 10:
SOP(“10”);
Case 10:
SOP(“”);
Case 1000:
SOP(“”)

 Every label should be valid compile time constant, variable are not allowed
 Expressions are allowed for switch argument and case label shold be a constant
 Duplicate case labels are not allowed
 FALL THROUGH INSIDE SWITCH
 With in switch statement if any case is matched from that case onwards all statements
will be executed until break statement or end of switch.

Switch(x)
{
Case 0:
SOP(“0”);
Case 1:
SOP(“1”);
Break;
Case 2:
SOP(“2”)
Default:
SOP(“def”)
}
X=0 output 0,1
X=1 output 1
X=2 output 2,def
while

 Used, if number of iterations are not known


 Argument to while should be of boolean type
 Curly braces are optional

You might also like