Anda di halaman 1dari 13

MODUL PERKULIAHAN

Algoritma dan
Pemrograman

Kondisional IF

Fakultas Program Studi Tatap Muka Kode MK Disusun Oleh

06
Ilmu Komputer Teknik Informatika W151700001 Andra Warastri, ST, MTI

Abstract Kompetensi
Modul ini memberikan pemahaman Mahasiswa mampu membuat program
tentang kondisional IF. bahasa C++ sederhana dengan
menerapkan kondisional IF.
1 Pernyataan if
Pernyataan if dapat dipakai untuk mengambil keputusan berdasarkan suatu kondisi. Bentuk
pernyataan ada dua macam :
if
if else

1.1 Pernyataan if sederhana

Pernyataan if paling sederhana berbentuk :

if (kondisi)
pernyataan

Kondisi digunakan untuk menentukan pengambilan keputusan


Pernyataan dapat berupa sebuah pernyataan-pernyataan majemuk.
Penerapan if misalnya untuk menentukan seseorang boleh atau tidak menonton
pertunjukkan bioskop. Kondisi yang digunakan seseorang boleh menonton kalau sudah
berusia 17 tahun.
Contoh program :
//*-----------------------------------------------------------*
//* Contoh 4.1 : Penggunaan if dalam pengambilan *
//* keputusan *
//*-----------------------------------------------------------*
#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;
}
Hasil eksekusi :
Berapa usia anda ? 16 
Anda Tidak diperkenankan menonton

Apabila program dieksekuisi lagi untuk memasukkan usia diatas 17 maka :


Tampak diatas bila dimasukkan usia diatas 17 maka pesan tidak akan ditampilkan. Untuk
mengatasi hal ini dapat dilakukan dengan menggunakan pernyataan kondisi if else.

Berapa usia anda ? 21 

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


2 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
1.2 Pernyataan if else

Pernyataan if else mempunyai bentuk sebagai berikut :

if (kondisi)
Pernyataan 1;
else
Pernyataan 2;
Untuk if else kita dapat menggunakan contoh 4.1 untuk melihat perbedaan dengan if
sederhana.
Contoh program :
//*-----------------------------------------------------------*
//* Contoh 4.2 : Penggunaan if else dalam *
//* pengambilan keputusan *
//*-----------------------------------------------------------*
#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;
}

Hasil eksekusi : Berapa usia anda ? 16 


Anda Tidak diperkenankan menonton

Apabila kita memasukkan umur lebih dari 17 maka hasil eksekusi yang didapat adalah :

Berapa usia anda ? 21 


Selamat menonton

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


3 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
1.3 Pernyataan if dalam if

Pernyataan if yang terletak dalam if sering disebut nested if atau if bersarang. Salah satu
bentuknya adalah :
if (kondisi1)
pernyataan1;
else if (kondisi2)
pernyataan2;
else if (kondisi3)
pernyataan3;
if (kondisiM)
pernyataanM;
else /*Opsional*/
pernyataanN; /*Opsional*/

Bentuk pernyataan if seperti ini bermanfaat untuk menyeleksi sejumlah kemungkinan


tindakan. Penyeleksian dilakukan secara bertingkat.
Contoh program :
//*------------------------------------------------------*
//* Contoh 4.3 : Pemakaian if bertingkat 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;
// Proses seleksi;
if (kode_hari = = 1)
cout << “Senin” << endl;
if (kode_hari = = 2)
cout << “Selasa” << endl;
if (kode_hari = = 3)
cout << “Rabu” << endl;
if (kode_hari = = 4)
cout << “Kamis” << endl;
if (kode_hari = = 5)
cout << “Jum’at” << endl;
if (kode_hari = = 6)
cout << “Sabtu” << endl;
if (kode_hari = = 7)
cout << “Minggu” << endl;

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


4 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
else
cout << “Kode hari salah” << endl;
}

Hasil eksekusi :

Menentukan hari
1 = Senin 3 = Rabu 5 = Jum’at 7 = Minggu “<< endl;
2 = Selasa 4 = Kamis 6 = Sabtu “ << endl;
Kode hari [1..7] : 2 
Selasa

Program diatas pertama-tama meminta kode hari dimasukkan dari keyboard. Kemudian if
dan else secara bertingkat akan menyeleksi nilai tersebut dan memeberikan nama hari. Bila
anda memasukkan kode hari yang salah maka :

Menentukan hari
1 = Senin 3 = Rabu 5 = Jum’at 7 = Minggu “<< endl;
2 = Selasa 4 = Kamis 6 = Sabtu “ << endl;
Kode hari [1..7] : 9 
Kode hari salah

1.4 Syntax

The syntax of an if...else statement in C++ is −


if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
} else {
// statement(s) will execute if the boolean expression is false
}
If the boolean expression evaluates to true, then the if block of code will be executed,
otherwise else block of code will be executed.

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


5 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
1.5 Flow Diagram

1.5.1 Example

#include <iostream>

using namespace std;

int main () {

// local variable declaration:

int a = 100;

// check the boolean condition

if( a < 20 ) {

// if condition is true then print the following

cout << "a is less than 20;" << endl;

} else {

// if condition is false then print the following

cout << "a is not less than 20;" << endl;

cout << "value of a is : " << a << endl;

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


6 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
return 0;

When the above code is compiled and executed, it produces the following
result −
a is not less than 20;
value of a is : 100

1.6 if...else if...else Statement

An if statement can be followed by an optional else if...else statement,


which is very usefull to test various conditions using single if...else if
statement.

When using if , else if , else statements there are few points to keep in
mind.

 An if can have zero or one else's and it must come after any else if's.

 An if can have zero to many else if's and they must come before the else.

 Once an else if succeeds, none of he remaining else if's or else's will be


tested.

1.6.1 Syntax
The syntax of an if...else if...else statement in C++ is −
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else {
// executes when the none of the above condition is true.
}

1.6.2 Example

#include <iostream>

using namespace std;

int main () {

// local variable declaration:

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


7 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
int a = 100;

// check the boolean condition

if( a == 10 ) {

// if condition is true then print the following

cout << "Value of a is 10" << endl;

} else if( a == 20 ) {

// if else if condition is true

cout << "Value of a is 20" << endl;

} else if( a == 30 ) {

// if else if condition is true

cout << "Value of a is 30" << endl;

} else {

// if none of the conditions is true

cout << "Value of a is not matching" << endl;

cout << "Exact value of a is : " << a << endl;

return 0;

When the above code is compiled and executed, it produces the following result −
Value of a is not matching
Exact value of a is : 100

Program

/* Example Program For If Statement In C++


little drops @ thiyagaraaj.com

Coded By: THIYAGARAAJ MP */

#include<iostream>

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


8 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
#include<conio.h>

using namespace std;


int main()
{
// Variable Declaration
int a;
//Get Input Value
cout<<"Enter the Number :";
cin>>a;
//If Condition Check
if(a > 10)
{
// Block For Condition Success
cout<<a<<" Is Greater than 10";
}
// Wait For Output Screen
getch();
return 0;
}

Sample Output

Enter the Number :15


15 Is Greater than 10

nested-if
A nested if is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement. Yes, C++ allows us to nest if statements
within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1)

// Executes when condition1 is true

if (condition2)

// Executes when condition2 is true

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


9 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
}

Flowchart:

Example:
// C++ program to illustrate nested-if statement
int main()
{
int i = 10;

if (i == 10)
{
// First if statement
if (i < 15)
cout<<"i is smaller than 15";

// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
cout<<"i is smaller than 12 too";
else
cout<<"i is greater than 15";
}

return 0;
}
Output:

i is smaller than 15

i is smaller than 12 too

if-else-if ladder

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


10 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
Here, a user can decide among multiple options. The if statements are executed
from the top down. As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed.
Syntax:
if (condition)

statement;

else if (condition)

statement;

else

statement;

Example:
// C++ program to illustrate if-else-if ladder
#include<iostream>
using namespace std;

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


11 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
int main()
{
int i = 20;

if (i == 10)
cout<<"i is 10";
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
}
Output:

i is 20

#include<iostream>
using namespace std;
int main ()
{
int a;
char finish;

cout<<"Please type finish to end this program"<<endl;


cin>>a;
if (a==finish)
{ cout<<"Thank u for using our program"<<endl;
}

else
{ cout<<"Please type finish only"<<endl;
}

#include <iostream>
#include <string>
using namespace std;
int main ()
{ string answer;

cout<<"Please type finish to end this program"<<endl;


cin >> answer;
if (answer == "finish") // note the quoted string literal
{ cout<<"Thank u for using our program"<<endl;
}
else
{ cout<<"Please type finish only"<<endl;
}
}

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


12 Andra Warastri, ST, MTI http://www.mercubuana.ac.id
Daftar Pustaka
Bӓckman, Kjell. (2012). “Structured Programming with C++”. http://bookboon.com
Deitel, P., Deitel, H. (2013). “C How To Program Seventh Edition”. Pearson Education
publishing as Prentice Hall, New Jersey, USA.
Suprapto. (2008). “Bahasa Pemrograman untuk SMK”. Departemen Pendidikan Nasional.

2018 Algoritma dan Pemrograman Pusat Bahan Ajar dan eLearning


13 Andra Warastri, ST, MTI http://www.mercubuana.ac.id

Anda mungkin juga menyukai