Selasa, 08 Januari 2013

Algoritma Perulangan/Looping


 










                                      Nama    : Inayah Oktafian
                                      Nim      : 12.12.0276
                                      Kelas    : SI 12 E


SEKOLAH TINGGI MANAJEMEN INFORMATIKA DA KOMPUTER
AMIKOM PURWOKERTO
2012



A. Dasar Teori
Dalam makalah ini kita menggunakan fungsi Looping atau Perulangan.
Looping atau perulangan merupakan suatu blok kode program berdasar kondisi yang ditentukan sampai tercapai kondisi untuk menghentikannya (terminasi). Setiap perulangan memiliki empat bagian, yaitu inisialisasi, badan program, iterasi,dan terminasi. Inisialisasi adalah program yang menyiapkan keadaan awal perulangan.
Badan program adalah pernyataan yang ingin kita ulangi. Iterasi adalah program yang kita jalankan setelah badan program, tetapi sebelum bagian tersebut dijalankan lagi. Terminasi adalah pernyataan boolean yang diperiksa setiap kali selama perulangan untuk melihat apakah sudah waktunya menghentikan eksekusi.
Struktur kontrol pengulangan adalah berupa pernyataan dari Java yang mengijinkan kita untuk mengeksekusi blok code berulang-ulang sesuai dengan jumlah tertentu yang diinginkan. Pada Java dikenal tiga macam bentuk perulangan, yaitu : While,Do While,For.

1.     While
Pernyataan while akan dijalankan secara terus-menerus selama kondisi bernilai benar (True).
Bentuk umumnya adalah :
while( boolean_expression )
                  {
                              statement1;
                              statement2;
                              . . .
                  }
2.     Do While
Pernyataan do……while, statement dieksekusi setidaknya satu kali.
Bentuk umumnya adalah :
do{
      statement1;
      statement2;
      . . .
}while( boolean_expression );
3.     For
Pernyataan for , melakukan eksekusi pengulangan beberapa kali.
Bentuk umumnya adalah :
for (Initialization; LoopCondition; StepExpression){
                  statement1;
                  statement2;
                  . . .
      }














 

B. Listing Program
1.      Menampilkan Bilangan 1-10
a.       Coding menggunakan perulangan While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
      int y=0;
        while(y<10){
            y++;
            System.out.println(y);
      }

    }
}
b.      Coding menggunakan perulangan Do…While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
            int y=1;
        do {
            System.out.println(y);
            y++;
        } while (y<=10);


    }
}
c.       Coding menggunakan perulangan For :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
           
            int y;
        for (y=1;y<=10;y++){
            System.out.println(y);
        }
    }
}

 










2.      Menampilkan bilangan 10-1
a.       Coding menggunakan perulangan While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
            int y=10;
        while(y>0){
            System.out.println(y);
            y--;
            }
    }
}
b.      Coding menggunakan perulangan Do…While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
              int a=10;
        do {
            System.out.println(a);
            a--;
        } while (a>0);
    }
c.       Menggunakan perulangan For :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
             
            int a;
        for (a=10;a>0;a--){
            System.out.println(a);
        }
    }
}
 









3.      Menampilkan bilangan kelipatan 3 tanpa kelipatan 5 mulai dari 3-30
a.       Coding menggunakan perulangan While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
            int y=3;
        while(y<=30){
            if(y%5!=0) {
                System.out.println(y);
            }
        y+=3;
        }
             
    }
}
b.      Coding menggunakan perulangan Do……While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int y=3;
        do {           
            if(y%5!=0) {
                System.out.println(y);
            }
            y+=3;
        } while (y<=30);

             
    }
}
c.       Coding menggunakan perulangan For :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        for(int y=3;y<=30;y+=3){
            if(y%5!=0) {
                System.out.println(y);
            }
        }
             
    }
}









4.      Menampilkan bukit bintang
a.       Coding menggunakan For :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int i=0,j=0;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }
        for(i=5;i>0;i--)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}


 











5.      Menampilkan perkalian 1-100
a.       Coding menggunakan perulangan While :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inayah_algo;

import java.util.Scanner;
/**
 *
 * @author acer
 */
public class Inayah_algo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
               Scanner msk=new Scanner(System.in);
       
        int i=1,j=1,hasil;
        String tampung;
       
        for(i=1;i<=10;i++)
        {
            for(j=1;j<=10;j++)
            {
                hasil=i*j;
                System.out.println(i+"x"+j+'='+hasil);
                if(j==10)
                {
                    System.out.println("============");
                    System.out.print("tekan enter untuk lanjut");
                    tampung=msk.nextLine();
                   
                    if(tampung.equals(""))
                    {
                        continue;
                       
                    }
                }
            }
        }
    }
}