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 කිරීමයි.

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment