Minggu, 20 November 2011

Perulangan



Untuk memahami mengenai fungsi perulangan, coba lihatlah kasus sebgai berikut:
buatlah 1 program untuk menampilkan angka dari 1 sampai dengan 5. Maka untuk kasus tersebut program yang buat adalah sebagai berikut:
#include <stdio.h>
#include <conio.h>
main ()
{
                printf (“1\n”);
printf (“2\n”);
printf (“3\n”);
printf (“4\n”);
printf (“5\n”);
getch ();
}

Ada beberapa jenis perulangan yang dapat dilakukan oleh bahasa pemrograman C, yaitu :
-          for
-          While
-          Do while

Ø  Perulangan dengan perintah do while
Perulangan for mempunyai bentuk umum sebagai :
for inisialisasi counter : kondisi perulangan : statement
{
                Statement ;
}
contoh berikut akan menampilkan angka 1 sampai 100, kemudian menampilkan angka 10 turun sampai 0 dengan perubahan nilainya setengah (0,5)
#include <stdio.h>
#include <conio.h>
main ()
{
                int i;
   int f;
   for (i=1;i<=1000;i++)
   printf ("%i\n",i);
   for (f=10;f>=0;f-=0.5)
   printf ("%6.2f\n",f);
getch ();
}

Ø  Perulangan dengan perintah while
Bentuk umum dari while adalah sebagai berikut :

While (kondisi)
{
                Perintah;
                Perintah;
}

Cara kerja dari perubahan while mirip dengan for.
#include <stdio.h>
#include <conio.h>
main ()
{
                int i;
                float f;
                i=1;
                while (1<=1000)
                {
                                printf ("%i\n",i);
                                i++;
                }
                f=10;
                while (f>=0)
                {
                                printf ("%6.2f\n",f);
                                f=f-0.5;
                }
                getch ();

}

Ø  Perulangan dengan perintah do while
Bentuk umum do while sebagai berikut :

Do
{
                peintah;
                perintah;
} while (kondisi);


#include <stdio.h>
#include <conio.h>
main ()
{
                int i;
                float f;
                i=1;
                do
                {
                                printf ("%i\n",i);
                                i++;
                } while (i<=1000);
                f=10;
                do
                {
                                printf ("%6.2f\n",f);
f=f-0.5;
} while (f>=0);
getch ();
}

Ø  Menggunakan for bilangan naik
#include <iostream.h>
#include <conio.h>


 main ()
{
                int x,y=0;
                clrscr();
                for (x=1;x<=5;++x)
                { cout<<x<<endl;y=x+y;y
                  cout<<"_______+"<<endl;
                  cout<<y;
                getch ();
}

Ø  Menggunakan program for bilangan turun
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
Void main ()
{
                int x;
                clrscr ();
                for (x=5;x>=1;--x)
                cout<<"nilai"<<endl<<x<<endl;
                getch ();
}Outputnya : 5 4 3 2 1


Ø  Program for bilangan genap
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main ()
{
                int a;
                clrscr ();
                for (a=0;a<=10;a+=2)
                printf ("%d",a);
                getch ();
}

Outputnya : 0 2 4 6 8 10


Ø  Program for bilangan ganjil
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main ()
{
                int a;
                clrscr ();
                for (a=1;a<=10;a+=2)
                printf ("%d",a);
                getch ();
}
Outputnya : 1 3 5 7 9

0 komentar:

Posting Komentar

zwani.com myspace graphic comments
 

Dyah Sharaswati Copyright © 2009 Cosmetic Girl Designed by Ipietoon | In Collaboration with FIFA
and web hosting