Wednesday, May 14, 2014

ජාවා Jump Statement හා Loops සදහා practice coding


පිජාවා ‍ language ‍කේ jump   statement 3‍නක්‍ ‍හාදක්‍‍. ‍නම්  

1.      break
2.      continue
3.      return

හොයි ‍‍පිමුමේ jump   statement ‍මොබාවිතාවෙන්‍‍නෙකිල. ‍අ‍පි ‍මේ‍ව use  ‍ක‍රන්‍‍නෙ control   structure ‍එ‍කක්‍ ‍පා‍ල‍න‍ය ‍ක‍රන්‍‍න ‍එ ‍කි‍යන්‍‍නෙ code  ‍එ‍කක්‍ ‍තු‍ල ‍ඇ‍ති loop  ‍එ‍කක්‍ ‍අ‍ත‍ර‍ම‍ග ‍අ‍ප‍ට ‍අ‍වශ්‍ය ‍ප‍රි‍දි ‍න‍වත්‍‍තන්‍‍න.‍හො‍ද‍යි ‍අ‍පි ‍එ‍කින්‍ ‍එ‍ක  coding ‍ව‍ලින්‍‍ම ‍පැ‍හ‍දි‍ලි ‍ක‍ර‍ග‍මු.

break statement
 Code:-

public class breakStatement {

    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            if(i==5){
                break;
            }
            System.out.print(i+" ");
        }

    }

}
Out Put:-

‍හො‍ද‍යි ‍අ‍පි ‍බ‍ල‍මු ‍මේ code ‍එ‍ක. ‍සා‍මාන්‍ය for   loop ‍එ‍කක්‍ ‍ආ‍දා‍ර‍යෙන්‍ 0 ‍සි‍ට 9 ‍දක්‍‍වා ‍අ‍ග‍යන්‍ print  ‍කි‍රී‍ම‍ට ‍යො‍දා‍ගන්‍‍නා code  ‍එ‍ක‍ක‍ට ‍අ‍පි break   statement ‍එ‍ක use  ‍ක‍ර‍ල ‍ති‍ය‍න‍ව ‍අ‍පි if   condition ‍එ‍කක්‍ ‍දා‍ල.‍ඒ ‍කි‍යන්‍‍නෙ 0 ‍සි‍ට ක්‍ර‍ම‍යෙන්‍ ‍වැ‍ඩි ‍වී ‍ය‍න i  ‍අ‍ග‍ය 5 ‍ට ‍ස‍මා‍න ‍ඌ ‍වි‍ට break   statement ‍එ‍ක  use ‍ක‍ර‍ල ‍ති‍ය‍න‍ව ‍එ‍ත‍නින්‍ i=5  ‍ඌ ‍වි‍ට loop  ‍එ‍කෙන්‍ ‍පි‍ට‍ට ‍යා‍ම ‍හෙ‍වත්‍ loop ‍එ‍ක ‍නැ‍ව‍තී‍ම ‍සි‍දු ‍ව‍නු ‍ඇ‍ත   

Continue

Code:-

public class continueStatement {

    public static void main(String[] args) {
        for(int i=0;i<=10;i++){
            if(i==5){
                continue;
            }
            System.out.print(i+" ");
        }
    }

}
 
Out Put:-


‍මෙ‍හි‍දී‍ද ‍අ‍පි for   loop ‍එ‍කක්‍ use  ‍ක‍ර‍ල 0 ‍සි‍ට 10 ‍දක්‍‍වා ‍අ‍ග‍යන්‍ print  ‍කි‍රී‍ම ‍සි‍දු ‍ක‍ර‍ගෙ‍න ‍ති‍බෙ‍න‍ව ‍න‍මුත්‍ ‍අ‍ප  continue  statement ‍එ‍ක use  ‍ක‍ර‍ල ‍ති‍ය‍න‍ව. ‍ඒ ‍කි‍යන්‍‍නෙ i  ‍හි ‍අ‍ග‍ය 0 ‍සි‍ට ක්‍ර‍ම‍යෙන්‍  ‍වැ‍ඩි ‍වී ‍ගෙ‍න ‍ය‍න ‍වි‍ට i=5  ‍ව‍න ‍අ‍වස්‍‍තා‍වේ‍දී  continue  key  word ‍එ‍ක‍ට ‍ප‍හ‍ලින්‍ ‍ඇ‍ති ‍කි‍සි‍දු statement  ‍එ‍කක්‍ execute  ‍නො‍ක‍ර ‍නැ‍ව‍ත loop  ‍එ‍කේ ‍ඊ‍ල‍ග ‍පි‍ය‍ව‍ර ‍ව‍න i=6  ‍අ‍වස්‍‍තා‍ව‍ට ‍යා‍ම ‍ත‍ම‍යි ‍සි‍දු‍වෙන්‍‍නෙ ‍ම‍ම ‍හි‍ත‍න‍ව ‍මේ‍ක ‍ඔ‍යා‍ල‍ට ‍තේ‍රු‍ම ‍ගැ‍නී‍ම‍ට ‍ඉ‍තා‍මත්‍ ‍‍ප‍හ‍සු‍යි   ‍කි‍ය‍ල.

මී‍ල‍ග statement  ‍එ‍ක‍ත‍ම‍යි return  ‍මේ‍ක ‍ගැ‍න ‍අ‍පි‍ට ‍දැන්‍‍ම ‍ක‍තා ‍කි‍රී‍ම‍ට ‍නො‍හැ‍කි‍යි. ‍අ‍ප ‍මේ ‍ගැ‍න oop  ‍පා‍ඩ‍මෙන්‍  ‍ක‍තා ‍ක‍ර‍මු.

‍දැන්‍ ‍ත‍ම‍යි ‍ලස්‍‍ස‍න‍ම code  ‍ග‍හ‍න‍ව ‍කිව්‍‍වෙ ‍අන්‍‍න ‍ඒ‍ව ‍ග‍හන්‍‍න ‍වෙ‍ලා‍ව ‍එ‍හෙ‍නම්‍ ‍අ‍පි ‍වැ‍ඩේ‍ට ‍බ‍හි‍මු .‍අ‍පි ‍ජා‍ව ‍ව‍ල loops  handlying  ‍පි‍ලි‍බ‍ද‍ව ‍ගො‍ඩාක්‍ expert  ‍වෙන්‍‍න ‍ත‍ම‍යි ‍මේ coding  ‍ග‍හන්‍‍නෙ.

Code:-

public class square {

    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                System.out.print("* ");
            }
            System.out.println();
        }
   
    }

}
Out Put:-

මේ  java  code ‍හොදින්‍ ‍විශ්‍‍ලේරන්‍‍. ‍ඉන්‍ ‍සු out   put ‍බාගැනී code  හන්‍‍ code ‍ගමතිබෙව.
Code:-

public class halfTriangle {

    public static void main(String[] args) {
        for(int vertical=1;vertical<=10;vertical++){
            for(int horizotal=1;horizotal<=vertical;horizotal++){
                System.out.print("* ");
            }
            System.out.println();
        }

    }

}
 
Out Put:-

Code:-

public class halfTriangle2 {

    public static void main(String[] args) {
        for(int vertical=1;vertical<=10;vertical++){
            for(int horizotal=10-vertical;horizotal>0;horizotal--){
                System.out.print("* ");
            }
            System.out.println();
        }
    }

}
 
Out Put:-


Code:-

public class halfTriangle3 {

    public static void main(String[] args) {
        for(int vertical=1;vertical<=10;vertical++){
            for(int horizotal=10-vertical;horizotal>0;horizotal--){
                System.out.print(" ");
            }
            for(int i=1;i<=vertical;i++){
                System.out.print("*");
            }
            System.out.println();
        }

    }

}
 
Out Put:-

Code:-

public class triangle {

    public static void main(String[] args) {
        for(int vertical=1;vertical<=10;vertical++){
            for(int horizotal=10-vertical;horizotal>0;horizotal--){
                System.out.print(" ");
            }
            for(int i=1;i<=vertical*2-1;i++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
       
}
 
Out Put:-

Code:-

public class triangle2 {

    public static void main(String[] args) {
        for(int vertical=1;vertical<=5;vertical++){
            for(int horizotal=10-vertical;horizotal>0;horizotal--){
                System.out.print(" ");
            }
            for(int i=1;i<=vertical*2-1;i++){
                System.out.print(i);
            }
            System.out.println();
        }
    }

}
 
Out Put:-




හෙනම්‍ ‍මේති ‍‍කිහියාකෞරුත්‍ try  රන්‍‍ for   loop  use ‍විවිටාදන්‍‍රුවක්‍ ‍දන්‍‍. ‍කෙන්‍ ‍ගොඩක්‍ practice  වෙයි.
                                                   
By Manoj  Priyankara

No comments:

Post a Comment