Home » » Logika IF dalam C++ dan Contoh Program C++ Menggunakan IF

Logika IF dalam C++ dan Contoh Program C++ Menggunakan IF

Belajar C++ khususnya tentang perintah IF membutuhkan logika atau pemikiran yang teliti dari seorang programmer sebelum menuliskan baris per baris kode program untuk menyelesaikan masalah yang ada. Perintah IF biasanya merupakan perintah pertama yang dipelajari setelah mengenal syntax-syntax dasar bahasa pemrograman, mengenal tipe data, konstanta, variable, mengenal operator dan lain-lain.

Contoh program C++ tentang operator aritmatika bisa dilihat pada posting blog kang asep sule sebelumnya yaitu contoh program C++ menghitung luas segitiga, hitung luas bujur sangkar, dan hitung luas persegi panjang.

Kembali ke IF dalam C++. Perintah atau pernyataan IF mengandung arti :


Jika kondisi bernilai benar, maka perintah akan dikerjakan.
Jika kondisi bernilai salah, maka perintah tidak akan dilaksanakan


IF the meaning of when depicted in the flow diagram (flowchart) as follows:



For more details about command IF in C++, please Googling wrote when it hasn't been able to buy the book the complete guide to C++. For the moment, kang asep sule just wanted to give you an example c++ program using the IF command.

Example case:

  • Bookstore "kamseupay" held a promotion.
  • If you bought your book with the total purchase is more than or equal to Rp. 100,000, then it got discount a discount of 10% of the total purchases.
  • If you bought your books with total purchases of less than $ 100,000, then don't get a discount.
  • Total pay = Total purchase-discount.

Program listings:

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

main ()
{
float total_beli, discount, total_bayar;

clrscr ();

<< cout "Kamseupay Bookstore" << endl;
cout << "= == == == == == == == == ==" << endl;
<< cout "Total Purchases = Rp."; total_beli; >> cin
cout << endl;

if (total_beli > = 100000)
discount = 0.1 * total_beli;
else
discount = 0;

cout << "== == == == == == == == == == ==" << endl;
<< cout "discount = Rp." << discount << endl;
total_bayar = total_beli-discount;
<< cout "Total Pay = Rp." << total_bayar;
getch ();
}

0 komentar:

Posting Komentar