We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 41
4
a
G truelines
BATE / go
eany
Java For loop
—e— g ao block of Code, us the E stezo instead
af 0 while boop :
Syntax
For (shatement |, statement 2, ghatemen| 3)
/1tode to be exe
[Statement 1 is executed before the execuhisn of the
core black
Blalemene 2 definea the vandition for exewting the
Se k how been —
exeusked
fla wil i fa ° 4
L
int 12/0) iiss i++) A
auf praintin (1); a
—
Gh Loop.» whichis used
Hreaigh elemerds in an ary.G reveines
Dares
pult_a ments_in the cars
Seat tEd |S
loop i
a block af cade a3 deny
thrashG63
o truclines
63 DATE: JJ
“Do_Idhile Joop —
ile loop —
{The da/while loop is a veriaht of the whil€
(This Loop will exetute the code lack ance, bebrare
i the candition \s tue, then it will repeat
phe Joop ai _tmg as the condition is bue, ____
Synkax
ido
YW tode bork ta be executed
Evample
will alias be evewubed sot least once, even if wal
candition i aa ode is
@ before the condition is tested.
int j =0; :
do
S
thus —ne habe implemertated Loop lalsmentt <<& - truelineso truelines |
72 DATE JY. bs
Example -
Sinng txt =* Hella blend”,
as out printin Ctyt Enllpper(aue (2), -@
“HELLO WoeiD”
(Syn a paniln Chet to OwerrCa.te C)),
#2 Oukput, * “hella world”
—__| Cohcuaien te)
=— Li hails wails i ees
—_— | methods! eo.
i
G truelines
1
DATE: _/_/__
TArraus ove. used te abe uy 2 Pat
| + ; f Fe 8 3 i
eo value — weer)
Mishoo 12s 4 Sie 7 8/5 —— Indice
|| index bg (cA 4
{| <— Array length .
75 to
or) —— e
"_Atashontiation. [gests “ofan. n Array in_dava |
— fecha al oe ty )
| ;oG truclines
Bales /—/_
fralliderlre on array debinedt the wanable bye sh
ere eii ech < =
| | ¢ + ovibe .
| int C muyNum = {to 20,26 ao};
—Sinag lI cars 5 a
You can access_an array element by referring jo_q@
This statement accesses the value of the first element in
Cars
a D te mIDE:
o—
aL
To _Find out har - elements On array hau, use @
Ibe Length :
Example.i 8.3
L truelines
a
Example See err. =
~» String C1 cars = {“Volva". “BMnL” “Pard” }, =
eee i
public clas Moin [
int _o¢, // Create o closs otibukey
I
| {
“public _main() |
_ ee =6; init 2
| Bivins. finn
eo patie (mybj) / Print the value of x
i
Outputs oa
etal
a
ble howe §mplement method ovexlaading ee.Program code.
public class OneDArray {
‘void show(int xf)
4
for(int i = 0,1 < xlength; it+)
4
e ‘System.outprint(xli+" 7),
)
y
‘Public class OneDArraytoMethod {
‘Public static void main(Stringl] args)
‘4 Declare an array x wit initialization itl] x = (2,3, 4,5, 6,7, 8);
‘1 Create an object of class OneDArray.. OneDArray obj = new OneDArray();
System out prntin(Value of array :"); obj show(x);// Calling show) method with passing 10
array.
F ~
) A
- Output:
Value of array x: 2345678
43...rade
'
G ... truelines
DATE: _/_/__
prolected Shing byaid_=" fae” ; vehicle bhi bude
public oid o4@ truelines
Ds) e
Z .
nhemifran ceclass) and the value of the e modell
ind myCar.nodelNane);i - a
| od truelines
pate: _f_/__
Experiment Nos
aT += Timp! ementation of AVA Exceplians eas
Thenry t= Jawa Exception .
——lhen executing Jawa code, different evrars—can_accutt:
eding erm made by the pmgammer, emacs due
ng input
HThe Dt apna wed together saith an
exception 4y c There cere many exceplicn typ
Loic _____
ArithmeticException , FileNatPoundExeplon,
Se colin stecanttaCy. oli 18.3
g truelines
Daves Jj -
alien SE a
Ahroa_an_exeplion if ogé isha 18 TP_age is 12
_lor_alder, print “Access_granted
a
publ clats_Main_{_
GE Cage
_fiedelge C16 USeb age 4015 (which is bolas
‘The
! dutpuk will be. :
Lvceplion in took" main” jaxa lang. Arithmebicb veep
! dekined - Yau mut be ot least 12 years old.
at _main - (Main. java: ¢) P
exceptions.18.5
an exception type. There are many exception types
NotFoundException,
ption,ASG
i (2ge 18) (throw new ArithmeticException(‘Access denied You must be at least 18 years old.’))
else ( System.out printin(‘Access granted You old enough!");
n in thread "main’ java.lang.ArithmeticException:
pust be at least 18 years old.
njava4)