You are on page 1of 102

'

( )

9
=
> B9
F

Copyright Kasun De Zoysa 2011-2012

First published 2012


Copyright Kasun De Zoysa 2011-2012

All rights reserved. Without limiting the rights under copyrights reserved above, no part of
this publication may be reproduced, stored in or introduced into a retrieval system, or
transmitted, in any form or by any means (electronic, mechanical, photocopying, recording
or otherwise), without the prior written permission of the copyright owner.

Formatted using Open Office


Printed and bound in Sri Lanka
by University of Colombo School of Computing (UCSC).
www.ucsc.cmb.ac.lk

Copyright Kasun De Zoysa 2011-2012

1. ' (Computer Languages).............................................................................7


2. ' (Java Programming Language).....................................10
2.1 9 (Java Development Kit)................10
2.2 '
R
(Executing a Java
Program)....................................................................................................................................................11
2.3 '
(Basic Component of a
Java Program) ...................................................................................................................................15
2.4 '

[ (Indentation)....................................................18
2.5 (Comments).......................................................................................20
3. ,

(Variables, Expressions and Statements)
..................................................................................................................................................................................23
3.1 (Variables and Operators).............................23
3.2

(Expression and Statements)................................25
3.3 ' ] R^ (Printing a Value)...........29
3.4
c (Entering a Value).........................32
4. (Operator)...................................................................................................................37
4.1 d (Mathematical Operators)....................................37
4.2 d ...................................................................................41
4.3 (Relational Operators)...............................................46
4.4 9R (Logical Operators)............................................................52
4.5 9R (Short Circuit Logical Operators)....54
4.6 ...........................................................................................................................57
4.7 .....................................................................................................................68
5.
.............................................................................................................................................72
5.1 if else
................................................................................................72
5.2 switch-case
.............................................................................................73
5.3 do while
(do while )...........................................................80
5.3 while
(while ).............................................................................87
5.4 for
(for )......................................................................................92
5.5 R e .....................................................................................97
5.6 ] .........................................................................................................................102
Copyright Kasun De Zoysa 2011-2012

4 -
'

6. .............................................................................................................................................106
6.1 c....................................................................................................106
6.2 for-each
..........................................................111
6.3 ..........................................................................................................117
6.4 R R^...................................................................122
6.5 ] l= .........................127
6.6 R^........................130
6.5 ...............................................................................................................133
7. .............................................................................................................................................................138
7.1 ..........................................................................................................................138
7.2 c...............................................................................144
7.3 ] l= .........................148
7.4 ] o eR
....152
7.4 R^...............155

Copyright Kasun De Zoysa 2011-2012

o '
i

'

9 , '
l
.
F , ' F

> B, 9
=

, '

i p c ].
'

'

'
, '

'
i

.
F
' F ,

'
i 9

=,

Copyright Kasun De Zoysa 2011-2012

6 -
'

c, 9 p .

'
,

'

u i

=
.

9
' F
F

Copyright Kasun De Zoysa 2011-2012

p
'

, '

'
i ,
' y ].
'

'

,
'

p. '
c l


9
uy p c .

'
,
R


R. , c ]

uy ' R

o
.
Copyright Kasun De Zoysa 2011-2012

8 -
'

'

'
9

=
9
.

9
=
' F
F

Copyright Kasun De Zoysa 2011-2012

***

$ .
******

Copyright Kasun De Zoysa 2011-2012

1. ' (Computer Languages)

c ' u

1 0

p
.

(assembler code) '.

' 1 0

, '9

1 0 p '9

.
c '9 (Assembler)
p. '9 1 0 p
'9 -1.1 p =.

MOV AX,10
ADD AX,BX

01101001010101
10111101010111

Assembler

1.1 : 9
<

MOV

AX,10

R.
9 '9 1 0

p
'9


.
c p

' u

p R 9 ]. '
p
.

u '

'

p .

'

R^ '
p.
Copyright Kasun De Zoysa 2011-2012

11

'
c, ' ]
' 9 . e

(library) '

R. '
(),
c (
c), ' ] , 9
.
' R u

c e

u 1 0 p
(machine code)


.
' , '9 p

R
=.
p c (compiler)

'
p
(Object Code)
=. p

'


'

p9
9c

(linker) ' ,

, ' R
p =. 1.2
R
p =.

C '

'
R. e

p

c '
1.2

=.

Copyright Kasun De Zoysa 2011-2012

12 -

'

(Compiler)

#include<stdio.h>
main(){
printf("I Love C");


p
(linker)
(library)

(Object Code)

(machine code)

1.2 :

Copyright Kasun De Zoysa 2011-2012

2. ' (Java Programming

Language)

1991 c >
> '

9 p. i
1994 c 9 9

' y
.

'
( )
9 (Java Software Development Kit) '

]
.
9

.

2.1 9 (Java Development

Kit)

(Ubuntu) '
] =
, 9 . e

install

(Terminal) sudo apt-get

sun-java5-jdk c ,

( ) .

' 9 ]
.

'

] ,

9 , java.sun.com

'

(Install) '
] .

Copyright Kasun De Zoysa 2011-2012

14 -

'

9 ' R

.

(javac), (java)

(javadoc) '
.

(javac), '
p

'9 =. (java), p ,

(Java Virtual Machine) , eR e R

=. e (linker) 9
(java) .
'


,

(javadoc) .
' i

, 9

],
(javac) (java) l

'

.

2.2 '
R
(Executing a

Java Program)

'

'

(),
R^, '9 R^
R
p R (3) .
1.

'
()
R^
R^.

2.

'

'9 R^.

Copyright Kasun De Zoysa 2011-2012

3.

15

'
, R
.

(Text Editor) '

. e
R^c

R .
:

(program name)

(file name)
.

9 (extension) .java
.

'
2.1
. e

ILoveJava
'

. e e '
IloveJava.java


.
R^

(text editor)
. R

(directory) .

public class ILoveJava{


static public void main(String[] args){
System.out.println("I Love Java");
}
}

2.1: <

i '
, prgs
(directory)

=
. i
e '

Copyright Kasun De Zoysa 2011-2012

16 -

'

' p

'9

.
javac IloveJava.java

p

'

IloveJava.class y

p
. i

'
' R

.
java IloveJava

'
I Love Java

' ]
. u

(Ubuntu) '


] ]
(Terminal) , y

'
R

2.2

=.

1
2

4
6

5
7

2.2: u

<

9
.

Copyright Kasun De Zoysa 2011-2012

17


R ,


R '
]

e
'

c, p

javac '
R


java '


]
.

'
'
]
R
.
1.

mkdir

(directory) p .

prgs

c 1

.
2.

cd ,
y

=.

prgs


R^ 2
.
3.

vim
u

'
] ]
R.

IloveJava
R^

vim R

3 .

'
IloveJava.java


.
4.

ls

=. 4

'p

ls

'

IloveJava.java ] l= .
5.

' p,

javac

IloveJava.java '
p

=.
6.

6 ] 'p ls
IloveJava.java IloveJava.class

Copyright Kasun De Zoysa 2011-2012

18 -

'

y
p
.
7.

] 'p

'
R

, I Love Java ' ] .

2.3 '
(Basic

Components of a Java Program)


l 2.1 '

]
.
'

, '
R^ y

public class =.
e ,

] IloveJava '

l . e

. e
,
(program name)

(file name)
.

'

({})
=. e '

( { })
. uy
static

public

void

main(String[]

args)

'
R

R^
static
public
.

void

main(String[]

args)

static, public, void, main String[]

args R .

, '
R

, ]
l .
Copyright Kasun De Zoysa 2011-2012

19

p
.

({}) . e R

p
.

'
e d. e
System.out.println("I Love Java")
. ' ]
R^ System.out.println
. e u c ] ' ]

=. '

] (;) .
l We will learn Java ' ]


=
. e System.out.println("I Love
Java") System.out.println("We will learn Java")
R^ . lR R

'
, p

R
d.

public class ILoveJava{


static public void main(String[] args){
System.out.println("I Love Java");
System.out.println("We will learn it");
}
}
2.3:

'

l I Love Java We will learn it

'

= .
e

Copyright Kasun De Zoysa 2011-2012

20 -

'

System.out.println("I

Love

Java")

System.out.println("We will learn it") e R^


. e c e e


R^ .

'
2.3 c ].

'
R
I Love Java
We will learn it R

' ]


( 2.4).

2
3

2.4: u

<

9
.
1.

IloveJava
R^ vim
R
1 .

2.

'p

javac

IloveJava.java '
p

=.
3.

] 'p

'
R

Copyright Kasun De Zoysa 2011-2012

21

I Love Java We will learn it R


' ] .

System.out.println

System.out.print() . e I

Love

Java We will learn it R

e .

e System.out.println ,

, System.out.print ,
e

=.

System.out.print

System.out.println R, e
. e

R^
c , ]
,

l
' + e
.
System.out.println("I Love Java"+"We will learn it")
l

'
, '

] .

2.4 '

[ (Indentation)
'

c,
[ (Indentation) i

R. '
p
R^




.
[
[

'
]

p R
=.
2.1 2.3 '

,
[ ]

R
.

[
[
p , 2.3 l
Copyright Kasun De Zoysa 2011-2012

22 -

'

'
. '

public class

'p,
[
[
c

e
.

'
]

.
uy static public void main(String[] args)

. '
R

R^ ]

. , e


(}) ]


. e ]
,

.

'
] p
R

R^,

[ . '

p R R
R^, '

R.

public class ILoveJava{


static public void main(String[] args){
System.out.println("I Love Java");
System.out.println("We will learn it");
} }

2.5:
P <

(2.5)
[
[

'

Copyright Kasun De Zoysa 2011-2012

23

l.
[
[
'

R
. e
, '
]

u R

[
] ,
'


c
[ R^
. '


[ R^, '
p
.

2.5 (Comments)
'

l . '
R

, (comment) . '

c (comment) c i
.

'
,
, e ]

, c
'

. e '

=. ' c,
R /* d , */ d

. // d
.

'
R

c,

=. e '
R
'
R ] .

c, '
] R

=.

'
l.
Copyright Kasun De Zoysa 2011-2012

24 -

'



R
]. e /* d , */ d .

e '

p c ].
.
i
lR R. //

d ]
.
e '
, c ] ' ]

c ].

'
R
c, c ]

R ' ] .

'
] ,

, '
R

' p R^

p =
.
/*
This Simple Java Program prints "I Love Java"
on the Screen.
*/
public class ILoveJavaWithComments{
static public void main(String[] args){
//Following command creates the print
System.out.println("I Love Java");
}
}

2.6:
'

=.

i '
System.out.println ("I
Love Java") ip' // .

'
R
c, ] R

Copyright Kasun De Zoysa 2011-2012

. e

25

// System.out.println ("I

Love Java") , ]
.

'

c, p ,

'
,
p

ip' .

'

y , e p c ] ,

[
[
] '

e,

'

,
[
[

Copyright Kasun De Zoysa 2011-2012

3. ,

(Variables,

Expressions and Statements)

'
lR R
R .

(operators),

(expressions),

(variables),

(statements) p.

'[c, ,
,
p
.

3.1 (Variables and Operators)


'

'

(access) R^ , (variables)
.
.
' ,

, e '

=.
]
uy

+ d

e =. d

'


=.


c .

(4) '[c p
.

'
R ] .

= (byte)

(char)

Copyright Kasun De Zoysa 2011-2012

27


9 o (short)

9 o (int)

p
9 o (long)

o (float)

p o (double)

(boolean) .

R l.

= (byte)

-128 127

(char)
16
0 65,535 ()

9 o (short) 16
-32,768 32,767

9 o (int)
32
-2,147,483,648
2,147,483,647
p
9 o (long)
64
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
o (float)

32

-1038 +1038

p o (double)

64

-10308 +10308

(boolean)
1
,

3.1: 9

'

c
c,

9 p
. e
p .

'
c, , R,

$ _
. e 2num
0zero

_num $zero0

Copyright Kasun De Zoysa 2011-2012

28 -

'

'

e int byte

y .
num, Num NUM , '

c eR

num, Num NUM

' ]

3.2

(Expression and Statements)

(expressions)

eR.
.
,

c .

9
(expressions) '

c
(statements) = R
.

(expression) ' R


R

(statement) . e

e ]

(;)
.

'

(variables)

(expressions)
(statements)
uy

R p
.
3.1, o

e R^

R. i '[
]
u

R
. e 12 + 15 = 25 ' ]
Copyright Kasun De Zoysa 2011-2012

29


. 12 + 15 =

System.out.print("12+13=") =. e
]

25


System.out.println(c)

public class Add {


public static void main(String[] args) {
int a; int b; int c;
a=12; b=13;
c=a+b;
System.out.print(" 12 + 13 =");
System.out.println(c);
}
}

3.1: U e 9V
e

u R ] , c

. e ' 25 ]

. e c (variable) =
R
.

'
R^

. R^ e 9

l= '
p
. e c

9 (integer) int c
(statement)
'
c ] . int 9
c .
c a b

Copyright Kasun De Zoysa 2011-2012

30 -

'


]. int a int b

'
c ] .
c

] e 9 ,

(int a,b,c). e


.
i uy

a=12 b=13
(expression)

, a b 12 13 c ].

(expression) .

y, e
(statements)

]
.
R

a b
12 13 .

p . e int a int , b a=12 , b=13


int a=12, b=13


. c, e

a b eRR R^ (,)
. , a b

12 13

c ] , int
9
.

c=a+b
(statement) a b
e c c ] .

c =

p
. '

c e

(assignment mark)

p. e

(expression) ]
(variable)

=.

uy
c

] a+b

, e a b e ] c
.

Copyright Kasun De Zoysa 2011-2012

R^

31

. '

9
R. i uy
a, b c

ip'

number1, number2

sum . e

p
.

'
c,
p

i
. i '

, p 'p .

public class AddNew {


public static void main(String[] args) {
int number1=12,number2=13,sum;
sum=number1+number2;
System.out.println(number1+"+"+number2+"="+sum);
}
}

3.2: U e <

c System.out.println()

number1, "+",

number2, "=" sum ]


.
eR +

y .


u c ] e
' ] =.

Copyright Kasun De Zoysa 2011-2012

32 -

'

3.3 ' ] R^ (Printing a

Value)
' ] R^
. 1025.25 c(/) 32.1

'

. c 1025.23 32.1

o ]
.

o cp
( 3.3)

l 'p e f '
. u
o , e ,
(float)


l
'p

answer=number1/number2
u
System.out.println (number1 + "/" + number2 + "=" +
answer) e ' ] =.

public class DivideIt{


public static void main(String[] args) {
float number1=1025.25f,number2=32.1f;
float answer;
answer=number1/number2;
System.out.println(number1+"/"+number2+"="+answer);
}
}

3.3: X <

'

System.out.println System.out.format

System.out.format

' ]

. i

Copyright Kasun De Zoysa 2011-2012

number1 number2

33

u
System.out.format

'p
( 3.4).

public class DivideItFormat{


public static void main(String[] args) {
float num1=1025.25f,num2=32.1f;
float ans;
ans=num1/num2;
System.out.format("%.2f / %.2f = %.3f
%n",num1,num2,ans);
}
}

3.4: X 9V
System.out.format


,

.
i uy
%n" =.

,
"%.2f / %.2f = %.3f

c %.2f o,

R . o
R

%.3f . % d


' ]
%.2f = %.3f %n"

. e

R
,

"%.2f /

, i
/ ,
o

,
=

, o
Copyright Kasun De Zoysa 2011-2012

34 -

'

=.

, ] %n u
=. e

, %n u
.

,
num1, num2, ans

p
. e c
c ] num1

, %.2f ]
. e e
] .

i
], num2 ,
] %.2f

e ] ans

, %.3f ] .
i

] o , u

l= l p =

. System.out.format ,
' ] .

l
, 345 * 460
'

.
o R

. '
uyR ( 3.5).

public class ShowMyName{


public static void main(String[] args) {
String name=Kasun De Zoysa;
System.out.println(name);
}
}

3.5: <

Copyright Kasun De Zoysa 2011-2012

35


c, Kasun De Zoysa '

name . e 9

(String) . (String)
9

, e '
c (Object)

]. (Object)


. e l

9 .
, ,
() p . (), u
p
. i
,
Kasun De Zoysa
c . c, Kasun De Zoysa u c
].

, System.out.println(name)

' ]

3.4
c (Entering a Value)

'

l p
.


, ' ]

'

. e '
3.6

l . l e R

. ec
'
]
Enter Your Name :
. l


'

Copyright Kasun De Zoysa 2011-2012

36 -

'

kasun Welcome kasun

' ] .

import java.util.*;
public class ReadName{
public static void main(String[] args) {
String s;
System.out.print("Enter Your Name :");
Scanner in = new Scanner(System.in);
s=in.nextLine();
System.out.println("Welcome "+s);
}
}

3.6: 9V <

'
s
. c, Enter Your Name : R '
]

System.out.print ("Enter Your Name :")

],

Scanner in = new Scanner(System.in) s=in.nextLine()

.

.

java.util.*

. e
,

,

.

,

import

=. c,

import

Copyright Kasun De Zoysa 2011-2012

37

p

. i uy
import java.util.*


e=.
Scanner

in

new

Scanner(System.in)


, in

Scanner ' c ].

(Object)
l= , e
.
R ,

R.

R
in R

nextLine() =. e s=in.nextLine()

nextLine() in R

R
=. R

(assignment mark) s =.

' ]

] System.out.println ("Welcome "+s)

p =
. ,
, R

, s=in.nextLine()

, p =
.

l

' ]

i
.

'
( 3.7) R
.
'
name

age . i
, Enter
Your

Name

System.out.print

Name :") ' ]

("Enter

Your


R
in

Copyright Kasun De Zoysa 2011-2012


'

38 -

Scanner

Scanner in =

new Scanner (System.in)


'

. ,

nextLine()
R , name =.
i
Enter Your Age : System.out.print
(" Enter Your Age :") ' ]

],

age=in.nextInt()
=. R nextLine()

,
9 R

nextInt() =.

import java.util.*;
public class ReadNameAndAge{
public static void main(String[] args) {
String name;
int age;
Scanner in = new Scanner(System.in);
System.out.print("Enter Your Name :");
name=in.nextLine();
System.out.print("Enter Your Age :");
age=in.nextInt();
System.out.format("Welcome %s %n",name);
System.out.format("You are %d years old.%n",age);
}
}

3.7: 9V <

nextInt() in R
,

y o . R

o,
(assignment mark) age =.
Copyright Kasun De Zoysa 2011-2012

R nextLine() ,

39

R nextInt() l

p =
. Scanner
in ,

in.nextLine()

in.nextInt()

in

R


. Scanner name ,

name R

name.nextLine()
name.nextInt() .

i '

, R
'
]
]


System.out.format =. '

System.out.format

System.out.format ("Welcome %s %n",name) ] .

c

,
c ] .

, e
%s p
.

e name , %s ] .
' ]

R^ System.out.format

, System.out.format ("You are %d years old.%n",age)

] p =
. c age

, %d
.
9 o

%d l
.
'

'

R
, (variables)
(expressions)


(statements), '

Copyright Kasun De Zoysa 2011-2012

40 -

'

4. (Operator)

'[

'

(operators) u
. ' ]
, d (arithmetic), (relational), 9R
(logic), (bit) (conditional)
i '[

9 =. l

c + / d

]. e , 9R,

'[ c

.

4.1 d (Arithmetic Operators)

d
. e R^

+
R^ - R^
*

c / =.

R e
R^c *


, + i

R
.

x=4+5*3-2
R


x
17
. e R^

R^

p
e

p
. x=(4+5)*3-2
R

x

25 .

Copyright Kasun De Zoysa 2011-2012

41

R


.

=. x=x+1 '
.
d

x x+1

. e
' =

. e i


x+1

x =.

R
x 5 x+1
6 .

x=x+1
, x+1 x , y

x 6
.

l x=x*4
. e x
4 9
9
e x

=.

R
x 8 e R

x
32 . d

'
R
. ( 4.1).


c int a=8,b=3 c=0

a,b,c . c a

8 , b 3 , c 0 . i
] c=a+b

a+b
c . e e
R

c 11 . System.out.println(c) e
(11) ' ] =.

c=a-b
a-b
c

. e e
R

c 5 .
System.out.println(c) e (5) ' ]
=. a*b 24 .

Copyright Kasun De Zoysa 2011-2012

42 -

'

public class Operators{


public static void main(String[] args) {
int a=8,b=3, c=0;
c=a+b;
System.out.println(c);
c=a-b;
System.out.println(c);
c=a*b;
System.out.println(c);
c=a/b;
System.out.println(c);
c=a%b;
System.out.println(c);
}
}

4.1: [
a/b
]
. e

c, (2) '

u c
9

.
9 o o
R

, ' .

e, '


9 o

' ]
=.
, c

(float) ' c
. c=a/b

c=(float)a/b R^ R.

(mod) (%) =. e a%b

Copyright Kasun De Zoysa 2011-2012

43

2 ' ] .

l '
o
, e o
d

i

. i

'
( 4.1), 'p ( 4.2)

.

, int a=0, b=0 c=0



a,b,c

0
. o

] Scanner in = new

Scanner(System.in) a=in.nextInt()
=.
import java.util.*;
public class ReadNumbers{
public static void main(String[] args) {
int a=0,b=0,c=0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a Number :");
a=in.nextInt();
System.out.print("Enter a Number :");
b=in.nextInt();
c=a+b;
System.out.println(c);
c=a-b;
System.out.println(c);
c=a*b;
System.out.println(c);
c=a/b;
System.out.println(c);
c=a%b;
System.out.println(c);

4.2:
U 9V

Copyright Kasun De Zoysa 2011-2012

44 -

'

Scanner in = new Scanner(System.in)


, in

Scanner
'
. i



o R in

.
o in.nextInt()
R
a

=.

in.nextInt()
]
.
e R
o b ].
c ] 4.1 c ]

'
, p
=

. l '
R

, a b
c, d

4.2 d (Shot-Circuit

Arithmetic Operators)
d R R

. d
.
d p '

R

( 4.3).

c, a+=b a=a+b;
=. a+=b

], += , d

p . e, a=a+b
, += d

a+=b . e a+=b

R
c, a+b a . e c, a

Copyright Kasun De Zoysa 2011-2012

45

12
.

public class ShotOperators{


public static void main(String[] args) {
int a=8,b=4;
a+=b;
System.out.println(a);
a-=b;
System.out.println(a);
a*=b;
System.out.println(a);
a/=b;
System.out.println(a);
a%=b;
System.out.println(a);
}
}

4.3: [

a=a-b
a-=b -= d

. e a-=b

R
c a-b a
. e a
8 .

a*=b
a=a*b
R

l
. e a*=b
R
c a*b
a . e a 32 .

a/=b
a=a/b
R

l . e a/=b
R
c
a/b a . e a 8 .

9
i
(mod)

Copyright Kasun De Zoysa 2011-2012

46 -

'

d (%) . e a%=b
R

c a%b a
. e a 0 .

c 12, 8, 32, 4 0

' ] l p
=

.
d
R
'
p = ( 4.4). c, ++

].

++

, a=a+1
. e,

a=a+1
a++ . a++
R


a eR . e a 13 .

public class Increment{


public static void main(String[] args) {
int a=12,b=4;
a++;
System.out.println(a);
a=b++;
System.out.println(a);
System.out.println(b);
a=++b;
System.out.println(a);
System.out.println(b);
}
}

4.4: ++ [
++ d

. a=b++ d
a=b+1

Copyright Kasun De Zoysa 2011-2012

47

.
a=b++ d
, a=b b=b+1
d


.
e ++ d


, b ] a

R^
, b eR =. uy
c, b

4 , a=b++
R

a 4 b
5 .
a=++b;

++ d

. a=++b d

,

b=b+1 a=b d
. e ++
d

, b ] eR


, e a =. uy c b 5
, a b 6 .
uy
a=a-1

a--

( 4.5)

'p,

. c, --

d
. -- d , c

] eR
=. e a 12 , a--;

R

a 11 .
-- d

. a=b-- d
a=b-1

. e a=b b=b-1
. e - d


y ] , b

eR

, e a p
. e, b 4

, a=b--
R

, a 4 b
3 .
a=--b , -- d

. e b=b-1 a=b
.

Copyright Kasun De Zoysa 2011-2012

48 -

'

c, b eR

, e a =.
e b 3 , a b

2 .


c 11, 4, 3, 2 2

' ] l p =
.

p
, '

R
.

public class Decrement{


public static void main(String[] args) {
int a=12,b=4;
a--;
System.out.println(a);
a=b--;
System.out.println(a);
System.out.println(b);
a=--b;
System.out.println(a);
System.out.println(b);
}
}

4.5: -- [
'
c R, d

l.

'p

eR^ , - d
R^
, * d R^ , / d

c
. %

d =.

Copyright Kasun De Zoysa 2011-2012

uy

49

uy

eR^

12-3
9

R^

R^

5*7

35

8/2

13 % 5

++

a++

a

eR .

4+6

10

eR^
eR

a

eR

R^

--

a--

4.1: [

++

eR R^

R^ =.

, e ,

] p .

4.3 (Relational Operators)


l (relational operators)

i
. o (variables) R R^
=. o
Copyright Kasun De Zoysa 2011-2012

50 -

'



9R
(logical
expression)

. 9R
(True)

(False) R . 9R
, '

R
R^ y .

9R

(control statements)
=.

'

( 4.6)

(relational operators), 9R
(logical

expression),
(control statements) p
.


c,
(*****)
' ] .

'


System.out.println(*****)

System.out.println(+++++)

' ] c ' ]


.
,

'
], if(a>b) else
=.

public class Stars{


public static void main(String[] args) {
int a=12,b=4;
if(a>b) System.out.println(*****);
else System.out.println(+++++);
}
}

4.6:

'
c, R

Copyright Kasun De Zoysa 2011-2012

51

=. '

c, if else,

switch case, do while, while for


. '[ c if else

i
.

if else
, c ] '

R
=. '
if(a>b)
else e y ].

if
9R

. o R
9R

(logical expression) . 9R

R
(True)
(False)
d.
i uy

c, if
a>b 9R

] . e c, a b

(>)

]
.

, 9R

e
R l

. i '

a b

12 4 , (a>b) 9R
.

9R

, if

] R
=. 9R

, else


] R

. if
, else

] . e 9R

R .

i '

c 9R

, if
, c ] R
=. ec

Copyright Kasun De Zoysa 2011-2012

52 -

'

' ]
.

l i '
b 20 y

. e (a>b)
, else


R
]
(+++++) ' ]

. c
= l=, l
p =
.
9R

, R

l .

(>), 9R

] .

'

c R,
( 4.2) l.

uy
(a=12;b=10)

uy

==

a==b

!=

a!=b

>

a>b

<

a<b

>=

a>=b

<=

a<=b

4.2:

l 'p, , 9R


(==),
(!=),
Copyright Kasun De Zoysa 2011-2012

(>),

53

(<),

(>=)

(<=)

. l= ,

(==), (

=)

.
uy

p
. o,
o , i

o R R '

R l= .
'

uy y .

public class Even{


public static void main(String[] args) {
int a=12;
if(a%2==0){
System.out.print(a);
System.out.println(, is an even number);
} else {
System.out.print(a);
System.out.println(, is a odd number.);
}
}
}

4.7: i U
o i
o

o =. e i


R i

, o
e i
.

Copyright Kasun De Zoysa 2011-2012

54 -

'

d .

i uy

if
] a%2==0
9R

o R


=
. e a%2 a (2)
i . e 0

=.

==

e a

o , a%2==0 9R

. 9R

, if
R

. if

R

, ] R
.

uy , if
e
. R (e )

R

, ({}) p

.
i u y ,
System.out.print(a)

System.out.println (, is an even number) if

,
System.out.print(a)

System.out.println(, is a odd number.) else



, ({}) ] . a

even

16 , if
R
16 is an
number ' ]

=
.
l

13

'

R
. e else
R
, 13 is a
odd number ' ]

.
e

, a

13 , a%2==0 9R


=
.

Copyright Kasun De Zoysa 2011-2012

55

4.4 9R (Logical Operators)

'
c,
9R (&,
|, !) =. , AND () 9R
(&), OR ()
9R

(|) NOT () 9R

(&

9R

(!)

R (AND) 9R


9R

d. e 9R

9


. 9R
(AND) 9R

(& )
9
R
l.

9R
(a) 9R
(b)
9
(a
& b)

4.3: AND () b9 (&)


9R
R (OR) 9R (|

) ,
9

9R
d. e 9R

Copyright Kasun De Zoysa 2011-2012

56 -

'

9R
(OR) 9R (| )


9
R
l.

9R
(a) 9R
(b)
9
(a
| b)

4.4: OR () b9 (|)
9R
ip' (NOT) 9R
(! )

,
e ,

e

9R


. 9R
ip' NOT

(! )

R l.

9R
(a)

9
(!a)

4.5: NOT () b9 (!)


l

(a) (3) (4)

Copyright Kasun De Zoysa 2011-2012

l=

, (7)

(9)

57

l= , (5)

l= '
.

public class CheckNumber{


public static void main(String[] args) {
int a=12;
if(a%3==0 & a%4==0)
System.out.println(a+ divides by 3 AND 4);
if(a%7==0 | a%9==0)
System.out.println(a+ divides by 7 OR 9);
if(!(a%3==5))
System.out.println(a+ is not divide by 5);
}

4.8: b9
(a) (3) (4)

l= (a%3==0 & a%4==0) 9R


.
(a) (7)

(9) l=

(a%7==0 | a%9==0) 9R
.
(a) (5)
(!(a%5==0))

o R

9R
. e

'
i 'p
.

Copyright Kasun De Zoysa 2011-2012

58 -

'

4.5 9R (Short-Circuit Logical

Operators)

'
c, AND () 9R
(&) OR () 9R

(|)

9R
()

9R

R
' i

c ]. AND

9R


&& . OR () 9R

9R

||

.
9R
R AND () 9R

,
9

9R
. e c, e

9R
, 9R


, . e 9R
R

, 9R
R


. && 9R e . &
9R

e . e,


, 9R
R
=.
&&

9R

, 9R

R
p =. 9R

R OR () 9R

, e 9R

9

. e c, 9R
R

, 9R
R

.
|| 9R e=. |
9R

e . e,
Copyright Kasun De Zoysa 2011-2012

59


, 9R
R
=.

9R
(a) 9R
(b)
9
(a
&& b)

R
-

R
-

4.6: AND () b9 (&&)


||

9R , 9R

R
p =.

9R
(a) 9R
(b)
9
(a
|| b)

R
-

R
-

4.7: OR () b9 (||)

9R

(&)

OR

9R

()

AND

()

9R

(|)

R
' i
l p =
. e,
'


c, AND () 9R , &&
9R

, OR () 9R
Copyright Kasun De Zoysa 2011-2012

60 -

'

|| 9R , R^
. e

9R c,

], 9R
R
.

4.6 (Bit Operators)


'
.

()

()

. '
c

(4.5) l.

uy
(a=1110;
b=0010)

uy
(c)

&

AND

c=a&b

10

OR

c=a|b

1110

XOR

c=a^b

1100

>>

Right Shift

c=a>>b

1111

<<

Left Shift

c=a<<b

1100

>>>

Zero Fill Right c=a>>>b


Shift

11

Compliment

c=~a

4.8:
c

1 0

=
. AND 1

1
B 1 ,

Copyright Kasun De Zoysa 2011-2012

61

c
B 0 . e AND
1
0 , 0 1

, 0 0 ,
B 0 .
e p =.

0 0 0 01 0 0 1

0 0 0 00 1 0 1

a & b

0 0 0 00 0 0 1

4.9: a & b (9 & 5 )

l 'p, a 9 b

5
. a b (=)
c ]

, 9 00001001

'

5 00000101

' .
l a b AND

a & b


. e, AND a b

, e e () R
=. a

e 1 b

e 1 , 1 AND 1

B 1 .

a
e 0 b
e 0 0 AND 0

B 0
. a
e 0 b
e 1

0 AND 1
B 0 . a
e 1 b

e 0 , 1 AND 0
B 0 . a
i]' 9

, b
i]' 9
0 , 0 AND 0
B ,
i]' 0 . e i

l 'p


B 00000001 1 .

Copyright Kasun De Zoysa 2011-2012

62 -

'

l, a

13

11


. e a & b
,
l 'p
00001001 9 .

a=13 0 0 0 0 1 1 0 1
b=11 0 0 0 0 1 0 1 1
a & b 0 0 0 0 1 0 0 1

4.10: a & b (13 & 11)

AND

uy

p
. , '
(
4.9) R
' ]

Rl= .
public class AndBitOperator{
public static void main(String[] args) {
int c=0,a=14,b=3;
System.out.format("%3d
System.out.format("%5s
System.out.format("%3d
System.out.format("%5s

=",a);
%n",Integer.toBinaryString(a));
=",b);
%n",Integer.toBinaryString(b));

c=a&b;
System.out.format("%3d =",c);
System.out.format("%5s %n",Integer.toBinaryString(c));
}
}

4.9: AND

Copyright Kasun De Zoysa 2011-2012

63

'
( 4.9) R
, 14
3 , 14 & 3

' ]
lR .
i

uy

System.out.format("%3d

=",a);

, a ' ]
.
9
Integer.toBinaryString(a) y . e ,

a p '9 =.
e

System.out.format '
] .

System.out.format("%3d

System.out.format("%5s

= " , b )

%n",Integer.toBinaryString(b))

, b , e , '
] =.

c=a&b
, AND a b


B c =. c

'

System.out.format("%3d

=",c)

System.out.format("%5s

%n",Integer.toBinaryString(c))

=
.
l OR


. OR

0 0
B 0

,
c
B 1 . e OR
1 1 , 0 1

, 1 0 ,
B 1
.

Copyright Kasun De Zoysa 2011-2012

64 -

'

p , a
, b

10

6
. a b

= (byte) c ]
, 10

00001010

'

00000110

'


.
.

6
l a b

OR a | b
.

0 0 0 0 1 0 1

0 0 0 0 0 1 1

a & b

0 0 0 0 1 1 1

4.11: a | b (10 | 6)

e, OR

a b
e e

()

R
=. a

e 0

e 0 0 OR 0
B 0 . a

e 1 b
e 1 1 OR 1
B 1 . a

e 0 b
e 1 0 OR 1

B 1 . a
e 1 b
e 0

1 OR 0
B 1 . a
i]' 9
, b

i]' 9
0 , 0 OR 0
B , i]'
0 . e i

l 'p

B 00001110 14 .
l, a

13

11


. e a | b
, l 'p 00001111
15 .

Copyright Kasun De Zoysa 2011-2012

a=13

0 0 0 0 1 1 0 1

b=11

0 0 0 0 1 0 1 1

a | b

0 0 0 0 1 1 1 1

65

4.12: a | b (13 | 11)

e , 4.9 '

, a b 13 11 c, c=a&b


c=a|b R
. e c c

15 ' ]
.
l XOR


. XOR

0 0 ,

1 1

,
B 0 .

B 1 . e XOR 1 0

, 0 1
B 1
.
p , a

, b 12
. a b

= (byte)

c ]
, 5

00000101 '

12

00001100 ' .
l a b XOR

a ^ b


. e, XOR , a b


, e e () R
=.

Copyright Kasun De Zoysa 2011-2012

66 -

'

0 0 0 0 0 1 0 1

0 0 0 0 1 1 0 0

a ^ b

0 0 0 0 1 0 0 1

4.13: a ^ b (10 ^ 6)

e 1 b

e 0 1 XOR 0


B 1
. a
e 0 b
e 0

0 XOR 0
B 0
. a
e 1 b

e 1 1 XOR 1
B 0
. a
e 0 b

e 1 0 XOR 1
B 1
. a
i]'

, b
i]' 9
0 , 0 XOR 0

B , i]' 0 . e i

l 'p
B 00001001, 9 .

l, a

13

, b

11


. e a ^ b
,
l 'p
00000110 6
.

a=13 0 0 0 0 1 1 0 1
b=11 0 0 0 0 1 0 1 1
a | b 0 0 0 0 0 1 1 0

4.14: a ^ b (13 ^ 11)

, 4.9 '
c=a&b

Copyright Kasun De Zoysa 2011-2012

67


c=a^b; R^ . e

, a b 13 11 c,
R

R^ c 6 ' ]

.
AND, OR, XOR

' p

, a b

, c=a&b
, c=a|b c=a^b ,

4.9 '
R

.
AND, OR, XOR

]
R^ <<
(Left Shift) >> (Right Shift)
y
.
a=7 0 0 0 0 0 1 1 1
b=1 0 0 0 0 0 0 0 1
a << b 0 0 0 0 1 1 1 0

4.15: a << b (7 << 1)


] R^ <<

(Left Shift) =. a

, b

1
. a

00000111

'

. a<<b


, a e R

=. e a << b
, l 'p 00001110
14 .

] 0

.
Copyright Kasun De Zoysa 2011-2012

68 -

'

]
R^ >>

(Right Shift) =. a

b 1
. a
00000110 '

. a >>

b
a
e R

=. e a >> b
, l 'p
00000011 3 .

a=6 0 0 0 0 0 1 1 0
b=1 0 0 0 0 0 0 0 1
a >> b 0 0 0 0 0 0 1 1

4.16: a >> b (6 >> 1)


c ]

0 1

. c 0

, ]
0

d. ]
1 ,

1 d. 0


,
R^ c, >>>

(Zero Fill Right Shift)


.
] 0 1 R^, 1
0 R^

. e a

9 (00001001) , a
l 'p 11110110 .

a (9),
9 '
, a -10 . ,

' r o l
.
Copyright Kasun De Zoysa 2011-2012

69

a=9 0 0 0 0 1 0 0 1
a 1 1 1 1 0 1 1 0

4.17: a (9)
l

uy

p
. , '
(

4.10) R
' ]

Rl= .
a 12 , e 00001100
'

. b 2 ,

a << b
a , R
=. e c 00110000

48 .


c
] 0

a >> b
a

R
=. e c 00000011

3 . o , ]
0

,
c, ]

.
] 0 1 R^, 1
0 R^

. e a

12 , ~a
-13 . ~a
-13

, ' r o
l
.

Copyright Kasun De Zoysa 2011-2012

70 -

'

public class BitOperators{


public static void main(String[] args) {
int c=0,a=0,b=0;
a=12;b=2;
System.out.format("%3d
System.out.format("%5s
System.out.format("%3d
System.out.format("%5s

=",a);
%n",Integer.toBinaryString(a));
=",b);
%n",Integer.toBinaryString(b));

c=a<<b;
System.out.format("%3d =",c);
System.out.format("%5s %n",Integer.toBinaryString(c));
c=a>>b;
System.out.format("%3d =",c);
System.out.format("%5s %n",Integer.toBinaryString(c));
c=~a;
System.out.format("%3d =",c);
System.out.format("%5s %n",Integer.toBinaryString(c));
}
}

4.10:

i '
( 4.10) R

' ] lR R.
12

= 1100
2 =

10

48 =110000
3 =

11

-13 =11111111111111111111111111110011
a b

Copyright Kasun De Zoysa 2011-2012

'

71

R
,
R

.

R . e a=a^b
a^=b

a&=b
, a=a&b

=.

c a b R
,

R ].

uy
(a=1110;
b=0010)

uy

&

AND

a&=b

0010

OR

a|=b

1110

XOR

a^=b

1100

>>

Right Shift

a>>=b

1111

<<

Left Shift

a<<=b

1100

>>>

Zero Fill
Shift

Compliment

L e f t a>>>=b
~a

0011
0001

4.18:

4.7 (Conditional Operator)


. if else



? :
y

.
c,
9R

Copyright Kasun De Zoysa 2011-2012


'

72 -

9R

, ?


]
R

. 9R

:
]
R
.

p '

R
.
marks 50
Pass s . e Pass

' ]
. l marks 40
. e Failed s , ' ]
Failed
.

public class PassFailed{


public static void main(String[] args) {
int marks=65;
String s;
s=((marks>=50)? "Pass": "Failed");
System.out.println(s);

4.11

e
]
R

(4.18) l. i ]


R
, ]

R
=.

e 2*3+5
* d
R

+ d

=. e

11 . l 2*3<=4

. e
2*3

Copyright Kasun De Zoysa 2011-2012

73

<=
R

=.

R
=.

. [] ( )
++ -- ! ~
new
* / %
+ <<
<

>>
>

>>>

<=

==

>=

!=
&
^
|

&&
||
?:
=

+=

&=

- =

|=

ect.

<<= etc.

R
=.

4.19: 9

a=++b%3-1

. e b 4
++ d

b 5
. i
%

5%3 2
.

1
.

(=) R
'=. e a 1
.

Copyright Kasun De Zoysa 2011-2012

74 -

'

R

p
. a=(b+3)%5


. e b 5 =
.
]

(b+3) R

8 . % d

R

i
. e
3 .
i

c

R

d

,
R


9R R
,

R

.

ip' c
.

Copyright Kasun De Zoysa 2011-2012

75

5.
(Control Statements)

'[ c ' ]

u
. l if else
=. e

'

c, switch-case, do while,

while for
.


'
R

R

=
.

l if else, switch-case, do while, while for


, '
R

l=
.

5.1 if else
(if-else control Statements)

i '[ c if else

. if
] 9R
,

if
]
R

=.

if
] 9R
,

]
R

. e c else

, else ]
R
.

5.1 l 'p, if
]

9R

,
-1a
-xa

Copyright Kasun De Zoysa 2011-2012

76 -

'

R
, if
]

else
]
(
-1b
-xb

) R
. if else
R

i p =
.

if

9R

else

-1b

-1a

-2b

-2a

-3b

-3a

-xb

-xa

5.1: if

5.2 switch-case
(switch Control

Statements)

switch-case
R
' if

R
'


. switch-case

Copyright Kasun De Zoysa 2011-2012

77

switch(x) ,
c x
R. x

(char),

=
(byte),


9 o (short)
9 o (int)

R. p
9 o (long),
o (float), p o (double)

(boolean)

switch-case

, switch(x)
p

. x
,

switch-case

] R. e , case x

R c
=. 5.2
p =.


] x
(char)

=
. e x , a, b, c ..
.
l 'p,

x a


-1a
-xd ]
R

. x

-1b
-xd , x

c
-1c
-xd ]

R
.
x

c ]

c,

case default
.

-1d
-xd ]

R
. e

, switch-case
case
default

R^ 9
. ,


switch-case

' break

Copyright Kasun De Zoysa 2011-2012

78 -

'

switch (x)

X ?

-1a

-xa

case 'a'

-1b

-xb

case 'b'
case 'c'

default

-1c

-xc

-1d

-xd

5.2: switch-case

l switch-case
uy

p
. 5.3 p ]
'

R
. '

System.out.println , ' ]
.
uy

'p ( 5.3)

switch-case


switch (a) ] . a

9
o . a , ]

Copyright Kasun De Zoysa 2011-2012

79


=. e a 1 case 1

, a

2 case 2

, a

3 case 3

, a 4 case 4

]
R


. a
1,2,3,4


default ]
.

public class Stars{


public static void main(String[] args) {
int a=3;
switch(a){

case 1:
System.out.println("*");
break;
case 2:
System.out.println("**");
break;
case 3:
System.out.println("***");
break;
case 4:
System.out.println("****");
break;
default:
System.out.println("#####");

5.3 switch-case
9V

Copyright Kasun De Zoysa 2011-2012

80 -

'

uy

a 3 , switch-case

R
, case 3 . ' ]

System.out.println(***) R
. e

System.out.println(*)

System.out.println(**)

R

,

switch-case
case 3

case 3

, System.out.println(****)

System.out.println(#####) ]
'
]

System.out.println(***)

] break

, switch-case
.


R
break
.

l break i i '

. e, ***, **** #####


' ]

. break ] , ****

#####
' ]

, break


switch-case
R

p =
.
l

d (+,-, *) ,

e R^,
R^ R^, switch-case



'

.
'

'
y (
5.4).

Copyright Kasun De Zoysa 2011-2012

81

'
,
9 o R^ a
b

, u R^ c

c ] . i
d

R^
9 op =. uyc op
R^ (*) .

public class Calculator{


public static void main(String[] args) {
int a=3,b=5,c=0;
char op='*';
switch(op){
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
default:
System.out.println("Invalid");

}
System.out.println("Answer = "+c);

5.4: switch case 9

c switch-case
,
switch(op) ] .
R^ (*)

uyc op

switch-case
R

Copyright Kasun De Zoysa 2011-2012

82 -

'

case '*'

e ,

'
case '*'
], c=a*b

R
=. e a b R^
c =.

i
] break , switch-case

=. e break

R

switch-case


]
System.out.println("Answer

"+c)

=.

u ' ] =.

l char op='*'
char op='+'

'
R
. e Answer =
8 u ' ]
b
. e


. e a

e R^
c

switch-case
case '+'

R
.

case '+'
] c=a+b
, a b
e c

, i
] break

switch-case
R


=.

, char op='*';
char op='-';

'
R
. e
u Answer = -2 p
=
.
uy ] c ]

, R
R^ , switch-case

p .

Copyright Kasun De Zoysa 2011-2012

83

5.3 do while
(do while Loop)
'

, 9R

R
R^ R^ if-else switch-case


l . '

c,

, 9R

R
R .
,


. do-while, while for


=.

e do-while
, '
, 9R

R
R^


R. R

' p ,


do-while
do-while (do-

while loop)
.

l 'p,

do-while

9R

, 1 n


. 9R
, do-while
R
.

do-while
9R
, 1 n


y . e e 9R
R

1 n
R

. e 9R

, 1 n
e
R
.

Copyright Kasun De Zoysa 2011-2012

84 -

'

do while

-1

-2

-3

-4

-n

9R

5.5: do-while
(do-while )

uy

3)


. (1, 2


, '

System.out.println()
e

] '
R

c 1 3

' ]

uy

, ' ]

R^ , e

R
]
. e


do-while

do-while =.

Copyright Kasun De Zoysa 2011-2012

85

public class PrintNumbers{


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<4);
}
}

5.6: do-while U 9 9V

'

c i=1 1 i

'
c ]. i
dowhile =. do while(i<5)

.

do
while
p

.
e

p .


, while
]

9R

] R

uy

System.out.println(i) i++

do-while


y ] .

System.out.println(i) i ' ]

i++
i eR =.

c System.out.println(i) , i

, e 1, ' ] =. i
] i++

Copyright Kasun De Zoysa 2011-2012

86 -

'

, i 2 =. R
while

] (i<4) 9R
=. c i 2 ,

(i<4) 9R

do-while ]
R
.

c System.out.println(i) R

, 2 ' ]

=. i
, i++

i 3 =. R

while
] (i<4) 9R
=. c i

3 (i<4) 9R
.

, do-while ]


. e i 3 ,
System.out.println(i) , 3 ' ]

=.
] i++
i 4 =.

c, (i<4) 9R

e do-while R
.
uy

c, do-while System.out.println()

l=, l p =

. e , 1 100 ] ]

i '
Rl= .

while ], (i<4) 9R
, (i<=100)

R^ p =
.
do-while 1 100 ] o

e '
R l= .

do-while 1 100

] o . l

e

o '
l 'p eR e e

Copyright Kasun De Zoysa 2011-2012

87

d.

public class AddNumbers{


public static void main(String[] args) {
int sum=0,i=1;
do{
sum+=i;
i++;
}while(i<=100);
System.out.println("Adddition ="+sum);
}
}

5.7: do-while U e

o e , '

i
sum=0 , 0 c

]. sum+=i
sum=sum+i
R

=
. c sum 0

i 1 sum+i
1 . sum
.

i
] i++
i eR =.
i 2 ,

i<=100 9R

] R
.
c, sum 1 i 2 sum+=i

2 . e, sum 2 . i
]

i++
i 3 =. i<=100 9R

R
.

Copyright Kasun De Zoysa 2011-2012

88 -

'


c, sum 2 i 3

sum+=i
sum 5 . i

] i++
i 4 =. c i<=100 9R

R
.

i<=100

9R

R
.
c

100

, e

eR e
=
. c
i sum e ,

c, 1

100 ] o e sum .
do-while

System.out.println

("Adddition ="+sum) , ' ]


=.

do-while

'

R
. c
do-while
R
, sum
' ]

p
. e do-while , ]

(100) R



.

i '

o

'
.
m
(m!)
1 m ] o d=.

e
, d .
'
, 5!
'
.

'
, int fac=1,i=1
,

1 , fac i
c

Copyright Kasun De Zoysa 2011-2012

89

]. i
do-while =. c

d
, sum+=i

, fac*=i
]
=

public class Factorial{


public static void main(String[] args) {
int fac=1,i=1;
do{
fac *=i;
i++;
}while(i<=5);
System.out.println("Factorial ="+fac);
}
}

5.8: U

fac*=i
fac=fac*i
R

=
. c fac 1

i 1 fac*i

1
. fac
.

i
] i++
i eR =.
i 2 ,

i<=5 9R

c ]
R
.
c, fac 1 i 2 fac*=i

2 . e, fac 2 . i
]

i++
i 3 =. i<=5 9R

R
.

Copyright Kasun De Zoysa 2011-2012

90 -

'


c, fac 2 i 3

fac*=i
fac 6 . i

] i++
i 4 =. c , i<=4 9R

R
.


]
, i<=5

9R

.
c 1 5 o d fac


=
. c, do-while
]

System.out.println("Factorial

="+fac)

,
' ] =.

5.3 while
(while Loop)
'

, 9R

R
R^, while

while .

l 'p, while c, 9R

. 9R

, 1 n
R

. 9R

, e
R R
.
e while c, , 1 n


e R
. e
e

R

, while
do-while

.

Copyright Kasun De Zoysa 2011-2012

91

while

9R

-1

-2

-3
..

-n

5.9: while
(while )

while
while uy

p
. 5.6 uy 'p, '

R
c 1 3 o ' ]
. ec , o ' ] R^
,
].

System.out.println(i)

c, e R

while


while =.
do-while
, while

c 9R
, y ]

c 9R

c =.

e while
c, 9R
,

Copyright Kasun De Zoysa 2011-2012


'

92 -

]

R R

. 9R

, ]

R

e
, do-while c, 9R

, ]

e R

l
.

public class PrintNumbersAgain{


public static void main(String[] args) {
int i=1;
while(i<4){
System.out.println(i);
i++;
}
}
}

5.10: while U 9 9V
l do while 1 100 ] o

e '
, while
.

,
1 100 ] o ' ]

'
while
.
o

o R 4.7

p .

o
, while
1 100
] o R
=.

'
'

y .

'
c, int i=1

Copyright Kasun De Zoysa 2011-2012

93


9 o c ] . i
]

while , ]
, 9R

] R
=.

public class OddNumbers{


public static void main(String[] args) {
int i=1;
while(i<=100){
if(i%2!=0)
System.out.println(i);
i++;
}
}
}

5.11: while 1 100 g U


9V
i uy

while
, if

=. if
i%2!=0 9R

] System.out.println(i) R

=.

c i 1 , i%2!=0 9R


. e System.out.println(i) R


1 ' ]

.

] i++
, i

2 '

R
' 9R


. e while

] 9R

]
R
.

c i 2 , i%2!=0 9R

if

],

Copyright Kasun De Zoysa 2011-2012

94 -

'

System.out.println(i) R

. e

]
i++
R
i 3 .

, i++
, if
]

. i
'

R
'

'p while
] 9R

.
i 100 while ]


R
=. while ] if

, o ' ] , i++

c i eR i =.

while i '
,
1 100 ] i

o e ,

while
'

R
.

'
'

public class AddEvenNumbers{


public static void main(String[] args) {
int sum=0,i=1;
while(i<=100){
if(i%2==0)
sum+=i;
i++;
}
System.out.println("Addition of even numbers = "+sum);
}
}

5.12: while 1 100 g i

Copyright Kasun De Zoysa 2011-2012

95

U e
o i o R if(i

%2==0) 9R
R l .

o = e sum+=i
R
. e

i '
i o

e ] p =
.

e '
] while 1 100 ]
o ,
i

if(i%2==0) 9R
,

o . e
i o

eR e e R^ sum+=i
.
c,
while
]

System.out.println ("Addition of

even numbers = "+sum) i


o e
' ] =.

5.4 for
(for Loop)
'

, 9R

R^

for
for (for loop) p
.

for , do-while while


.

for

R
.

c ,

for c

9R
,

eR

e
.
Copyright Kasun De Zoysa 2011-2012

96 -

'

l 'p for R
, e

c

. i

R

e

9R
=. 9R

for ]
R

for

=. i
R

, for

9R

=. 9R

] , for ]


. 9R

for
R
.

for

9R

-1

-2

-3

-n


R^

5.13: for
(for )

Copyright Kasun De Zoysa 2011-2012

uy

97

5.6 uy c ] 'p, '

R
c, 1 3 o ' ]
.
ec , o ' ]

R^ ,

System.out.println(i) R
]
.

c, e

for
for =.

public class PrintNumbersFor{


public static void main(String[] args) {
for(int i=1;i<4;i++){
System.out.println(i);
}
}
}

5.14: for U 9 9V
i uy
,

for(int

i=1;i<4;i++){} y

for
for =. for

, int i=1
1 i c ] . e

i<4 9R
c ] .

i
] .

R^ i++

'p for

c ].

for R

Copyright Kasun De Zoysa 2011-2012

98 -

'


e
, e

p
.
uy

] for

i ' . i
for


.
for R

,

] i<4

9R
=. e 9R
for
R
. e for ]

R
.

e c, System.out.println(i) , R
, i

e (1) ' ] =. i
for

i++
R


. e

i 2 =.

i
R

for

(i<4) 9R
=. c i 2 (i<4)

9R

.

for

]
R
.

c System.out.println(i) R

, 2 ' ] =. i
, for

i++
R
i 3
.

R

for

(i<4) 9R
=. c i 3 (i<4)

9R

for ]
R
.
e i 3 , System.out.println(i)
, 3 ' ]

=.
for

Copyright Kasun De Zoysa 2011-2012

99

i++
R

i 4
=.

i
R
for

(i<4) 9R
=.

c, (i<4) 9R

. e for R
.


, for
, do-while while

R
=
. do-while

while '
, for
.
l for
, 100 1
' ]

] o

R l= .

for

, y 'p

R^.

public class PrintNumbers100To1{


public static void main(String[] args) {
for(int i=100;i>0;i--){
System.out.println(i);
}
}
}

5.15: for 100 1 g U


9V

100 1 ] o , for


, int i=100
100 i
c
. for

(i>0) 9R

Copyright Kasun De Zoysa 2011-2012

100 -

'

i--
c 100
1 ' ]

=
.

for for-each

].

for-each

. , for-each ,

'[

c
.

5.5 R e (Nested Loops)


'

R
.

public class PrintSquare{


public static void main(String[] args) {
for(int i=1;i<4;i++){
for(int j=1;j<5;j++){
System.out.print(j);
}
System.out.println("*");
}
}
}

5.16: 9 e 9V

e '
o ' ]
=.
Copyright Kasun De Zoysa 2011-2012

101

1234*
1234*
1234*
i uy

for(int i=1; i<4; i++) {}

for(int j=1; j<5; j++){} ].

i 1 3 ]

R
=.

c
for for R. e

]
for(int j=1; j<5; j++)

{System.out.print(j);}

System.out.println("*") R
.
for(int j=1; j<5; j++){} , j
1 4 ] System.out.print(j)

=. e
c,
1234 o ' ]


. e
]

System.out.println("*") ' ]

=.
R
,
1234* , ' ] .
l o ' ]

'

0
01
012
0123

Copyright Kasun De Zoysa 2011-2012

102 -

'


'
, '
y
.

uyc for(int i=1; i<5; i++){}

for(int j=0; j<i; j++){}

].
i 1 4

]
R
=.
e


] for(int j=0; j<i; j++){}

R

. R

j
0 , j
i


c d. e

p i 1

R

e d. e j

' ]

System.out.println("") ,

=.

public class PrintTriangle{


public static void main(String[] args) {
for(int i=1;i<5;i++){
for(int j=0;j<i;j++){
System.out.print(j);
}
System.out.println("");
}
}
}

5.17: e9

,

p
] i 2

=. e j 0

Copyright Kasun De Zoysa 2011-2012

You might also like