အခြေခံ ကွန်ပျူတာပရိုဂရမ် ရေးသားနည်း - Conditionals
Conditionals
နိစ္စဓူဝ ကျွန်တော်တို့တတွေဟာ ကိစ္စကြီးငယ်တွေကို အခြေအနေနဲ့ အချိန်အခါအလိုက် ရွေးချယ်မှုတွေကို ပြုလုပ်ကြရပါတယ်။
သွားတိုက်ဆေးလိုပစ္စည်းမျိုး ဝယ်ယူတာကစလို့ ကွန်ပျူတာ မိုဘိုင်းလ် အိမ်ခြံမြေ အစရှိတဲ့အရာတွေထိ အခြေအနေနဲ့ အချိန်အခါအလိုက် ရွေးချယ်ဆုံးဖြတ်ဝယ်ယူကြရပါတယ်။
လေဆာ မကျန်တော့ရင် cold gate ဝယ်မယ်ဆိုတဲ့ ဆုံးဖြတ်ချက်မျိုး။ Macbook Air မရခဲ့ရင် Macbook Pro ယူမယ်။ Macbook Pro လည်း မကျန်တော့ရင် Lenovo ဝယ်ယူမယ် စသဖြင့် ဆုံးဖြတ်ကြရလေ့ရှိပါတယ်။
ကွန်ပျူတာပရိုဂရမ်တွေ ရေးသားရာမှာတော့ Control Flow Statements တွေကို အသုံးပြုပြီး ရွေးချယ်မှုတွေကို ပြုလုပ်လို့ရတယ်။
If Statement
If statement ကို တစ်ခုတည်းသော ရွေးချယ်မှု ပြုလုပ်ဖို့ အတွက် အသုံးပြုတယ်။ ဉပမာ Netflix မှာ Spider-Man ဇာတ်ကား ရှိရင်ကြည့်မယ်ဆိုတဲ့ ဆုံးဖြတ်ချက်မျိုး။
Syntax :
if(condition) {
Statements(s);
}
Condition ဟာ true ဆိုပြီး return ပြန်ရင် if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။ condition ဟာ false ဆိုရင်တော့ if block ရဲ့ အပြင်ကို ထွက်သွားတယ်။
Example :
if(movie.equals(“Spider-Man”)) {
watchOnNetflix();
}
Example of If Statement
public class IfStatement {
public static void main(String args[]){
int num=70;
if( num < 100 ){
System.out.println("Number is less than 100");
}
if( num > 100 ){
System.out.println("Number is greater than 100");
}
}
}
Nested If Statement
Nested If statement ကို ရွေးချယ်မှုအတွင်း နောက်ထပ် ရွေးချယ်မှု တစ်ခု ပြုလုပ်ရာမှာ အသုံးပြုတယ်။ ဉပမာ Netflix မှာ Spider-Man ဇာတ်ကား ရှိရင်ကြည့်မယ်ဆိုတဲ့ ဆုံးဖြတ်ချက်မျိုး။
Syntax :
if(condition1) {
Statements(s1);
if(condition2) {
Statements(s2);
}
}
Example :
if(movie.equals(“Spider-Man”)) {
watchOnNetflix();
if(movie.quality(“FullHD”)){
selectFullHD();
}
}
Example of Nested If Statement
public class NestedIfStatement {
public static void main(String args[]){
int num=70;
if( num < 100 ){
System.out.println("Number is less than 100");
if(num > 50){
System.out.println("Number is greater than 50");
}
}
}
}
If-Else Statement
If-Else statement မှာ if ရဲ့ condition ဟာ true ဖြစ်ခဲ့ရင် if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။ If ရဲ့ condition ဟာ false ဖြစ်ခဲ့ရင် else block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။
ဉပမာ Coca Cola မရခဲ့ရင် ဘာပဲဖြစ်ဖြစ်ရှိတာ သောက်မယ် ဆိုတဲ့ ရွေးချယ်မှုမျိုး။
Syntax :
if(condition) {
Statements(s);
} else {
Statements(s);
}
For Example :
if(num > 0) {
System.out.println(“Positive Number”);
}else {
System.out.println(“Zero or Negative Number”);
}
Example :
if(movie.equals(“Spider-Man”)) {
watchOnNetflix();
}
Example of If-Else Statement
public class IfElseStatement {
public static void main(String args[]){
int num=120;
if( num < 50 ){
System.out.println("num is less than 50");
}
else {
System.out.println("num is greater than or equal 50");
}
}
}
If-Else-If Statement
If-Else-If statement မှာ if ရဲ့ condition ဟာ true ဖြစ်ခဲ့ရင် if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။ If ရဲ့ condition ဟာ false ဖြစ်ခဲ့ရင် ပထမ else-if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။
Else-if ရဲ့ condition ဟာ true ဖြစ်ခဲ့ရင် else-if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။ Else-If ရဲ့ condition ဟာ false ဖြစ်ခဲ့ရင် နောက် else-if block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။
အထက်က condition တွေ အားလုံးဟာ false ဖြစ်ခဲ့ရင် နောက်ဆုံးမှာ else block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။
ဉပမာ Coca Cola မရခဲ့ရင် Pepsi သောက်မယ်။ Pepsi မရခဲ့ရင် Sprite သောက်မယ်။ Sprite မရခဲ့ရင် Fanta သောက်မယ်။ အထက်ကဟာတွေ မရခဲ့ရင် ဘာပဲဖြစ်ဖြစ်ရှိတာ သောက်မယ် ဆိုတဲ့ ရွေးချယ်မှုမျိုး။
Syntax :
if(condition1) {
Statements(s);
} else if(condition2) {
Statements(s);
}else if(condition3) {
Statements(s);
}
else if(condition4) {
Statements(s);
}
else {
Statements(s);
}
For Example :
if(coldDrink.equals(“Coca Cola”)) {
DrinkCocaCola();
} else if(coldDrink.equals(“Pepsi”)) {
DrinkPepsi();
}else if(coldDrink.equals(“Sprite”)) {
DrinkSprite();
}
else if(coldDrink.equals(“Fanta”)) {
DrinkFanta();
}
else {
AnyColdDrinks();
}
Example of If-Else-If Statement
public class IfElseIfStatement {
public static void main(String args[]){
int num=1234;
if(num <100 && num>=10) {
System.out.println("Its a two digit number");
}
else if(num <1000 && num>=100) {
System.out.println("Its a three digit number");
}
else if(num <10000 && num>=1000) {
System.out.println("Its a four digit number");
}
else if(num <100000 && num>=10000) {
System.out.println("Its a five digit number");
}
else {
System.out.println("number is not between 1 & 99999");
}
}
}
Switch-Case Statement
Switch-Case ကို ရွေးချယ်မှုများစွာရှိတဲ့ အခြေအနေတွေမှာ အသုံးပြုတယ်။ If-Else-If နဲ့ ပုံစံတူတယ်။
Switch-Case statement မှာ switch ဟာ variable သို့မဟုတ် integer expression ဖြစ်ရမယ်။ ပြီးတော့ case ထဲက constant ဟာ switch ထဲက variable သို့မဟုတ် integer expression တစ်ခုခု ဟုတ်ခဲ့ရင် အဲ့ဒီ case block ထဲကို ဝင်ပြီး line by line ဆက်ပြီး execute လုပ်တယ်။ ပြီးရင် နောက် case ကို တစ်ခုပြီးတစ်ခု ထပ်ပြီး execute လုပ်တယ်။ case ဟာ switch ထဲက variable သို့မဟုတ် integer expression တစ်ခုခု ဟုတ်ခဲ့ရင် နောက်ဆုံးမှာ default ထဲကိုဝင်ပြီး တစ်ကြောင်းပြီးတစ်ကြောင်း execute လုပ်တယ်။
Syntax :
switch(variable or an integer expression) {
case constant:
// code
case constant:
// code
default:
// code
}
For Example :
int day = 2;
switch(day) {
case 1:
System.out.println(“Monday”);
case 2:
System.out.println(“Tuesday”);
default:
System.out.println(“Sunday”);
}
Example of Switch-Case Statement
public class SwitchCaseExample1 {
public static void main(String args[]){
int num=2;
switch(num+2)
{
case 1:
System.out.println("Case1: Value is: "+num);
case 2:
System.out.println("Case2: Value is: "+num);
case 3:
System.out.println("Case3: Value is: "+num);
default:
System.out.println("Default: Value is: "+num);
}
}
}
Break Statement
Break ကို block တစ်ခု ရဲ့ အပြင်ထွက်သွားစေဖို့ အသုံးပြုတယ်။ ဉပမာ case တစ်ခုခုမှာ break ကို အသုံးပြုထားရင် အဲ့ဒီ condition ကို execute လုပ်ပြီးတာနဲ့ switch block ရဲ့ အပြင်ကို ထွက်သွားမှာ ဖြစ်တယ်။
Syntax :
switch(variable or an integer expression) {
case constant:
// some code
break;
case constant:
// some code
break;
default:
// code
}
For Example :
int day = 2;
switch(day) {
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
default:
System.out.println(“Sunday”);
}
Example of Break Statement
public class SwitchCaseExample2 {
public static void main(String args[]){
int i=2;
switch(i)
{
case 1:
System.out.println("Case1 ");
break;
case 2:
System.out.println("Case2 ");
break;
case 3:
System.out.println("Case3 ");
break;
case 4:
System.out.println("Case4 ");
break;
default:
System.out.println("Default ");
}
}
}
Comments
Post a Comment