Wednesday, November 20, 2013

Conditional Statement(1)



අපි කලින් පොස්ට් එකේදී operators use කරන විදිය කතා කරා මතක ඇති .මන් මේ පොස්ට් එකින් කියන්න හදන්නේ conditional ස්ටටෙමෙන්ට් use කරන විදිය තමය්

කලින් පොස්ට් එකේ දී කරපු programme run වෙන්නේ line පිලිවලට.නමුත් අපිට ඕනේ කියමු කෝ මේ run වෙන අනුපිලිවල වෙනස් කරන්න.ඒ කියනේ යම් ප්‍රතිපලයකට අදාල ස්ටටෙමෙන්ට් එක විතරක් run වෙන්න නැත්නම් යම් ප්‍රතිපලයක් කිහිප වාරයක් run කරව ගන්න වගේ වැඩ කරගන්න තමා Control statement කියල එඅව වගෙයක් ජාවා වල තියනේ.අපි දැන් බලමු මොනවද මේ Control statement කියල

ජාවා වලදී අපි විවිද Control statement use කරනවා ඒවා තමය් පහත දැක්වෙන්නේ

  • Selection statements
  • Loop statements
  • specific break and continue

මුලින්ම අපි කතාකරමු Selection statements ගැන

මේවත් ප්‍රදාන වශයන් කොටස් කිහිපයකට බෙදෙනව.ඒවා තමය්
1)if සහ if .... else ස්ටටෙමෙන්ට්

2) Nested if ස්ටටෙමෙන්ට් (if ස්ටටෙමෙන්ට් එකක් ඇතුලේ තව if ස්ටටෙමෙන්ට් එකක් use කරන්න එක )

3)switch ස්ටටෙමෙන්ට්

4)conditional operator use කරලා කරන Control statement
දැන් අපි බලමු if සහ if .... else ස්ටටෙමෙන්ට් use කරන්නේ කොහොමද කියල
if (booleanExpression)

{

statement(s);

}
මේ දක්වල තියනේ if ස්ටටෙමෙන්ට් එකක syntax use කරන ෆොර්මැට් එක තමය්.එක run වෙන්න විදිය flow chart එකකින් පහත  පෙන්නලා තියනවා .

booleanExpression කියන තැන දන්නා ඕනේ අර අපි කලින් කතාකරපු operators හරි boolean අගයක් තමය්.උදාහරණයක් වශයෙන් එතනට දන ස්ටටෙමෙන්ට් එකේ අවසන් අගය True හෝ False වන්න ඕනේ.එහෙම කීවට හරියට තේරුනේ න නේ ද?

අපි පොඩි programme එකක් ලියල බලමු.මේ programme එකින් වෙන්නේ යම් ශිෂයෙක් ලකුණු 50 ට වඩා වැඩිනම් pass කියල ප්‍රින්ට් කරන්න ඕනේ නැතිනම් අපි ප්‍රින්ට් කරන්න ඕනේ Fail කියල ප්‍රින්ට් කරන්න ඕනේ
import java.util.Scanner;//
class Example5{
      public static void main(String args[]){
            Scanner sc = new Scanner(System.in);
       
        System.out.println("Enter an integer : ");
        int input = sc.nextInt();
       
       if(input >50){
         System.out.print("Pass");
       }else{
         System .out.print("Fail");
       }
      }
}
මේ programme එකේ input >50 True වන වලාවට විතරයි pass කියන ප්‍රින්ට් comand එක run වන්නේ එක False වන හැම වෙලාවෙම run වෙන්නේ fail කියන ප්‍රින්ට් comand එක.මේ programme එකේදී input කියන veriable එකට කලින් පොස්ට් එකේ විස්තර කරපු විදියට user input එකක් තමි අරන් තියනේ.එක ගන්න විදිය දැන් අව්ලක් න නේ.

ඊළඟට අපි හිතමු අපිට programme එකක conditione එකකට වැඩියන් check කරන්න තියෙනවා කියල එතකොට මේ විදියට if ...else use කරන්න පුළුවන් .එක පහත flow chart එකකින් විස්තර කරලා තියනවා.පහත programme එක බලන්න
import java.util.Scanner;
class Example5{
      public static void main(String args[]){
            Scanner sc = new Scanner(System.in);
       
        System.out.println("Enter an integer : ");
        int input = sc.nextInt();
       
       if(input >=75){ //Line 1
         System.out.print("A");
       }else if(input >=65){ //Line 2
         System.out.print("B");
       }else if(input >=50){ //Line 3
         System.out.print("C");
       }else if(input >=35){ //Line 4
         System.out.print("S");
       }else{                 //Line 5
         System .out.print("Fail");
       }
      }
}
මේ programme එකත් කලින් programme එක වගේ තමය්.නමුත් පොඩි වෙනසක් තියෙනවා.line 1 වලදී input එක 75 ට වැඩි ද කියල check කරනවා ඊටපසේ line 2 දේ ලකුණු 65 ට වැඩි ද බලනවා.මේ දෙවෙනි line එක run වෙන්නේ line 1 false වොනුත් විතරයි හරියටම කිවොත් input එකේ අගය (75<input <=65) අතර තිබුණු විට විතරයි line 2 run වෙන්නේ.(එවැනි අවස්ථා වල if else ()තමය් use කරන්නේ).අනිත් line වලත් වෙන්නේ මේ සමානම process එකක් තමය්.දැන් තරුම් ගන්න පුළුවන් නේ.

දැන් අපි බලමු logical operators, condition එකක් ඇතුලෙදි කොහොමද use කරන්නේ කියල
import java.util.Scanner;
class Example5{
      public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
       
        System.out.println("Enter an integer 1: ");
        int input1 = sc.nextInt();
        System.out.println("Enter an integer 1: ");
        int input2 = sc.nextInt();
       
       
       if(input1 >=75 && input2>=75){//Line 1
         System.out.print("Two Subjects A Pass");
       }else if(input1 >=65 || input2>=65){//Line 2
         System.out.print("B");
       }else{
         System .out.print("Fail");
       }
      }
}


අපි දැන් මේ programme එකේ input 2 ක් ගන්නවා ඊට පස්සේ line 1 කොඉතිඔන් එකේ ඩි check කරන්න condition එක true නම් විතරයි Two Subjects A Pass ප්‍රින්ට් වන්නේ.මේ condition එකේ ටික්කක් අමුතු ගතියක් තියනවා. input1 >=75 && input2>=75 මේකේ && වලින් දෙපැත්තේ තියෙන condition දෙකම true නම් විතරයි condition එකට අදාල බෝද්ය් එක run වෙන්නේ එකක් හරි false නම් ඊළඟ condition එක check කරන්න programme එක යනවා .හරියට logical gate වල AND gate එක වගේ.line 2 කෙරන්නේ input1 >=65 || input2>=65 එකේ || දෙපැත්තේ තියෙන දෙකින් අවම වශයෙන් 1 වත් true වනවානම් conditon එකට අදාල body එක run වෙනවා.අපිට මේ logical operators එකකට වැඩි ගනනක් පවා programme එකකදී use කරන්න පුළුවන්.

උදාහරණයක් වශයෙන් (marks!=50 && marks1>50 && marks<100) වගේ use කරන්න පුළුවන් තව කලවෙමේත් &&,|| වගේ ඒවා යොදන්න පුළුවන්.එහෙනම් දැන් ඔයාල ලියන්න programme එකක් මේ wage problem එකක්  solve කරන්න

දැන් අපි බලමු කොහොමද කියල nexted if conditional ස්ටටෙමෙන්ට් එකක් ලියනේ කියල
class Example5{
      public static void main(String args[]){
            int age = 29;
             
            if (age < 13)
            {
              System.out.println("You are but a wee child!");
            }
            else if (age < 19)
            {
              System.out.println("You are no longer a child, but a budding teenager.");
            }
            else
            {
              if (age < 65)
              {
                System.out.println("You are an adult!");
              }
              else
              {
                System.out.println("You are now a senior, enjoy the good life friends!");
              }
              System.out.println("Also, since you are over the age of 19, you deserve a drink!");
      }
      }
}
බලන්න මේ programme එක මේක else එක ලඟට එනකම්ම කලින් programme දෙකේ විදියටම තමය් තියනේ.නමුත් else එක ඇතුලේ නැවත වරක් age verable එකේ අගය check කරන්න if ස්ටටෙමෙන්ට් එකක් පට්ටන්ගන්නවා එකිදී කලින් if එකේ machinisum එක වගේ තමා වැඩකරන්නේ කිසි වනසක් නැ.

අපි තව උදා programme ටික්කක් බලමු .
class Example5{
      public static void main(String args[]){
            int num=15;
            if ( num > 0 )      // Outer if
                    if ( num < 10 )  // Inner if
                      System.out.println( "num is between 0 and 10" ) ;
      }
}
මේකෙදි num එක 0-10 අතරද කියල check කරන්න use කරන්නේ nexted if condition එකක්
class Example5{
      public static void main(String args[]){
            int num=115;
            if ( num > 90 )
            {
              System.out.println( "You earned an A" ) ;
            }
            else
               if ( num > 80 )
               {
                  System.out.println( "You earned a B" ) ;
               }
               else
                  if ( num > 70 )
                  {
                     System.out.println( "You earned a C" ) ;
                  }
      }
}
මෙකිදී මුල්ම programme එකේ වගේ else දෙකක් ඇතුලේ නැවත අලුත් condition දෙකක් check කරන්න නැවත if එකක් ගන්නේ දල තියනවා.

දැන් අපි බලමු  switch ස්ටටෙමෙන්ට් ගැන

Switch කියන්නෙත් if වගේම selection ස්ටටෙමෙන්ට් එකක් තමය් .නමුත් තරමක් දුරට if වලට වඩා ටිකක් වෙනස් .එහෙම කිවේ if වල Boolean Condition එක ඇතුලේ අපිට පුළුවන් ගොඩක් දිග Boolean expression එකක් දෙන්න එනමුත් switch එකේ දි එහෙම කරන්න බැ දෙන්න පුළුවන් එක Boolean එකක් විතරයි .programme ටිකක් ගහනකොට තේරවි .

පහත programme එක බලන්න
import java.util.Scanner;
class Example5{
      public static void main(String args[]){
            Scanner sc = new Scanner(System.in);
       
        System.out.println("Enter an Month 1: ");
        int month = sc.nextInt();

        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
      }
}
මුලින්ම බලමු එක run වවෙන විදිය flow chart එකකින් 


මේ programme එකේ දී වෙන්නේ අපි month එක user input එකක් විදියට ගත්තම එකට අදාල month එක ප්‍රින්ට් වන එක තමා .අපිට මේක if දමලත් කලින් ලියපු programme වගේ කරන්න තිබුන නමුත් අඩු ස්ටටෙමෙට් ගානකින් ලෙසින් මේ programme එක ලියන්න අපිට switch use කරලා මේවිදියට පුළුවන් .switch use කරනකොට switch (month ) විදියට condition එක සලකන veriable එක ගන්න ඕනේ.ඊටපස්සේ ඒ veriable එකට ලැබෙන්න තියෙන එකක values ,(case 1) ඇතුලෙදි consider කරලා ඒ case එක හරිනම් වෙන්න ඕනේ දේ ලියන්න ඕනේ හැම case එකක් අවසානයටම break ස්ටටෙමෙන්ට් එක දන්න ඕනේ.නැත්තනම් අපි හිතමු case 5 කියන්න එක true වොනුත් එක run වෙලා programme එක නවතින්න ඕනේ break එක  නැත්තනම්  වෙන්නේ එක run වුනාට පස්සේ එතිරි ඒවත් run වෙන එක break එකින් එක වැඩියෙන් run වෙන එක නවත්තනවා .තව switch එක අන්තිමට තියනවා default කියල case එකක් එකින් වෙන්නේ අර දීපු case ඔක්කොම වැරදිනම් default case එක run වෙන එක.එකින් පස්සේත් break statement එක යොදන්න මතක තියාගන්න.අපිට case ඇතුලේ nomal විදියට වැඩ කරන්න පුළුවන් .

දැන් ඔයගොල්ලෝ programme එකක් ලියන්න switch use කරලා අපි if use කරලා ලකුණු වලට අදාල grade දෙන්න ලියපු programme එකට සමාන වෙන විදියට.

 දැන් අපි බලමු conditional operator use කරලා කරන Control statement එකක් ලියන්නේ කොහොමද කියල

if (a > b) {
  max = a;
}
else {
  max = b;
}
මේ programme එක conditional operator use කරලා ලින්නේ කොහොමද කියල
max = (a > b) ? a : b;
දැන් ඔයගොල්ලෝ programme එකක් ලියන්න conditional operator  use කරලා ලකුණු වලට අදාල grade ලබාගන්න

අපි ඊළඟ පොස්ට් එකින් බලමු loops use කරන හැටි කොහොමද කියල......


No comments: