SlideShare una empresa de Scribd logo
1 de 31
Struktur Kawalan (control structures) dalam C++ : if…end if, if…then…else, if…then…else if dan switch…case
Struktur Pilihan  if..end if ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Sintaks: If   (condition) statement; Atau If (condition) { statement 1; statement 2; …  statement n; }
Struktur Pilihan  if..end if #include<iostream.h> void main(){ int nombor = 1; if (nombor == 1)   cout<<&quot;pembolehubah nombor bernilai 1 &quot;<<endl; cout<<&quot;Pernyataan ini di print selepas kenyataan if &quot;<<endl; } Contoh output Kenyataan if
Struktur dwipilihan  if – then - else ,[object Object],[object Object],[object Object],[object Object],[object Object],Sintaks: If (condition) statement a; else statement b; Atau If (condition) { statement a 1 ; statement a 2 ; …  statement a n; } else { statement b 1; statement b 2; …  statement b n; }
[object Object],[object Object],[object Object],[object Object],[object Object],Struktur dwipilihan  if – then - else
Struktur dwipilihan  if-then - else #include<iostream.h> void main(){ char jawapan; cout<<&quot;Apakah ibukota malaysia? &quot;<<endl; cout<<&quot;a. Jarkarta &quot;<<endl; cout<<&quot;b. Kuala Lumpur &quot;<<endl; cout<<&quot;Jawapan: &quot;; cin>>jawapan; if (jawapan == 'b')   cout<<&quot;Anda betul! tahniah. &quot;<<endl; else  cout<<&quot;Jawapan anda salah, cuba lagi &quot;<<endl; } Contoh output jawapan betul Kenyataan if Contoh output jawapan salah
Tambahan… ,[object Object],[object Object]
Struktur multipilihan  if-else-if atau nested if-else ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Sintaks: If  (syarat 1) statement a; else if (syarat 2) statement b; else if (syarat 3) statement c; … Atau If  (syarat 1) { statement a 1 ; …  statement a n; } else if (syarat 2) { statement b 1; …  statement b n; } …
7 #include  <iostream.h> Enter two integers, and I will tell you  the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 5 6 8 9 10 int  main() 11 { 12   int  num1, num2; 13 14   cout << &quot;Enter two integers, and I will tell you&quot; 15   << &quot;the relationships they satisfy: &quot;; 16   cin >> num1 >> num2;  // read two integers 17 18   if  ( num1 == num2 ) 19   cout << num1 << &quot; is equal to &quot; << num2 << endl; 20 21   else   if  ( num1 != num2 ) 22   cout << num1 << &quot; is not equal to &quot; << num2 << endl; 23 24   else if  ( num1 < num2 ) 25   cout << num1 << &quot; is less than &quot; << num2 << endl; 26 27   else if  ( num1 > num2 ) 28   cout << num1 << &quot; is greater than &quot; << num2 << endl; 29 30   else   if  ( num1 <= num2 ) 31   cout << num1 << &quot; is less than or equal to &quot; 32   << num2 << endl; 33 The  if  statements test the truth of the condition.  If it is  true , body of  if  statement is executed.  If not, body is skipped. To include multiple statements in a body, delineate them with braces  {} .
Struktur multipilihan  if-else-if if ( markah < 40) gred = “F”; else if ( markah < 50) gred = “E”; else if (markah < 60) gred = “D”; else if ( markah < 70) gred = “C”; else if (markah < 80) gred = “B”; else gred =”A”; end if
Struktur Pilihan Bersarang ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Sintaks: If  (syarat 1) if (syarat 2) if (syarat 3) statement M; else statement 3; else statement 2; else statement 1;
#include <iostream.h> void main() { int markah = 0; cout<<&quot;Sila masukkan markah anda :&quot;; cin>>markah; if ( markah > 40){ if (markah > 60){ if (markah > 80){   cout <<&quot;Gred anda ialah A”; } else{ cout <<&quot;Gred anda ialah B”; } } else{ cout <<&quot;Gred anda ialah C”; } } else{ cout <<&quot;Gred anda ialah F”; } }
Struktur multipilihan  switch dan case ,[object Object],[object Object]
Pernyataan  if - else if  boleh digantikan dengan  switch dan case  sebagaimana berikut : ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Struktur multipilihan  switch dan case switch ( pilihan )  { case  1 :  cout<<“Anda pilih nombor 1”; break; case  2 : cout<<“Anda pilih nombor 2”; break; case  3 : cout<<“Anda pilih nombor 3”; break; default  : cout<<“Anda pilih nombor selain daripada 1,2  atau 3”; }
Kenyataan lompatan-Break & Continue ,[object Object],[object Object],[object Object],[object Object],[object Object]
Cth: break ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Cth: continue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lanjutan Aturcara Berstruktur dalam bahasa pengaturcaraan C++ Minggu 8
Struktur Gelung ,[object Object],[object Object],[object Object],[object Object]
Struktur Pengulangan  while ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Struktur Pengulangan  while
Struktur Pengulangan  while #include<iostream.h> void main(){ int kira = 1; while (kira <= 5) { cout<<kira<<endl; kira = kira + 1; } cout<<endl; } Contoh output While loop
Struktur gelung (do-while) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Struktur Pengulangan  do-while #include<iostream.h> void main(){ char jawapan; int bilangan = 0; do{ cout<<&quot;Ini adalah gelungan kali &quot;<<bilangan<<endl; ++bilangan; }while(bilangan < 4); } Contoh output Do-While loop
Struktur gelung (for) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Struktur Pengulangan  for #include<iostream.h> void main(){ for (int nombor = 0; nombor < 6; ++nombor){ cout<<nombor<<endl; } } Contoh output For loop
Terima kasih Banyakkanlah latihan untuk menambah kemahiran pengaturcaraan struktur dan kawalan

Más contenido relacionado

Más de Kamarudin Jaafar (20)

Konstruk 6 kumpulan 4.(a11)doc
Konstruk 6   kumpulan 4.(a11)docKonstruk 6   kumpulan 4.(a11)doc
Konstruk 6 kumpulan 4.(a11)doc
 
Konstruk 6 kumpulan 4(1).(a10)doc
Konstruk 6   kumpulan 4(1).(a10)docKonstruk 6   kumpulan 4(1).(a10)doc
Konstruk 6 kumpulan 4(1).(a10)doc
 
Pmpp.1
Pmpp.1Pmpp.1
Pmpp.1
 
Buku panduan pentaksiran_kemahiran_berfikir_aras_tinggi (1)
Buku panduan pentaksiran_kemahiran_berfikir_aras_tinggi  (1)Buku panduan pentaksiran_kemahiran_berfikir_aras_tinggi  (1)
Buku panduan pentaksiran_kemahiran_berfikir_aras_tinggi (1)
 
Instrumen pppm.2
Instrumen pppm.2Instrumen pppm.2
Instrumen pppm.2
 
Instrumen pppm.1
Instrumen pppm.1Instrumen pppm.1
Instrumen pppm.1
 
Info 5 minit pppm bhg2
Info 5 minit pppm   bhg2Info 5 minit pppm   bhg2
Info 5 minit pppm bhg2
 
Info 5 minit pppm bhg 5
Info 5 minit pppm   bhg 5Info 5 minit pppm   bhg 5
Info 5 minit pppm bhg 5
 
Info 5 minit pppm bhg 4
Info 5 minit pppm   bhg 4Info 5 minit pppm   bhg 4
Info 5 minit pppm bhg 4
 
Brosur pppm 2
Brosur pppm 2Brosur pppm 2
Brosur pppm 2
 
Inst 2 pd p kbat ithink
Inst 2  pd p kbat ithinkInst 2  pd p kbat ithink
Inst 2 pd p kbat ithink
 
Protim
ProtimProtim
Protim
 
Analisis upsr
Analisis upsrAnalisis upsr
Analisis upsr
 
Program kecemerlangan bi
Program kecemerlangan biProgram kecemerlangan bi
Program kecemerlangan bi
 
Tarwqat dan suluk
Tarwqat dan sulukTarwqat dan suluk
Tarwqat dan suluk
 
Prinsip ilmu tasawwuf
Prinsip ilmu tasawwufPrinsip ilmu tasawwuf
Prinsip ilmu tasawwuf
 
Al tazkiyat al-nafs
Al tazkiyat al-nafsAl tazkiyat al-nafs
Al tazkiyat al-nafs
 
Al hal dan al-maqam
Al hal dan al-maqamAl hal dan al-maqam
Al hal dan al-maqam
 
Syuhanna(psm viva)
Syuhanna(psm viva)Syuhanna(psm viva)
Syuhanna(psm viva)
 
Slq 4072
Slq 4072Slq 4072
Slq 4072
 

Spm2102 minggu67

  • 1. Struktur Kawalan (control structures) dalam C++ : if…end if, if…then…else, if…then…else if dan switch…case
  • 2.
  • 3.
  • 4. Struktur Pilihan if..end if #include<iostream.h> void main(){ int nombor = 1; if (nombor == 1) cout<<&quot;pembolehubah nombor bernilai 1 &quot;<<endl; cout<<&quot;Pernyataan ini di print selepas kenyataan if &quot;<<endl; } Contoh output Kenyataan if
  • 5.
  • 6.
  • 7. Struktur dwipilihan if-then - else #include<iostream.h> void main(){ char jawapan; cout<<&quot;Apakah ibukota malaysia? &quot;<<endl; cout<<&quot;a. Jarkarta &quot;<<endl; cout<<&quot;b. Kuala Lumpur &quot;<<endl; cout<<&quot;Jawapan: &quot;; cin>>jawapan; if (jawapan == 'b') cout<<&quot;Anda betul! tahniah. &quot;<<endl; else cout<<&quot;Jawapan anda salah, cuba lagi &quot;<<endl; } Contoh output jawapan betul Kenyataan if Contoh output jawapan salah
  • 8.
  • 9.
  • 10. 7 #include <iostream.h> Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 5 6 8 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << &quot;Enter two integers, and I will tell you&quot; 15 << &quot;the relationships they satisfy: &quot;; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) 19 cout << num1 << &quot; is equal to &quot; << num2 << endl; 20 21 else if ( num1 != num2 ) 22 cout << num1 << &quot; is not equal to &quot; << num2 << endl; 23 24 else if ( num1 < num2 ) 25 cout << num1 << &quot; is less than &quot; << num2 << endl; 26 27 else if ( num1 > num2 ) 28 cout << num1 << &quot; is greater than &quot; << num2 << endl; 29 30 else if ( num1 <= num2 ) 31 cout << num1 << &quot; is less than or equal to &quot; 32 << num2 << endl; 33 The if statements test the truth of the condition. If it is true , body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {} .
  • 11. Struktur multipilihan if-else-if if ( markah < 40) gred = “F”; else if ( markah < 50) gred = “E”; else if (markah < 60) gred = “D”; else if ( markah < 70) gred = “C”; else if (markah < 80) gred = “B”; else gred =”A”; end if
  • 12.
  • 13. #include <iostream.h> void main() { int markah = 0; cout<<&quot;Sila masukkan markah anda :&quot;; cin>>markah; if ( markah > 40){ if (markah > 60){ if (markah > 80){ cout <<&quot;Gred anda ialah A”; } else{ cout <<&quot;Gred anda ialah B”; } } else{ cout <<&quot;Gred anda ialah C”; } } else{ cout <<&quot;Gred anda ialah F”; } }
  • 14.
  • 15.
  • 16. Struktur multipilihan switch dan case switch ( pilihan ) { case 1 : cout<<“Anda pilih nombor 1”; break; case 2 : cout<<“Anda pilih nombor 2”; break; case 3 : cout<<“Anda pilih nombor 3”; break; default : cout<<“Anda pilih nombor selain daripada 1,2 atau 3”; }
  • 17.
  • 18.
  • 19.
  • 20. Lanjutan Aturcara Berstruktur dalam bahasa pengaturcaraan C++ Minggu 8
  • 21.
  • 22.
  • 23.
  • 24. Struktur Pengulangan while #include<iostream.h> void main(){ int kira = 1; while (kira <= 5) { cout<<kira<<endl; kira = kira + 1; } cout<<endl; } Contoh output While loop
  • 25.
  • 26.
  • 27. Struktur Pengulangan do-while #include<iostream.h> void main(){ char jawapan; int bilangan = 0; do{ cout<<&quot;Ini adalah gelungan kali &quot;<<bilangan<<endl; ++bilangan; }while(bilangan < 4); } Contoh output Do-While loop
  • 28.
  • 29.
  • 30. Struktur Pengulangan for #include<iostream.h> void main(){ for (int nombor = 0; nombor < 6; ++nombor){ cout<<nombor<<endl; } } Contoh output For loop
  • 31. Terima kasih Banyakkanlah latihan untuk menambah kemahiran pengaturcaraan struktur dan kawalan