Thursday, September 6, 2012

java selections ‍දෙ‍ව‍න ‍කො‍ට‍ස


java  language ‍කේදීබාවිතා selection  ගැපිහුගිපාමෙන්‍ ‍තාලා.‍නම්‍ if-else  හා  Conditional  operator ‍වේපිතිරි Selection  හාබාවිතා switch   statement,  nested  if ,   if  else  if ‍ගැනිමු. ‍(මෙ post  ටිකක්‍ leate  ‍‍නාගාටුයි.‍ගේ lapa  පොඩ්‍‍ඩක්‍ ‍වුල්‍ ‍ගියායියාලුනේ )

Switch   Statement
 පිජාවාතුදී  selection ‍හාබාවිතාSpecial  ක්යක්‍ ‍මා  switch  statement ‍මේවිශේයෙන්‍‍තිගන්‍‍ මෙ switch   statement ‍වැරන්‍‍නෙ int,byte,short,char සහ enumeration  data types ‍හායි.
පිමු code  run  ‍‍.‍වත්‍ ‍දෙයක්‍ ‍පිමේහන්‍ ‍ codes ‍යාගොඩක්‍ practice  රන්‍‍. logic  තේරුම්‍ ‍ගෙයාපුලුවන්‍ ‍රම්‍ code  ලියන්‍‍.  logics ‍මන්‍ use  රන්‍‍රන්‍‍ගොඩක්‍ expert  වෙතියි.

CODE:-

public class switchStatement {
  public static void main(String[] args) {
        int a=2;
        String out;
        switch (a){
        case 1 :out="january";
        break;
        case 2 :out="february";
        break;
        case 3 :out="March";
        break;
        case 4 :out="April";
        break;
        case 5 :out="May";
        break;
        case 6 :out="june";
        break;
        case 7 :out="july";
        break;
        case 8 :out="August";
        break;
        case 9 :out="September";
        break;
        case 10 :out="October";
        break;
        case 11 :out="November";
        break;
        case 12 :out="December";
        break;
        default : out="wrong input";
        }
//End Switch statement
        System.out.println(out);
    }
}
මෙහිදී break  key   word ‍යොදානුබන්‍‍නේ condition    true ‍වූවිනක්‍ switch   block ‍කෙන්‍ ‍වත්‍ ‍වීයායි .‍ default  ගින්‍ ‍සිදුවන්‍‍නේ  case ‍තුකිසියක්‍ ‍දීති  input ‍නොගැපේනම්‍ default ‍තේති statement  ක්රියාත්‍‍වීයි.

Out Put:-


  Nested  if
මෙ code  ගින්‍ ‍පැදිලිරන්‍‍නම්.
1)       If  (condition1)  {  
2)          if(condition2)    {  
3)       //Peform Task1  
4)        }
5)         else{  
6)       //Peform Task2  
7)        }  
8)       }
9)       Else {  
10)    //Peform Task3  
11)    }  

මෙහිදීසිදුවන්‍‍නේ‍ if   statements ‍කිහියක්‍ ‍ගොනුබාවිතා ‍‍කිරීයි  නම්‍ ‍ condition   1 true  විදෙ if  තුමන්‍ ‍යිහිදී condition   2  true ‍හොත්‍ task   1 ‍සිදුයි false  නම්‍ task   2 ‍වෙයිමු condition   1  false ‍හොත්‍ task   3 ‍සිදුයි. ‍ප‍හ‍ත ‍දක්‍‍වා ‍ඇ‍ති java   code ‍එ‍ක ‍ම‍ගින්‍ ‍වැ‍ඩි‍දු‍ර ‍අද්‍ය‍න‍ය ‍ක‍රන්‍‍න
 
Code:-

ublic class nestedIf {
   
    public static void main(String[] args) {
        int marks=3;
       
        if(marks>=35){
            if(marks>=75){
                System.out.println("Student has verry good pass");
            }
            else {
                System.out.println("Student has good pass ");
            }
        }
        else {
            System.out.println("Student has'nt  pass ");
        }

    }

}
 
Out Put:-




     IF, Else If 

මෙ‍ම‍ගින්‍ ‍සි‍දු ‍ක‍ර‍නු‍යේ ‍ප‍ල‍ව‍න if  ‍තු‍ල ‍ඇ‍ති condition   1 false  ‍වු‍ව ‍හොත්‍ ‍ත‍වත්‍  if  Condition ‍කි‍හි‍ප‍යක්‍ ‍යෙ‍දී‍ම‍යි.
‍මෙ‍ය ‍‍ප‍හ‍ත java   code ‍එ‍ක ‍ම‍ගින්‍ ‍දැක්‍‍වේ.
1)              If (condition 1) {
2)       //task 1
3)       }
4)       Else if (condition 2) {
5)       //Task 2
6)       }
7)       Else if (Condition3) {
8)       //Task 3
9)       }
10)    Else {
11)    //Task 4
12)    }

Code:-

public class ifElseIf {

    public static void main(String[] args) {
        int marks=67;
        String grade;
       
        if(marks>=75){
            grade="A";
        }
        else if(marks>=65){
            grade="B";
        }
        else if(marks>=50){
            grade="C";
        }
        else if(marks>=35){
            grade="S";
        }
        else {
            grade="F";
        }
       System.out.println("Student grade is "+grade);
    }

}
 


Out Put:-

හො‍ද‍යි java   selection ‍පි‍ලි‍බ‍ද‍ව ‍ක‍තා ‍ක‍ර ප්‍ර්‍මා‍න‍වත්‍ ‍යා‍ලු‍ව‍නේ. ‍අ‍පි ‍මී‍ල‍ග ‍පා‍ඩ‍මෙන්‍ ‍ක‍තා ‍ක‍රන්‍‍නෙ java   Loops ‍ගැ‍න ‍එ‍ම ‍පා‍ඩ‍ම ‍ඉක්‍‍ම‍නින්‍ ‍බ‍ලා‍පො‍රොත්‍‍තු ‍වෙන්‍‍න... 

By Manoj Priyankara                                                               
  

No comments:

Post a Comment