Modul Algoritma & Pemrograman (TM6)
Modul Algoritma & Pemrograman (TM6)
Algoritma dan
Pemrograman
Kondisional IF
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
if (kondisi)
pernyataan
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;
}
Apabila kita memasukkan umur lebih dari 17 maka hasil eksekusi yang didapat adalah :
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*/
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
1.5.1 Example
#include <iostream>
int main () {
int a = 100;
if( a < 20 ) {
} else {
When the above code is compiled and executed, it produces the following
result −
a is not less than 20;
value of a is : 100
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.
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>
int main () {
if( a == 10 ) {
} else if( a == 20 ) {
} else if( a == 30 ) {
} else {
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
#include<iostream>
Sample Output
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)
if (condition2)
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
if-else-if ladder
statement;
else if (condition)
statement;
else
statement;
Example:
// C++ program to illustrate if-else-if ladder
#include<iostream>
using namespace std;
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;
else
{ cout<<"Please type finish only"<<endl;
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{ string answer;