Showing posts with label Part IV : Perulangan (program). Show all posts

Nested For

Example 1:
# include <iostream.h>
# include <conio.h>

main()
{

int loop,counter;
clrscr();

for(counter=0;counter<5;counter++)
{
for(loop=0;loop<counter;loop++)
{
cout <<counter;
}
cout <<"\n";
}

getch();
}
Example 2:
# include <iostream.h>
# include <conio.h>

main()
{

int bil,loop,counter;
clrscr();

cout <<"Masukkan sembarang bilangan ! : ";
cin >>bil;

for(counter=0;counter<=bil;counter++)
{
for(loop=1;loop<counter;loop++)
{
cout <<"*";
}
cout <<"\n";
}
for(counter=bil;counter>=0;counter--)
{
for(loop=2;loop<counter;loop++)
{
cout <<"*";
}
cout <<"\n";
}

getch();
}

Posted in | Leave a comment

For Multi Condition

Untuk For Multi Kondisi (kondisi1 & kondisi2), maka diperlukan operator LOGIKA && untuk relasi AND, dan operator LOGIKA || untuk relasi OR.
Untuk materi dasar for, dapat dilihat di sini!

Langsung saja pada contoh listing programnya.

# include <iostream.h>
# include <conio.h>

main()
{

int bil;
char ket;
clrscr();

for((bil=1)&&(ket='D');(bil<=4)&&(ket>='A');(bil++)&&(ket--))
{
cout <<bil <<" | " <<ket;
cout <<"\n";
}

getch();
}

Posted in | Leave a comment

For

Untuk materi dasar for, dapat dilihat di sini!


Example 1:

# include <iostream.h>
# include <conio.h>

main()
{

int number;
clrscr();

for(number=1;number<5;number=number+1)
{
cout <<number;
cout <<"\n";
}

getch();
}

Example 2 :
# include <iostream.h>
# include <conio.h>

main()
{

clrscr();

for(int number=1;number<5;number=number+1) //Declaration variable type, example (int) will do in property for
{
cout <<number;
cout <<"\n";
}

getch();
}

Example 3:
# include <iostream.h>
# include <conio.h>

main()
{

int number;
clrscr();

number=1;
for( ;number<=5; ) //Initial Variable in property for, moved before intruction for. fix Condition write in property for. And Interval put on under instruction for
{
cout <<number;
cout <<"\n";
bil++; //interval can brief writing from number=number+1 to be number++, and number=number-1 to be number--
}

getch();
}

Posted in | Leave a comment

Search

Support by Blogger ITN | Converted by LiteThemes.com