Anda di halaman 1dari 18

BAB 5

PERNYATAAN DASAR

Program 5.1
// *--------------------------------------------------------------------------------------------- *
// * blok.cpp * // * *
// * contoh utnuk menunjukan variable yang *
// * dedeklarasikan di dalam suatu blok hanya dikenal didalam blok itu sendiri *
// *--------------------------------------------------------------------------------------------*

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // hapus layer
int a=5;
cout <<"a ="<<a<< endl;
{
int a;
a=20;
cout << "a = "<<a<<endl;
}
{
cout << "a = "<<a<<endl;
}

getche();
}
Program 5.2
// *-------------------------------------------------------------------------------------------- *
// * testgoto. Cpp *
// * contoh pemakaian goto *
// *---------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main( )
{
clrscr () ; // hapus layer
cout << " test goto " << endl;
goto selesai ;
cout << " hai, saya kok tidak disapa " << endl;
selesai :
cout << " selesai…." << endl;
getche ();
}

Hasil Program
Program 5.4

// *---------------------------------------------------------------------------------------------*
// * usia2.cpp * // * *
// * Program untuk mengambil keputusan *
// * dengan menggunakan pernyataan if-else *
// *---------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ( )
{
int usia;
clrscr (); // hapus layar
cout << "Berapa usia Anda ? " ;
cin >> usia ;
if ( usia < 17 )
cout << " anda tidak diperkenankan menonton"<< endl ;
else
cout << " Selamat menonton " <<endl ;
getche ();
}

Hasil Program
Program 5.5

// *---------------------------------------------------------------------------------------------*
// * kodehari.cpp *
// * contoh pemakaian if untuk menentukan *
// * nama hari *
// *---------------------------------------------------------------------------------------------*

# include<iostream.h>
# include <conio.h>
void main ()
{
int kode_hari ;
clrscr (); // hapus layar
cout << " 1=Senin 3=Rabu 5=Jum'at 7=Minggu " << endl ;
cout << " 2=Selasa 4=Kamis 6=Sabtu" << endl ;
cout << " kode hari ( 1…7..) " ;
cin >> " kode_hari";
// Proses seleksi
if ( kode_hari == 1)
cout << "senin" << endl;
else if ( kode_hari == 2)
cout << " selasa "<< endl;
else if ( kode_hari == 3)
cout << " rabu " << endl;
else if ( kode_hari == 4)
cout << " kamis" << endl;
else if ( kode_hari == 5)
cout << " jum'at " << endl;
else if ( kode_hari == 6)
cout << " sabtu " << endl;
else if ( kode_hari == 7)
cout << " minggu" << endl;
else 2
cout << "kode hari salah " <<endl;
getche ();
}
Program 5.6

// *---------------------------------------------------------------------------------------------*
// * kodehari.cpp *
// * contoh pemakaian if untuk menentukan *
// * nama hari *
// *---------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ()
{
int kode_hari ;
clrscr (); // hapus layar
cout << " 1=Senin 3=Rabu 5=Jum'at 7=Minggu " << endl ;
cout << " 2=Selasa 4=Kamis 6=Sabtu" << endl ;
cout << " kode hari (1...7)";
cin >> kode_hari;
// Proses seleksi
{
if ( kode_hari == 1 )
cout<<"senin"<<endl;
else if ( kode_hari == 2)
cout << " selasa " << endl;
else if ( kode_hari == 3)
cout << " rabu " << endl;
else if ( kode_hari == 4)
cout << " kamis" << endl;
else if ( kode_hari == 5)
cout << " jum'at " << endl;
else if ( kode_hari == 6)
cout << " sabtu " << endl;
else if ( kode_hari == 7)
cout << " minggu" << endl;
else
cout << "kode hari salah " <<endl;
}
getche ();
}
Program 5.7

// *------------------------------------------------------------------------------------------*
// * kabisat. cpp *
// * contoh pemakaian if untuk menentukan *
// * suatu tahun sebagai tahun kabisat atau bukan *
// *-------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ()
{
int tahun ;
int tahun_kabisat;
clrscr ( ); // hapus layar
cout << " menentukan tahun kabisat"<<endl;
cout << "tahun:";
cin >> tahun ;
if ( tahun> 0)
{
if (( tahun % 100 ) == 0)
tahun_kabisat = (( tahun % 400 ) == 0 );
else
tahun_kabisat = (( tahun % 4 ) == 0 );
if ( tahun_kabisat )
cout << "tahun kabisat"<<endl;
else
cout << "bukan tahun kabisat"<<endl;
}
else
cout << "tahun harus berupa bilangan positif "<<endl;
getche ();
}
Program 5.8

// * -----------------------------------------------------------------------------------------------*
// * xy.cpp *
// * contoh pemakaian operator logika && *
// * pada bagian kondisi perintah if *
// * *
// * -----------------------------------------------------------------------------------------------*
#include<iostream.h>
#include<conio.h>

// *---------------------------------------------------------------------------------------------*
// * xy.cpp *
// * contoh pemakaian operator logika && *
// * pada bagian kondisi perintah if*
// *---------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ( )
{
float x, y ;
clrscr ( ); // hapus layar
cout << " masukan bilangan positif atau negative "<<endl;
cout << "Nilai x:";
cin >> x ;

cout << "Nilai y:";


cin >> y ;

if (( x >= 0 ) & (y >= 0 ))


{
cout << " x dan y bernilai negatif "<<endl;
}

cout << " x, y : ada yang bernilai positif "<<endl;


getche ();
}
Hasil Program
Program 5.9

// *-------------------------------------------------------------------------------------------- *
// * pilih 1. cpp *
// * contoh pemakaian operator && *
// * untuk memeriksa kebenaran pilihan *
// *--------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ( )
{
char pil ;
clrscr (); // hapus layar
cout << " masukan pilihan ( 1...5 ) ";
pil =getche () ; // membaca tombol enter
if (( pil >= '1' ) && ( pil <= '5' ))

cout << " \npilihan Anda benar " <<endl;


else
cout << " \npilihan Anda salah " <<endl;
getche ();
}

Program 5.10

// *--------------------------------------------------------------------------------------------*
// * pilih 2. cpp *
// * contoh pemakaian operator \\ *
// * untuk memeriksa kebenaran pilihan *
// *--------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main ( )
{
char pil ;
clrscr (); // hapus layar
cout << " masukan pilihan ( A, B, Q ) ";
pil = getche () ; // membaca tombol tanpa enter

if (( pil == 'A' ) || ( pil == 'B' ) || ( pil <='Q'))


cout << " \nPilihan Anda Benar " <<endl;
else
cout << " \nPilihan Anda salah " <<endl;
getche ();
}
Program 5.11

// * -----------------------------------------------------------------------------------------------*
// * switch.cpp *
// * contoh pemakaian switch untuk menentukan *
// * nama hari *
// * *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
int Kode_hari;
clrscr (); // hapus layar
cout << " menentukan Hari : " << endl;
cout << " 1 = Senin 3 = Rabu 5 = jum'at 7 = Minggu "<< endl;
cout << " 2 = Selasa 4 = Kamis 6 = sabtu "<< endl;
cout << "Kode_hari ( 1. . . 7) :";
cin >> Kode_hari;
// Selesai dengan Switch
switch(Kode_hari)
{
case 1:
cout << " Senin " << endl;
break;
case 2 :
cout << " Selasa " << endl;
break;
case 3:
cout << " Rabu " << endl;
break;
case 4:
cout << " Kamis " << endl;
break;
case 5:
cout << " Jum'at " << endl;
break;
case 6:
cout << " Sabtu " << endl;
break;
case 7:
cout << " Minggu " << endl;
default:
cout << " kode Hari Salah " << endl;
break;
}
// akhir Switch
getche();
}

Program 5.12
// * -----------------------------------------------------------------------------------------------*
// * xy.cpp *
// * contoh pemakaian operator logika && *
// * pada bagian kondisi perintah if *
// * *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
float x, y;
clrscr(); //hapus layar
cout << " masukan Bilangan Positif atau Negatif " << endl;
cout << " Nilai x : ";
cin >> x ;
cout << " nilai y : ";
cin >> y ;
if (( x >=0) && (y >= 0) )
cout << " x dan y bernilai positif " << endl;
else
cout << " x, y : ada yang bernilai negatif "<< endl;
getche ();
}
Program 5.13

// * -----------------------------------------------------------------------------------------------*
// * CPP10x.cpp *
// * contoh pemakaian while untuk menampilkan *
// * tulisan C++ sebanyak 10 kali *
// * *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
int i ;
clrscr(); //hapus layar
i = 0; // mula - mula diisi sama dengan nol
while ( i<10)
{
cout << "C++"<< endl;
i++; // menaikan pencacah sebesar 1
}
getche();
}

Program 5.15
// * -----------------------------------------------------------------------------------------------*
// * jumnilai.cpp *
// * contoh pemakaian while untuk menghitung *
// * nilai rata - rata dari sejumlah nilai *
// * yang dimasukan dari keyboard *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
int pencacah ;
float nilai;
float total;
clrscr(); //hapus layar
cout << "Menghitung Nilai rata - rata. "<< endl;
cout << " Akhiri dengan memasukan nilai negatif "<< endl;
pencacah = 0; // mula - mula diisi denga nol
total = 0; // begitu juga variabel ini
cout << " nilai =" ;
cin >> nilai;
while ( nilai >=0 )
{
pencacah++;
total+= nilai ;
cout << "nilai =";
cin>> nilai; // Baca nilai
}
cout << "Nilai rata - rata : "<<total /pencacah << endl;
getche();
}

Program 5.16
// * -----------------------------------------------------------------------------------------------*
// * CPP10x2.cpp *
// * contoh pemakaian do - while untuk menampilkan *
// * tulisan C++ sebanyak 10 kali *
// * *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
int i ;
clrscr(); //hapus layar
i = 0; // mula - mula diisi sama dengan nol
do
{
cout << "C++" << endl;
i++; // menaikan pencacah sebesar 1
}
while (i<10);
getche();
}
Program 5.17
// * -----------------------------------------------------------------------------------------------*
// * abjad.cpp *
// * Menampilkan abjad A sampai dengan Z *
// * menggunakan do - while *
// * *
// * -----------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
char abjad;
clrscr(); //hapus layar
abjad ='A'; // mula - mula diisi sama dengan nol
do
{
cout << abjad;
abjad+=1;
}
while (abjad <='Z');
getche();
}

Program 5.18

// *-------------------------------------------------------------------------------------------- *
// * cpp 10x. cpp *
// * contoh pemakaian do- while untuk menampilkan *
// * tulisan C++ sebanyak 10 kali *
// *---------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main ( )
{
char abjad;
clrscr ( ) ; // hapus layer
abjad = 'A';
do
{
cout << abjad ;
abjad += 1 ;
} while ( abjad <= 'Z' ) ;
getche ();
}
Program 5.19

// *--------------------------------------------------------------------------------------------*
// * abjad2. cpp *
// * menampilkan abjad A sampai dengan Z *
// * menggunakan for *
// *--------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main ( )
{
char abjad;
clrscr ( ) ; // hapus layer
for (abjad = 'A'; abjad <= 'Z'; abjad ++ )
cout << abjad;
getche ();
}

Program 5.20
// *-------------------------------------------------------------------------------------------- *
// * deret.cpp *
// * contoh untuk memanpilkan bilangan *
// *genap yang nilainya kurang dari atau sama *
// * dengan n dan ditampilkan dari yang terbesar menuju nol *
// *---------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ( )
{
int n;
clrscr ( ); // hapus layer
cout << " menampilkan deret bilangan genap."<< endl;
cout << " kurang dari atau sama dengan n."<< endl;
cout << " masukan nilai n.n."<< endl;
cin >> n;
// jika n bilangan ganjil, maka kurangi sebesar 1
if ( n % 2 )
n --; // tampilkan deret bilangan genap dari besar ke kecil
for (; n>= 0; n-= 2 );
cout << n << '/n';
getche ();
}
Program 5.21

// *--------------------------------------------------------------------------------------------*
// * segitiga.cpp *
// * membentuk segitiga yang berisi karakter *
// * dengan menggunakan for di dalam for *
// *--------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main ()
{
int tinggi; // menyatakan tinggi segitiga
int baris; // pencacah untuk baris
int kolom ; // pencacah untuk kolom
clrscr (); // hapus layer

cout << " tinggi segitiga = ";


cin >> tinggi ;
cout << endl ; // membuat garis kosong
for ( baris = 1, kolom <= baris , kolom++ );
cout << '*' << endl ; // pindah baris
getche ();
}

Program 5.22
// *---------------------------------------------------------------------------- *
// * tesbreak.cpp * // * *
// * contoh program untuk melihat efek break *
// * pada pernyataan while *
// *----------------------------------------------------------------------------*
# include <iostream.h>
# include <conio.h>

void main ()
{
int bil = 1;
clrscr (); // hapus layer
while ( bil <= 10 )
{
if ( bil > 5 )
break :
cout << bil << endl ;
bil ++ ;
getche();
}
}
Program 5.23
// *-------------------------------------------------------------------------------------------- *
// * ketik.cpp * // * *
// * contoh pemakaian break pad pernyataan for *
// * *
// *---------------------------------------------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main ()
{
char karakter;
clrscr (); // hapus layer
cout << " anda bisa mengetik apa saja " <<endl;
cout << " sampai tombol ESCAPE anda tekan " <<endl;
for ( ; ; )
{
karakter = getche () ; // baca tombol dan tampilkan
if ( karakter == 27 ) // escape
break ;
}
}

Program 5.24
// *-------------------------------------------------------------------------------------------- *
// * testcont.cpp * // * *
// * contoh program untuk melihat efek continue *
// * pada pernyataan for *
// *---------------------------------------------------------------------------------------------*
# include<iostream.h>
# include<conio.h>
void main ( )
{
int bil;
clrscr (); // hapus layer
for ( bil = 0, bil < 10, bil++ )
cout << " bil " <<;
continue ;
cout << " perhatikan apakah saya muncul? " <<endl;
}
Program 5.25

// *--------------------------------------------------------------------------------------------*
// * segitiga.cpp * // * *
// * membentuk segitiga yang berisi karakter *
// * dengan menggunakan for di dalam for *
// *--------------------------------------------------------------------------------------------*

# include<iostream.h>
# include<conio.h>
void main ()
{
int pencacah; // menyatakan banyaknya nilai
float nilai; // menyatakan nilai yang dimasukan
float total // menyatakan total dari nilai

clrscr (); // hapus layer

cout << " menghitung nilai rata-rata = "<< endl ;


cout << "akhiri dengan memasukkan nilai negatip" << endl;
cout << endl ; // membuat garis kosong
pencacah = 0; // mulamula diisi dengan nol
total = 0; // begitu juga variable ini
while ( 1 ) // ungkapan selalu benar
{
cout << " nilai ke-" << pencacah ++ << " = ";
cin >> nilai; // baca nilai
if ( nilai < 0 )
break; / / keluar dari
while
pencacah ++; // naikan pencacah sebesar 1
total += nilai; // tambahkan nilai ke total
cout << " jumlah nilai =" <<pencacah << endl<< ;
cout << " nilai rata rata" <<total / pencacah << endl;

}
getche ();
}
Program 5.26
// *--------------------------------------------------------------------*
// * yatidak.cpp * // * *
// * program ini akan memberikan nilai keluar *
// * 1 = untuk jawan ya 0 = untuk jawaban tidak *
// *--------------------------------------------------------------------*

#include<iostream.h>
#include<conio.h>
#include<ctype.h> // untuk toupper ( )
#include<stdlib.h> // untuk exit ( )

void main ()
{
char jawab ;
cout << " Y = ya, T = tidak.pilihan ( Y / T ) : ";
for ( ; ; )
{

// baca tombol dan ubah ke huruf capital


jawab = toupper (getch ());
if (( jawab == 'Y') // ( jawab == 'T' ))
{
cout << jawab << endl;
if ( jawab == ' T ' )
exit ( 1 ) ;
else
exit (0);

}
}
}

Anda mungkin juga menyukai