အခြေခံ ကွန်ပျူတာပရိုဂရမ် ရေးသားနည်း - Loops
Loops
နေဟာ မနက်တိုင်းထွက်ပြီး ညနေတိုင်းဝင်တယ်။ ကမ္ဘာကြီးဟာ နေကို တစ်နှစ်တစ်ခါ တစ်ပတ်ပတ်တယ်။ ကျောင်းသားကျောင်းသူတွေဟာ တနင်္လာကနေသောကြာနေ့တိုင်း ကျောင်းသွားတယ်။ ဝန်ထမ်းတွေဟာ စနေ တနင်္ဂနွေ နေ့တိုင်း အနားယူကြတယ်။
ကွန်ပျူတာပရိုဂရမ်ရေးသားရာမှာ Loop တွေကို အကြိမ်ကြိမ် အထပ်ထပ်အခါခါ လုပ်ရလေ့ရှိတဲ့ အခြေအနေတွေမှာ အသုံးပြုတယ်။
For Loop Statement
အထပ်အခါခါ ပြုလုပ်မဲ့အကြိမ်ရေကို တိတိကျကျ သိပြီးသားဖြစ်တဲ့ ကိစ္စမျိုးတွေမှာ for loop ကို အသုံးပြုတယ်။
Syntax :
for(initialization; condition ; increment/decrement) {
statement(s);
}
For Example :
for(int i; i<5 ; i++) {
System.out.println(“Number ” + i);
}
Example of For loop
public class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--){
System.out.println("The value of i is: "+i);
}
}
}
Infinite For Loop Statement
For loop condition ရဲ့ boolean expression ဟာ အရေးကြီးပါတယ်။ တကယ်လို့ condition ဟာ တချိန်လုံး true ကို return ပြန်နေမယ်ဆိုရင် ပရိုဂရမ်ဟာ မရပ်မနား run နေပါလိမ့်မယ်။ အဲ့ဒီလို မရပ်မနား run နေတဲ့ loop ကို infinite loop လို့ ခေါ်တယ်။
Example of Infinite For loop
public class InfiniteForLoopExample {
public static void main(String args[]){
for(int i=1; i>=1; i++){
System.out.println("The value of i is: "+i);
}
}
}
For Loop and An Integer Array
အောက်က ဉပမာမှာ Array ထဲက int values တွေကို for loop သုံးပြီး print လုပ်ထားတယ်။
Example of For loop and Integer Array
public class ForLoopAndArrayExample {
public static void main(String args[]){
int arr[]={3,11,16,21};
//i starts with 0 as array index starts with 0 too
for(int i=0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
}
Example of For loop and String Array
public class ForLoopWithStringArraryExample {
public static void main(String args[]){
String arr[]={“hi”,”good”,”morning”};
for (String str : arr) {
System.out.println(str);
}
}
}
Enhanced For Loop
အောက်က ဉပမာမှာ Array ထဲက int values တွေကို enhanced for loop သုံးပြီး print လုပ်ထားတယ်။
Example of Enhanced For loop and Array
public class EnhancedForLoopExample {
public static void main(String args[]){
int arr[]={12,1,4,19,21};
for (int num : arr) {
System.out.println(num);
}
}
}
While Loop Statement
While loop ကို အခြေအနေ condition တစ်ခု မှန်နေသ၍ အကြိမ်ကြိမ် ပြုလုပ်မှာ ဖြစ်တယ်။
Syntax :
while(condition) {
statement(s);
}
For Example :
while(i>0) {
System.out.println(“Number ” + i);
}
Example of While Loop Statement
public class WhileLoopExample {
public static void main(String args[]){
int i=10;
while(i>1){
System.out.println(i);
i--;
}
}
}
Infinite While Loop Statement
While loop condition ရဲ့ boolean expression ဟာ အရေးကြီးပါတယ်။ တကယ်လို့ condition ဟာ တချိန်လုံး true ကို return ပြန်နေမယ်ဆိုရင် ပရိုဂရမ်ဟာ မရပ်မနား run နေပါလိမ့်မယ်။ အဲ့ဒီလို မရပ်မနား run နေတဲ့ loop ကို infinite loop လို့ ခေါ်တယ်။
Example of Infinite While Loop Statement
public class InfiniteWhileLoopExample {
public static void main(String args[]){
int i=10;
while(i>1){
System.out.println(i);
i++;
}
}
}
While Loop and An Integer Array
အောက်က ဉပမာမှာ Array ထဲက int values တွေကို while loop သုံးပြီး print လုပ်ထားတယ်။
Example of While loop and Integer Array
public class WhileLoopAndArrayExample {
public static void main(String args[]){
int arr[]={12,10,15,19,3,6};
//i starts with 0 as array index starts with 0 too
int i=0;
while(i<6){
System.out.println(arr[i]);
i++;
}
}
}
Do While Loop Statement
Do-While loop ကို အနည်းဆုံး တစ်ကြိမ် နဲ့ condition မှန်နေသ၍ အကြိမ်ကြိမ် ပြုလုပ်မှာ ဖြစ်တယ်။
Syntax :
do(condition) {
statement(s);
} while(condition);
For Example :
do{
System.out.println(“Number ” + i);
} while(i>0)
Example of Do-While Loop Statement
public class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}
Do-While Loop and An Integer Array
အောက်က ဉပမာမှာ Array ထဲက int values တွေကို do-while loop သုံးပြီး print လုပ်ထားတယ်။
Example of For loop and Integer Array
public class DoWhileLoopAndArrayExample {
public static void main(String args[]){
int arr[]={21,1,5,8};
//i starts with 0 as array index starts with 0
int i=0;
do{
System.out.println(arr[i]);
i++;
}while(i<4);
}
}
Continue Statement
Continue ကို လက်ရှိ block တစ်ခုရဲ့ အပြင် တစ်ကြိမ်ထွက်သွားစေဖို့ အသုံးပြုတယ်။
Syntax :
continue;
Example of Continue Statement
public class ContinueExample {
public static void main(String args[]){
for (int j=0; j<=5; j++)
{
if (j==4)
{
continue;
}
System.out.print(j+" ");
}
}
}
Example of Continue and While Loop
public class ContinueInWhileLoopExample {
public static void main(String args[]){
int counter=10;
while (counter >=0){
if (counter==3)
{
counter--;
continue;
}
System.out.print(counter+" ");
counter--;
}
}
}
Comments
Post a Comment