0% found this document useful (0 votes)
2 views12 pages

Operators & Control Statement in Java

The document provides an overview of operators and control statements in Java, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It also explains control flow statements like if-else and switch, as well as loop structures such as for, while, and do-while loops. Additionally, it covers the use of break and continue statements to manage loop execution.

Uploaded by

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

Operators & Control Statement in Java

The document provides an overview of operators and control statements in Java, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It also explains control flow statements like if-else and switch, as well as loop structures such as for, while, and do-while loops. Additionally, it covers the use of break and continue statements to manage loop execution.

Uploaded by

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

cIASSMAte

Date
Page:

OPERATORS AND CONTROL


STATEMENTS IN JAVA
1Operators
yariables
Operators are used to perform operations. n
and values.
These include acithmetic,relat-iomal , logical,
bitwise asignment and miscellaneous operators.

a) Arithmetic Operatars
These are used for basic math operations.
Operator Example Descripion
+ atb Addition
d-b Substraion
Mutiplication
alb Diyisiom
% a%b Modulus (remainder)

@Curious-programmer
cIASSMAte
Data
Page

Exan1ple
int a =10 , b=5;
int sum =a+b; || SUm = 15
int remainder = a% b / remainder =0

b) Relational OperaBors
Ihese operators coTmpare values to see if they are
equal Qreater or smaller than
than each other.

Qperator Example Descripiom


VAV
Equal to
al=b Not equal to
azb Gareater than
d<b Less than
Greater tharn or equal to
<= Less than or equal to

Example
if (azb) {
printf ("a is greater than b");

@urious --programimer
cIASSMAte
Date :
Page:

9 Logical Operalors :

These are used when you want to check muliple


Conditioms at once (for example. if two things
are true ).

Operator Example Descriptiom


a8 b AND (both must be true)
all b OR (One of them is true)
NOT (inverts the condition)

Example
if (a>0) &R b<10)
| |Executs if both condiioms are true

combine conditions ike


Logical aperatorstwohelpcomditions
if are true at
checking
the Same time.

OCurious -programmer
cIASSMAte
Date :
Page:

d) Bitwise Operators
These wonk with the binary fornn f rumbers (bits)
and perform operatiorns indivicual bits.

Operator Example Description


& a &b AND
a lb OR
XOR
NOT
< a <<2 Left shift
a>> 2 Right shift

Example
int result a&bi | bituise and a andb

These used for low -level programming and


dealing with individual bits humbers.

@ Curious -programmer
cIASSMAte
Date :
Page:

e) Assignment Operators
These are used to assion a value to a variable

Operator Example Descripiom


Assignsb to a
d+b Adds b to a and stores the
results in a
d-=b |Subtracts btrom a and stores
the result in a
k=b Multiplies aand b and stores result ina
Divides a by b and stores the
resut in a
oo = a% =b Stores the remainder of a
dividend by b in a

Evample
int a=5;
t3 ; Il a= 8

Assignmert operaBors make it easy to store


results in variables afBer doing operations

O urious - programmer
clASSMAte
Date:
Page:

)Miscellaneous Operalors
These are special operators used or speciftc
tasks.

Operator Example Descripiom


b?a:b Ternary (shortend if-else)
sizeof s0Zeof (a) Gives the size of a
yariable in memory

Exanmple
int max (a>b) ?a:b ;
Il max = a it a>b , else max =b

These help perform tasks like quick decision


making or finding the size dt
Yariable.

@rious-programmer
if aciom twotrue,
decice
program only
cIASSMAte
repeat between
code is
perform Conditian
:DatePage:
Or ") choose @
urious
-progra
oa b
conditioms
your
help
nents block than
to youthe
Statements greater conditim
isnet
want letsif
statements statement
runs talse.
true.
is yoL statemert
Statementone
Flow based :statement ("a when : it's
sta actions. is S
printf code
b)
Comtrollouwdo conditiom
Eanaple: isIH a
used ifonly -elseif-else
if
to (a of ane
me
Cortrol if ifThe
whatcertaina)
if if blocks
2 and
b) The
to handle options
@
urious--programmer
cIASSMAte acioms me
to checking i breaki
break different
:DatePage:
parallel way ")i
Qreatera");
b');
than ")i");
day
cleaner
than by options
many
against ("Monday
Tuesday
nvalid manychoices
two
Conditians
Qreater Conditioms. hae
are (" ("
is printf menu
there printF
printf
) statemert
Switch
Stotement you
printfis
("a
is possible when1ike
when : :
(day)
Switch
("b 1 2 default for,
Casecase
if elseprintf
Exanmple
: (a>b) usedbased
take Suitch used
multiple
yariable check
is is
3
It The IH to
cIASSMAte
Date
Page:

3 Loops
Loops are used to repeat a set of actions multiple
times, like running a task over and Over
umtil condiion charnges
a) for LOop
A for loop is used when you know how many.
times you want o repeat action

Example
for (int i=0: ii<5i
i+)
printf ("%d"ili /Prints 01234

Its used when you meed to repeat something


a fixed number o times , 1ike loping
through a list ot items.
b) while Loop
A while lop repeais an acti¡n ad long as
Conditiom is true. You miaht mot know how
many imes it will repeat:
O urious - programmer
clASSMAte
Date :
Page:

Examples
int i 0i
while (i<5)
print+ "%d", i)i Prints 0011 23 4
1++j
3

I4:s useful when you dnt knou) how many


repeitioms you meed, but just want to
kep aoing until something changes.

) do-while Loop:
A do-while loop is similar toa while loop ,
but it always runs at least OMce betore
checkinghe condition

int i =0j
do
printf ("%d",i);
1++j
3 while (i<5) ;

urious -programmer
programme @uriLS
next the nmoves and
Toop
me.
on current heskips stament continue Ihe
Continue: b)
finishes. itbefore
fo p astop towant you when break Use
is wheni Ioop the SBops break (is=5) if
;itt) j=0ii<
10 Cint tor
Exarmple:
progranm
opart fhe to n moves
and
ompletelyfhe 1op the next stopsstatenernt
statoment break Jhe
breok a)
SiaBermernt continue and break 4: 4
:
conditian. the checking before
showing like once least atruns
menu
code the ensuYe towant you when used IA's
Page:
Date:
cIASSMAte
cIASSMAte
Date :
Page:

Example
tor (int i= 0; 1<10; 1++)
if (i==5) continue ; lskips prinHing 5
printt ("%d" i) 5
I/ Print 012 3 4 6 789
3

Use Continue when you want to skip certain


steps 1n loop but keep the Toop going

@ urious - programmer

You might also like