PERTEMUAN 4
description
Transcript of PERTEMUAN 4
PERTEMUAN
4
Seleksi Kondisi
[Kristanto:2009] perintah if berguna untuk memilih satu dari 2 atau lebih alternatif jawaban yang tersedia. Jika perintah if tersebut terdiri dari 2 atau lebih pernyataan.
Bentuk Umum:
if (kondisi) { pernyataan }
If Sederhana
#include <iostream.h>#include <conio.h>main(){int harga,diskon=0;char bonus[10];cout<<" \2\2\2\3\3\3\3\2\2\2\2"<<endl;cout<<" Input Harga : ";cin>>harga;
if(harga>10000) { diskon=0.10*harga; strcpy(bonus,"Permen"); }
clrscr();cout<<" Harga Beli : "<<harga<<endl;cout<<" Diskon : "<<diskon<<endl;cout<<" Bonus : "<<bonus;getch();}
Perintah If...Else
if(kondisi)pernyataan1
elsepernyataan2
Bentuk Umum:
#include <iostream.h>#include <conio.h>main(){int harga,diskon=0;char bonus[10];cout<<" \2\2\2\3\3\3\3\2\2\2\2"<<endl;cout<<" Input Harga: ";cin>>harga;
if(harga<10000) {diskon=0.10*harga;
strcpy(bonus,"Permen"); }else{diskon=0.20*harga;
strcpy(bonus,"Coklat"); } clrscr();cout<<" Harga Beli : "<<harga<<endl;cout<<" Diskon : "<<diskon<<endl;cout<<" Bonus : "<<bonus;getch();}
Perintah If...Else Majemuk
if (kondisi) {
perintah-1; ... }
else {
perintah-2; ... }
#include <stdio.h>#include <conio.h>#include <iostream.h> main( ){char gol, jab[10]; long gapok=0; clrscr( );cout<<endl<<" Data Jabatan"<<endl<<" = = = = = = = = = = = = ="<<endl;cout<<" Masukan Golongan [1/2]:";cin>>gol;
Contoh: if(gol=='1'){ strcpy(jab,"Direktur"); gapok=5000000; }else if(gol=='2'){ strcpy(jab,"Manajer"); gapok=4000000; }
cout<<" Jabatan = "<<jab<<endl cout<<" GaPok="<<gapok<<endl; getch ( );}
Golongan
Jabatan
Gaji Pokok
1 Direktur
5.000.000
2 Manager
4.000.000
Input:
Output:
Pendapatan Jasa Komisi
<=Rp 200.000 Rp 10.000 10% * Pendapatan
<=Rp 500.000 Rp 20.000 15% * Pendapatan
>Rp 500.000 Rp 30.000 20% * Pendapatan
total=pend+komisi+jasa;
Ketentuan Soal