June 2014 - Archieve

Under the hood articles from the past.

Java Jump Statement

මේවා උපකාරී වනුයේ කිසියම් control structure එකක් පාලනය කිරීම සඳහාය.

උදා: කිසියම් loop එකක මැදදී එය නැවත්වීමට අවශ්‍ය වුවහොත් මෙම jump statement යොදා ගත හැක.


1. break
2. continue
3. return

1.break
ex:
class Break {
    public static void main (String args[]) {
    for ( int i=0 ; i<10 ; i++ ) {
    if (i == 5) {
    break;     // "if is equal to 5 terminate the loop"
    }
    System.out.print( i + " " );
    }
}
}

මෙහිදී සිදු වන්නේ ක්‍රමයෙන් වැඩි වීගෙන යන i අගය 5 ට සමාන වූ විගස loop එක නවත්විමයි. break keyword එකෙන් විධාන කරනුයේ
loop එකෙන් පිටතට යාමටයි.එනම් loop එක නවත්විමයි,
2. continue
ex:
class Continue {
    public static void main (String args[]) {
    for ( int i= 0; i<10; i++) {
    if (i==5) {
    continue;      //     " if   'i'  is equal to 5 ignore  'i'   and   continue"
    }
    System.out.println(i + " ");
    }
    System.out.println( "###");   
    }
    }

මෙහිදී සිදු වන්නේ ක්‍රමයෙන් වැඩි වීගෙන යන i අගය 5 ට සමාන වූ විට continue keyword එකට යටින් ඇති කිසිදු statement එකක් execute ]
නොකර Loop එකෙහි ඊළඟ step එකට පැනීමයි.එනම් i හි අගය 6 ට අදාල iteration එක පටන් ගැනීමයි.මෙහිදී 5 යන අගය console
එකෙහි මුද්‍රණය නොවීමට හේතුව එයයි.

Java Iteration loops

නැවත නැවත සිදු කිරීම (Loop)

භාවිතා වන අවස්ථා.
1. කිසියම් array  එකක් තුල ඇති element (අවයව ) එකින් එක පිලිවෙලින් පරික්ෂා කිරීම.
2. විවිධ algorithm නිර්මාණයේදී.
3. කිසියම් දත්ත සමුහයක් විශ්ලේෂණය කිරීමේදී.

Loop වර්ග.
1. while
2. do-while
3. for
4. for-each

1.while Loop

syntax
       while (expression) {
              // do something
       }

while loopඑකක් යොදා ගනුයේ loop එක කොපමණ වාර ගණනක ක්‍රියාත්මක විය යුතුද යන්න අනුමාන කල නොහැකි අවස්ථා වලදීය.

2. do-while loop

syntax
       do {
              //do some thing
       } while (expression) ;

මෙහිදී expression එක පරික්ෂා කිරීම සිදු කරන්නේ statement execute කිරීමෙන් පසුවය.

3. for loop

syntax
       for ( declaration & initialization ; condition ; iterative statement ) {
       // statement 1;
       // statement 2;
       }      

 සාමාන්යෙන් for loop එක භාවිත කරනුයේ loop එක ක්‍රියාත්මක වන වාර ගණන දන්නා විටදීය.
4. for-each loop

syntax
       for (element : arrey ) {
       // do some thing
       }

මෙහිදී දෙන ලද arrey එකක් තුල සියලු අවයව (elements) එකින් එක ලබා ගැනීම කල හැකිය. දී ඇති element නම් variable එක තුලට සෑම  iteration එකක දීම value ලබා දීම සිදු කරනවා .

Java Selection Statement

සරළව කියනවනම් මේ තමයි ජාවා වල Logic පටන් ගන්න තැන.
ගොඩාක් අයට අමාරු වෙන්නේ මෙතන.
සමහර අය මේක කටපාඩම් කරනවා.
නමුත් හරිම Concept එක ඔලුවට දාගත්තට පස්සේ කිසිම ප්‍රශ්නයක් නැහැ.
මුලදී මටත් අමාරු වුනා. නමුත් කාලයත් එක්ක Practical කරනකොට හොඳට Train වුනා.

O/L සහ A/L වලදී මේ පාඩමේ Theory එක හොඳටම කථා කරනවා.
ඒ නිසා ඒ ළමයින්ට ඉතා පහසුවෙන් මේ දේ වටහාගනිවී යයි සිතනවා.



හොඳයි අපි මෙතැනදී Statement 6ක් සමබන්ධව කථා කරනවා.


1. if
2. if-else
3. Nested-if
4. if-else-if  (if-else ladder)
5. switch
6. Ternary operator

1.if statement 

Syntax 

       if (condition) {
              // perform Task 1        
              }
condition එක true නම් if block තුල පවතින statement execute කරයි.

 2. if-else statement

syntax
       if (condition) {
              //perform task 1
              } else {                   
                //perform Task 2       
              }
if condition true නම් else block තුල පවතින statement එලෙසම පවතියි.
if condition false නම් else block තුල පවතින statement execute කරයි

3. Nested if
syntax
       if (condition 1) {
          if (condition 2) {
              // perform Task 1                             
       } else {
              // perform task 2                            
       }
       } else {                          
              // perform task 3
       }


condition 1   true   නම්       Task 1 execute කරයි
condition 2   true  නම්

condition 1   true   නම්       Task 2 execute කරයි.
condition 2   false  නම්

condition 1   false   නම්       Task 3 execute කරයි.
4. if-else-if    (if-else ladder)

syntax
       if (condition 1) {
              //perform Task 1         
         } else  if (condition 2)  {
              //perform Task 2         
         } else if (condition 3)   {
              //perform Task 3
         } else {
              //perform Task 4  
         }

මෙහිදී condition 1 true නම් perform Task 1 (statement 1) execute කරයි.
condition 2 true නම් perform Task 2 (statement 2) execute කරයි.
condition 3 true නම් perform Task 3 (statement 3) execute කරයි.
condition 1, 2, 3 හෝ true නොවේ නම් Task 4 execute වේ.

5. Switch statement

class Switch {
       public static void main (String args[]) {
//    support only these data type   " byte, short, char, int & enumeration "
       int week=3;                      
       String output;
       switch (week) {
       case 1 : output= "sunday" ; break;
       case 2 : output= "Monday" ; break;
       case 3 : output= "Tuesday" ; break;
       case 4 : output= "Wednesday" ; break;
       case 5 : output= "Friday" ; break;
       case 6 : output= "Saturday" ; break ;
       default : output= "Invalid day of week !";
       }    // end of the switch statement
       System.out.println (output);
}
}
මෙහිදී break keyword එක යොදා තිබෙන්නේ condition එක true වූ විට
switch block එකෙන් ඉවතට යාමටයි. default යටතේ දී ඇති statement
එක execute වනුයේ ඉහත සඳහන් කිසිඳු case එකක් සමග දී ඇති values
නොගලපුනහොත් පමණි.

6. Ternary operators
syntax

result = condition  ?  value A  : value B

මෙහිදී සිදු වන්නේ දී ඇති condition එක true නම් result සඳහා value A
condition false නම් value B assign කිරීමයි.

Java Boolean Logical Operators

Boolean Logical Operators

Operators
Name
&
Logical AND
|
Logical OR
^
Logical XOR
&&
Short Circuit AND
||   
Short Circuit OR
!
Logical NOT
&=
AND Assignment
|=
OR Assignment
^=
XOR Assignment
==
equal to
!=   
Not Equal to
? :
Ternary operator(if-then-else)




A
B
A &B
A |B
A ^B
A &&B
A ||B
True
True
true
True
false
True
true
True
False
False
true
True
False
True
False
True
False
True
True
False
True
false
false
False
false
False
false
False

Java Relation Operators


Relation Operators (result is always a Boolean data type)

Boolean Data Type එකක් යනු 1 හෝ 0 යි.
මෙය True False ලෙසද අර්ථකථනය කළ හැක.


a=18 ; b=21; c=21 ;


Operators
Operator name
Usage
result
==
Equal to
a == b
False
!=
Not equal to
b != c
False
Greater than
b > c
False
Less than
a < b
True
>=
Greater than or equal to
b >= c
True
<=
Less than or equal to
b <= a
false



Ex:
      
Class Javaguru1{
 public static void main(String args[]){
   int a=18; int b=21; int c=21;
       System.out.println("Value of a    : " + (a) ); 
       System.out.println("Value of b: " + (b) );    
       System.out.println("Value of c    : " + (c) ); 
       System.out.println("a == b : " + (a==b) );    
       System.out.println("b != c : " + (b !=c) );    
       System.out.println("b > c  : " + (b > c ) );  
       System.out.println("a < b  : " + (a < b ) );  
       System.out.println("b >=c  : " + (b >=c ) );  
       System.out.println("b <=a : " + (b <=a ) );
 }
}

Java Operators

Java Operators - ජාවා කර්ම

සාමානය ගණිතය තමයි මෙතැනදී සාකච්චා වෙන්නේ.
සමහර අවස්ථා වල වෙනස් කම් තිබුනට සම්පුර්ණ ගනිතයමයි කිව්වොත් නිවැරදියි.


උදාහරණයක් ලෙස Variable 3 ක් Define කරගෙන ඒවාට Values Initialize කරගන්නවා. 



int a=14;  
int b=8;
int c;

Arithmetic Operators

Operators
Operators name
usage
result
+
Addition
C = a + b
C = 22
-
Subtraction 
C= a - b
C = 6
*
Multiplication      
C = a * b
C = 112
/
Division
C = a / b
C = 1
%
Modulus
C = a % b
C = 6
++
Increment
C = a++
C = 15
--
Decrement
C = b--
C = 7

Assignment Operators
Operators
Operators name
usage
result
+=
Addition  Assignment     
a+= b
a = 22
-=
Subtraction  Assignment  
a-= b
a = 6
*=
Multiplication  Assignment
a*= b
a = 112
/=
Division  Assignment
a/= b
a = 1
%=
Modulus  Assignment     
a%= b
a = 6


                                                
Ex: Increment & Decrement
int x=10;
x++;
SOP(x);
int x=10;
++x;
SOP(x);
int x=10;
SOP(x++);
SOP(x);
int x=10;
SOP(++x);
SOP(x);
Answers



11
11
11
11
10
11
 
SOP යනු කෙටියෙන් System.out.println(); කියන එකයි.