Monday, January 20, 2014

Java Tutorial 7

Java Tutorial 7(Control Flow and Condition) part 2

If statement အေၾကာင္းေျပာၿပီးတဲ့ေနာက္ switch statement အေၾကာင္းဆက္ေျပာပါမယ္။ switch ဟာ if ႏွင့္နည္းနည္းဆင္ပါတယ္၊ “if…else if…else if” ႏွင့္သေဘာတရားျခင္း ဆင္ပါတယ္။ ေအာက္ကနမူနာကို ရွင္းျပပါမယ္။

“SwitchSample.java”

public class SwitchSample {

public static void main(String[] args) {

 

int month = 8;

switch (month) {

case 1:  System.out.println("January");       break;

case 2:  System.out.println("February");      break;

case 3:  System.out.println("March");         break;

case 4:  System.out.println("April");         break;

case 5:  System.out.println("May");           break;

case 6:  System.out.println("June");          break;

case 7:  System.out.println("July");          break;

case 8:  System.out.println("August");        break;

case 9:  System.out.println("September");     break;

case 10: System.out.println("October");       break;

case 11: System.out.println("November");      break;

case 12: System.out.println("December");      break;

default: System.out.println("Invalid month"); break;

}

System.out.println(monthString);

}

}

 

ဒီစာသားကိုၾကည့္ပါ “switch(month)” ၊ အဓိပၸါယ္ကေတာ့ month ရဲ႕တန္ဖိုးကိုစစ္ေဆးတာပါ။ “case 1:” ဆိုတဲ့စာသားကေတာ့ month ရဲ႕တန္ဖိုး ၁ ျဖစ္ခဲ့ရင္ ဆိုတဲ့အဓိပၸါယ္ရပါတယ္။ month ရဲ႕တန္ဖိုးကိုစစ္ေဆးၿပီး 1 ျဖစ္ရင္ “case 1:” ရဲ႕အေနာက္ကကုဒ္ေတြ run ပါမယ္၊ break ထိေရာက္ေအာင္ run မွာပါ။ တကယ္လို႔တန္ဖိုးက 2 တို႔ 3 တို႔ျဖစ္မယ္ဆိုရင္ “case 2:” တို႔ “case 3:” တို႔ စသည္ျဖင့္ သက္ဆိုင္ရာ case ေနာက္ကကုဒ္ကို run မွာပါ။ ဘယ္ case နဲ႔မွမညီဘူးဆိုရင္ default ေနာက္ကကုဒ္ကို run မွာပါ၊ default ကိုမထည့္လည္းရပါတယ္။ default မထည့္ရင္ေတာ့၊ ဘယ္ case နဲ႔မွမညီတဲ့အခါ ဘာအလုပ္မွ လုပ္မွာမဟုတ္ပါဘူး။

If ႏွင့္ switch ဟာနည္းနည္းဆင္တယ္ဆိုေတာ့ ဘယ္ေနရာမွာ ဘယ္ဟာကိုသံုးရမလဲဆိုတာ မိမိဘာသာစဥ္းစားဆံုးျဖတ္ရမွာပါ။ if ကေတာ့တန္ဖိုးအမ်ိဴးမ်ိဴး expression အမ်ိဴးမ်ိဴးကို စစ္ေဆးႏိုင္ေပမဲ့၊ switch ကေတာ့တန္ဖိုးတစ္ခုတည္း(single value) ကိုပဲစစ္ေဆးႏိုင္ပါတယ္။ switch မွာဆန္းျပားတဲ့ အခ်က္တခ်က္ရွိပါတယ္၊ break; ဆိုတဲ့ကုဒ္ပါ။ case တစ္ခုကိုအလုပ္လုပ္ၿပီးခ်ိန္ break; ကိုေရာက္တဲ့အခါ ေအာက္က case ေတြကိုဆက္မလုပ္ပဲနဲ႔ switch ရဲ႕အျပင္ကိုထြက္သြားပါတယ္။ break; ကိုထည့္မေရးလည္းရပါတယ္၊ ထည့္မေရးရင္ ေအာက္ကကုဒ္ေတြကို ဆက္လုပ္ပါတယ္၊ break; ဆိုတာ ေရွ႕ဆက္မသြားနဲ႔ရပ္လိုက္ေတာ့ လို႔အဓိပၸါယ္ရပါတယ္။ အေပၚကနမူနာ program ကို break; ဆိုတဲ့ကုဒ္ေတြ ျဖဳတ္ၿပီး run ၾကည့္ပါ။

 

While and Do-While Loops

While ဆိုတာ condition မွန္သေရြ႕ အလုပ္ကိုဆက္လုပ္ေနမွာပါ၊ အေျခအေနမွန္ေနသမွ် ကာလပတ္လံုး တြန္႔ကြင္းထဲကကုဒ္ေတြ ထပ္ကာထပ္ကာဆက္တိုက္ run ေနမွာပါ။ သူ႔ရဲ႕ structure ကဒီလိုပါ၊ “while(condition) { //statements; }” ဒီပံုစံနဲ႔ေရးရပါမယ္။ while ကိုသံုးရင္သတိထားရပါမယ္၊ ဘာေၾကာင့္လဲဆိုေတာ့ condition မွန္ေနရင္ အဆက္မပ်က္ run ေနမွာျဖစ္တဲ့အတြက္ ဘယ္ေတာ့မွမရပ္ပဲ run ေနတာမ်ိဴးျဖစ္တက္ပါတယ္။ ဒါေၾကာင့္ တစ္ခ်ိန္ခ်ိန္မွာ condition မွားသြားတာမ်ိဴးျဖစ္ေအာင္ ေရးထားသင့္ပါတယ္။ ေအာက္ကနမူနာကို run ၾကည့္ပါဦး။

“WhileSample.java”
class WhileSample {

     public static void main(String[] args){

          int count = 1;

          while (count < 11) {  //when condition is true

              //this block is running

               System.out.println("Count is: " + count);

               count++;

          }

     }

}


ဒီနမူနာမွာ “while(count < 11)” ဆိုတာ၊ count တန္ဖိုးဟာ 11 ေအာက္ငယ္ေနသေရြ႕ တြန္႔ကြင္းထဲကကုဒ္ေတြကို run မွာပါ။ တြန္႔ကြင္းထဲမွာ count ကို ၁ တိုးတဲ့ကုဒ္ပါဝင္ပါတယ္၊ ဒါမွ count ရဲ႕တန္ဖိုးဟာ တစ္ခ်ိန္မွာ 11 ႏွင့္ညီၿပီး သို႔မဟုတ္ 11 ထက္ႀကီးၿပီး condition ဟာမွားသြားပါလိမ့္မယ္၊ အဲဒီအခ်ိန္မွာ while ဟာရပ္တန္႔သြားမွာပါ။

Do-while ဆိုတဲ့ statement တစ္မ်ိဴးလည္းရွိပါေသးတယ္။ သူက while နဲ႔ဆင္တူပါတယ္၊ ဒါေပမဲ့ကြာျခားခ်က္ရွိပါတယ္။ while ရဲ႕ flow ကဒီလိုရွိပါတယ္၊ while ကအျမဲတမ္း condition ကိုအရင္စစ္ေဆးပါတယ္၊ ၿပီးမွ condition မွန္မွအလုပ္ဆက္လုပ္ပါတယ္။ do-while မွာက condition စစ္တဲ့ကုဒ္ဟာ တြန္႔ကြင္းရဲ႕ေအာက္မွာရွိပါတယ္၊ ဒီေတာ့ condition မွန္သည္ျဖစ္ေစ မမွန္သည္ျဖစ္ေစ ပထမဦးဆံုးတစ္ေခါက္ေတာ့ သူက run ပါတယ္။ သူ႔ကိုဒီလို “do { statement; } while(condition);” ေရးရပါတယ္။ while ႏွင့္ do-while ကိုႏွိဳင္းယွဥ္ျပထားတဲ့ ေအာက္ကနမူနာကို စမ္းၾကည့္ပါ။

“WhileNDo.java”
class WhileNDo {

     public static void main(String[] args){

          int count = 1;

          while (count > 2) {

            System.out.println(“while”);

          }

          do {

             System.out.println(“do while”);

          } while(count > 2);

     }

}

 

For Loop
While တို႔ Do-while တို႔ဆိုတာ condition မွန္ေနသေရြ႕ run မွာပါ၊ ဘယ္ႏွစ္ႀကိမ္ loop ပတ္မလဲဆိုတာ အေရအတြက္ အတိအက် မသိရပါဘူး။ ဒါေပမဲ့ for ကေတာ့ အႀကိမ္အေရအတြက္ အတိအက်ရွိပါတယ္။ “for(initialization; termination; increment) { statement; }” ဆိုတဲ့ပံုစံအတိုင္းေရးရပါတယ္။ “initialization” ဆိုတဲ့အပိုင္းက စမွတ္ကိုေျပာတာပါ၊ “termination” ဆိုတာဆံုးမွတ္ပါ၊ “increment” ဆိုတာတန္ဖိုးကိုတိုးတဲ့အပိုင္းပါ။

“ForSample.java”

class ForSample {

     public static void main(String[] args){

          for(int i=1; i<11; i++){

               System.out.println("Count is: " + i);

          }

          for(i=1; i<5; i++){

               System.out.println("Now count is lower than five”);

          }

     }

}


ဒီနမူနာမွွာ for ရဲ႕ပထမပိုင္းမွာ variable တစ္ခုေၾကာ္ျငာၿပီး ထည့္ရပါမယ္၊ ေၾကာ္ျငာၿပီးသား variable ကိုထည့္လည္းရပါတယ္၊ သူက loop ရဲ႕စမွတ္ပါပဲ။ for ရဲ႕အလယ္ပိုင္းက condition ပါ၊ ဒီ condition မွန္သေရြ႕ loop ပတ္ေနမွာပါ၊ သူကို loop ရဲ႕ဆံုးမွတ္လို႔ ေျပာလို႔ရပါတယ္။ for ရဲ႕ေနာက္ဆံုးပိုင္းကေတာ့ variable ကိုတန္ဖိုးေျပာင္းတဲ့ အပိုင္းပါ၊ for ရဲ႕စမွွတ္ျဖစ္တဲ့ variable ကိုဆံုးမွတ္ေရာက္ေအာင္ တန္ဖိုးေျပာင္းေပးတဲ့ အပိုင္းပါ။ ဒီကုဒ္ “for(i=1; i<5; i++)” မွာ i ဟာ 1 ကေနစပါတယ္၊ “i<5” ဆိုတဲ့ condition မွန္ေနသေရြ႕ ဒီ loop ကိုပတ္ပါမယ္၊ “i++” ဆိုတာ loop တစ္ခါပတ္ၿပီးတိုင္း i ကို 1 တိုးပါတယ္။


Branching Statements

ဒီ while တို႔ for တုိ႔ဆိုတဲ့ loop ေတြကိုသံုးတဲ့အခါ၊ ဒီ loop ေတြအလုပ္လုပ္ေနခ်ိန္မွာ run ေနတာကိုရုတ္တရက္ ရပ္တန္႔ေစခ်င္ရင္ break ဆိုတဲ့ကုဒ္ကိုသံုးရပါမယ္။ “break;” လို႔ေရးလိုက္ရင္ loop ဟာဆက္မ run ေတာ့ပဲရပ္တန္႔သြားပါတယ္။ “continue” ဆိုတဲ့ကုဒ္ကေတာ့ loop ကိုဆက္ run ခိုင္းတာပါပဲ။

Previous : Java Tutorial 6
Next : Java Tutorial 8

2 comments:

  1. […] Java Tutorial 7Microsoft fixes Surface Pro 2 problems [ battery problem and sleep problem ]Samsung Galaxy S5 […]

    ReplyDelete
  2. […] Java Tutorial 8breaking news for master thesis 20th batch of ucsyJava Tutorial 7Microsoft fixes Surface Pro 2 problems [ battery problem and sleep problem ]Samsung Galaxy S5 […]

    ReplyDelete