Perulangan


Perulangan

1. FOR
Contoh 1:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
main()
{ int i,a;
clrscr();
for(a=10;a>=1;--a)
cout<<" "<<a;
getch();
}

Hasil : 10 9 8 7 6 5 4 3 2 1






Contoh 2 :

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
main()
{ int a;
clrscr();
for(a=1;a<=10;++a)
cout<<" "<<a;
getch();
}
Hasil : 1 2 3 4 5 6 7 8 9 10




Contoh 3 :
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
main()
{
int x, y;
clrscr();

for(x = 1;x <= 5; ++x)
{
for(y = 1; y <= x; ++y)
cout<<" "<<y;
cout<<endl;
}
getch();
}

Hasil :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Contoh 4:
#include<iostream.h>
#include<stdio.h>
#include<iomanip>
#include<conio.h>
main()
{
int a;
clrscr();
for(a=1; 17>a;a++)
{ gotoxy(a,a);textcolor(a);
cprintf("\n warna ke-%d",a);
}
textcolor(4+BLINK);cprintf("Borland C++");
getche();
}

Comments