SlideShare a Scribd company logo
1 of 4
Struktur Kontrol Percabangan pada C++
Struktur Kontrol Pecabangan merupakan suatu algoritma program memiliki suatu kondisi yang
dimana kondisi tersebutlah yang akan menentukan perintah-perintah yang akan dijalankan oleh
suatu program. Perintah dalam suatu kondisi akan dijalankan ketika kondisi tersebut bernilai
benar, dan sebaliknya apabila kondisi bernilai salah maka perintah didalamnya tidak akan
dijalankan. Ada beberapa macam Struktur Kontol Pecabangan yang ada didalam C++, yaitu :
Pecabangan if : Merupakan struktur kontrol pecabangan yang memiliki sebuah kondisi. Bentuk
umum penulisannya adalah sebagai berikut.
if(kondisi)
{
statement;
}
/*
Contoh Program IF
Kondisi benar dan perintah dijalankan
*/
#include <iostream.h>
#include <conio.h>
main()
{
int a = 10;
int b = 7;
if(a>=b)
{
a = 7;
b = 10;
}
cout<<"Nilai A : "<<a<<endl;
cout<<"Nilai B : "<<b<<endl;
getch();
}
Output contoh program if
Percabangan if-else : Merupakan struktur kontrol pecabangan yang memiliki sebuah kondisi
yang apabila kondisi tersebut benar maka perintah yang didalamnya akan dijalankan dan apabila
kondisinya salah maka ada perintah lainnya yang akan dijalankan oleh program. Bentuk umum
penulisannya adalah sebagai berikut.
if(kondisi)
{
statement;
}
else
{
statement;
}
/*
Contoh Program IF-ELSE
Kondisi benar dan perintah dijalankan
*/
#include <iostream.h>
#include <conio.h>
main()
{
int x;
int angka = 10;
cout<<"Angka 1 - 10"<<endl;
cout<<"Masukan Tebakan anda : ";cin>>x;
if(x==angka)
{
cout<<"Tebakan anda benar"<<endl;
cout<<"Selamat!";
}
else
{
cout<<"Maaf tebakan anda salah!";
}
getch();
}
Output contoh program if-else
Percabangan if-else if-else : Merupakan struktur kontrol pecabangan yang memiliki banyak
kondisi. Bentuk umum penulisannya adalah sebagai berikut.
if(kondisi1)
{
statement;
}
else if(kondisi2)
{
statement;
}
else
{
statement;
}
/*
Contoh program IF-ELSE IF-ELSE
*/
#include <iostream.h>
#include <conio.h>
main()
{
int x;
int angka = 6;
cout<<"Angka 1 - 10"<<endl;
cout<<"Masukan Tebakan anda : ";cin>>x;
if(x==angka)
{
cout<<"Tebakan anda benar"<<endl;
cout<<"Selamat!";
}
else if(x>angka)
{
cout<<"Tebakan anda terlalu besar";
}
else
{
cout<<"Tebakan anda terlalu kecil";
}
getch();
}
Output contoh program if-else if-else
Percabangan switch : Merupakan struktur kontrol pecabangan yang juga memiliki banyak
kondisi, akan tetapi kondisi dalam percabangan switch ini bertipe numerik. Bentuk umum
penulisannya adalah sebagai berikut.
switch(kondisi)
{
case konstanta_1:
statement;
break;
case konstanta_2:
statement;
break;
default:
statement;
}
NB : default berfungsi untuk menjalankan perintah default/umum/dasar apabila kondisi tidak
sesuai dengan nilai case yang ada.
/*
Contoh program SWITCH
*/
#include <iostream.h>
#include <conio.h>
main()
{
int nilai;
cout<<"Masukan Nilai anda 0 - 10 : ";cin>>nilai;
cout<<"Anda masuk kategori ";
switch(nilai)
{
case 10:
case 9:
cout<<"A";
break;
case 8:
case 7:
cout<<"B";
break;
case 6:
case 5:
cout<<"C";
break;
case 4:
case 3:
case 2:
case 1:
case 0:
cout<<"D";
break;
default:
cout<<"nilai anda tidak masuk kategori!";
}
getch();
}
Output contoh program switch

More Related Content

Recently uploaded

Pertemuan 3-bioavailabilitas-dan-bioekivalensi.ppt
Pertemuan 3-bioavailabilitas-dan-bioekivalensi.pptPertemuan 3-bioavailabilitas-dan-bioekivalensi.ppt
Pertemuan 3-bioavailabilitas-dan-bioekivalensi.pptNabilahKhairunnisa6
 
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKA
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKAPPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKA
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKARenoMardhatillahS
 
Teknik Menjawab Kertas P.Moral SPM 2024.pptx
Teknik Menjawab Kertas P.Moral SPM  2024.pptxTeknik Menjawab Kertas P.Moral SPM  2024.pptx
Teknik Menjawab Kertas P.Moral SPM 2024.pptxwongcp2
 
MA Kelas XII Bab 1 materi musik mkontemnporerFase F.pdf
MA Kelas XII  Bab 1 materi musik mkontemnporerFase F.pdfMA Kelas XII  Bab 1 materi musik mkontemnporerFase F.pdf
MA Kelas XII Bab 1 materi musik mkontemnporerFase F.pdfcicovendra
 
rpp bangun-ruang-sisi-datar kelas 8 smp.pdf
rpp bangun-ruang-sisi-datar kelas 8 smp.pdfrpp bangun-ruang-sisi-datar kelas 8 smp.pdf
rpp bangun-ruang-sisi-datar kelas 8 smp.pdfGugunGunawan93
 
Tina fitriyah - Uji Sampel statistik.pptx
Tina fitriyah - Uji Sampel statistik.pptxTina fitriyah - Uji Sampel statistik.pptx
Tina fitriyah - Uji Sampel statistik.pptxTINAFITRIYAH
 
PLaN & INTERVENSI untuk sekolah yang memerlukan
PLaN & INTERVENSI untuk sekolah yang memerlukanPLaN & INTERVENSI untuk sekolah yang memerlukan
PLaN & INTERVENSI untuk sekolah yang memerlukanssuserc81826
 
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptx
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptxJurnal Dwi mingguan modul 1.2-gurupenggerak.pptx
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptxBambang440423
 
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi Online
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi OnlinePPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi Online
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi OnlineMMario4
 
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptx
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptxKeberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptx
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptxLeniMawarti1
 
Sejarah Perkembangan Teori Manajemen.ppt
Sejarah Perkembangan Teori Manajemen.pptSejarah Perkembangan Teori Manajemen.ppt
Sejarah Perkembangan Teori Manajemen.pptssuser940815
 
modul 1.2 guru penggerak angkatan x Bintan
modul 1.2 guru penggerak angkatan x Bintanmodul 1.2 guru penggerak angkatan x Bintan
modul 1.2 guru penggerak angkatan x BintanVenyHandayani2
 
Modul Ajar Matematika Kelas 2 Fase A Kurikulum Merdeka
Modul Ajar Matematika Kelas 2 Fase A Kurikulum MerdekaModul Ajar Matematika Kelas 2 Fase A Kurikulum Merdeka
Modul Ajar Matematika Kelas 2 Fase A Kurikulum MerdekaAbdiera
 
Buku Saku Layanan Haji Ramah Lansia 2.pdf
Buku Saku Layanan Haji Ramah Lansia 2.pdfBuku Saku Layanan Haji Ramah Lansia 2.pdf
Buku Saku Layanan Haji Ramah Lansia 2.pdfWahyudinST
 
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdf
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdfAPRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdf
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdfVenyHandayani2
 
PERTEMUAN 9 KESEIM 3 SEKTOR.............
PERTEMUAN 9 KESEIM 3 SEKTOR.............PERTEMUAN 9 KESEIM 3 SEKTOR.............
PERTEMUAN 9 KESEIM 3 SEKTOR.............SenLord
 
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptx
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptxMATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptx
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptxrofikpriyanto2
 
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...jumadsmanesi
 
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 Tesalonika
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 TesalonikaMateri Kelas Online Ministry Learning Center - Bedah Kitab 1 Tesalonika
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 TesalonikaSABDA
 
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.aechacha366
 

Recently uploaded (20)

Pertemuan 3-bioavailabilitas-dan-bioekivalensi.ppt
Pertemuan 3-bioavailabilitas-dan-bioekivalensi.pptPertemuan 3-bioavailabilitas-dan-bioekivalensi.ppt
Pertemuan 3-bioavailabilitas-dan-bioekivalensi.ppt
 
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKA
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKAPPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKA
PPT TEKS TANGGAPAN KELAS 7 KURIKUKULM MERDEKA
 
Teknik Menjawab Kertas P.Moral SPM 2024.pptx
Teknik Menjawab Kertas P.Moral SPM  2024.pptxTeknik Menjawab Kertas P.Moral SPM  2024.pptx
Teknik Menjawab Kertas P.Moral SPM 2024.pptx
 
MA Kelas XII Bab 1 materi musik mkontemnporerFase F.pdf
MA Kelas XII  Bab 1 materi musik mkontemnporerFase F.pdfMA Kelas XII  Bab 1 materi musik mkontemnporerFase F.pdf
MA Kelas XII Bab 1 materi musik mkontemnporerFase F.pdf
 
rpp bangun-ruang-sisi-datar kelas 8 smp.pdf
rpp bangun-ruang-sisi-datar kelas 8 smp.pdfrpp bangun-ruang-sisi-datar kelas 8 smp.pdf
rpp bangun-ruang-sisi-datar kelas 8 smp.pdf
 
Tina fitriyah - Uji Sampel statistik.pptx
Tina fitriyah - Uji Sampel statistik.pptxTina fitriyah - Uji Sampel statistik.pptx
Tina fitriyah - Uji Sampel statistik.pptx
 
PLaN & INTERVENSI untuk sekolah yang memerlukan
PLaN & INTERVENSI untuk sekolah yang memerlukanPLaN & INTERVENSI untuk sekolah yang memerlukan
PLaN & INTERVENSI untuk sekolah yang memerlukan
 
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptx
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptxJurnal Dwi mingguan modul 1.2-gurupenggerak.pptx
Jurnal Dwi mingguan modul 1.2-gurupenggerak.pptx
 
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi Online
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi OnlinePPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi Online
PPT PERLINDUNGAN KONSUMEN .Pengertian Transaksi Online
 
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptx
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptxKeberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptx
Keberagaman-Peserta-Didik-dalam-Psikologi-Pendidikan.pptx
 
Sejarah Perkembangan Teori Manajemen.ppt
Sejarah Perkembangan Teori Manajemen.pptSejarah Perkembangan Teori Manajemen.ppt
Sejarah Perkembangan Teori Manajemen.ppt
 
modul 1.2 guru penggerak angkatan x Bintan
modul 1.2 guru penggerak angkatan x Bintanmodul 1.2 guru penggerak angkatan x Bintan
modul 1.2 guru penggerak angkatan x Bintan
 
Modul Ajar Matematika Kelas 2 Fase A Kurikulum Merdeka
Modul Ajar Matematika Kelas 2 Fase A Kurikulum MerdekaModul Ajar Matematika Kelas 2 Fase A Kurikulum Merdeka
Modul Ajar Matematika Kelas 2 Fase A Kurikulum Merdeka
 
Buku Saku Layanan Haji Ramah Lansia 2.pdf
Buku Saku Layanan Haji Ramah Lansia 2.pdfBuku Saku Layanan Haji Ramah Lansia 2.pdf
Buku Saku Layanan Haji Ramah Lansia 2.pdf
 
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdf
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdfAPRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdf
APRESIASI SURAT DAN MASUKAN CGP ANGKATAN X.pdf
 
PERTEMUAN 9 KESEIM 3 SEKTOR.............
PERTEMUAN 9 KESEIM 3 SEKTOR.............PERTEMUAN 9 KESEIM 3 SEKTOR.............
PERTEMUAN 9 KESEIM 3 SEKTOR.............
 
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptx
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptxMATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptx
MATERI 1_ Modul 1 dan 2 Konsep Dasar IPA SD jadi.pptx
 
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...
UNGGAH PEGANGAN LOKAKARYA DAN PENDAMPINGAN INDIVIDU DALAM KEGIATAN PEMBEKALAN...
 
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 Tesalonika
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 TesalonikaMateri Kelas Online Ministry Learning Center - Bedah Kitab 1 Tesalonika
Materi Kelas Online Ministry Learning Center - Bedah Kitab 1 Tesalonika
 
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.
PUEBI.bahasa Indonesia/pedoman umum ejaan bahasa Indonesia pptx.
 

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Struktur kontrol percabangan pada c++ taslim

  • 1. Struktur Kontrol Percabangan pada C++ Struktur Kontrol Pecabangan merupakan suatu algoritma program memiliki suatu kondisi yang dimana kondisi tersebutlah yang akan menentukan perintah-perintah yang akan dijalankan oleh suatu program. Perintah dalam suatu kondisi akan dijalankan ketika kondisi tersebut bernilai benar, dan sebaliknya apabila kondisi bernilai salah maka perintah didalamnya tidak akan dijalankan. Ada beberapa macam Struktur Kontol Pecabangan yang ada didalam C++, yaitu : Pecabangan if : Merupakan struktur kontrol pecabangan yang memiliki sebuah kondisi. Bentuk umum penulisannya adalah sebagai berikut. if(kondisi) { statement; } /* Contoh Program IF Kondisi benar dan perintah dijalankan */ #include <iostream.h> #include <conio.h> main() { int a = 10; int b = 7; if(a>=b) { a = 7; b = 10; } cout<<"Nilai A : "<<a<<endl; cout<<"Nilai B : "<<b<<endl; getch(); } Output contoh program if Percabangan if-else : Merupakan struktur kontrol pecabangan yang memiliki sebuah kondisi yang apabila kondisi tersebut benar maka perintah yang didalamnya akan dijalankan dan apabila kondisinya salah maka ada perintah lainnya yang akan dijalankan oleh program. Bentuk umum penulisannya adalah sebagai berikut. if(kondisi) { statement;
  • 2. } else { statement; } /* Contoh Program IF-ELSE Kondisi benar dan perintah dijalankan */ #include <iostream.h> #include <conio.h> main() { int x; int angka = 10; cout<<"Angka 1 - 10"<<endl; cout<<"Masukan Tebakan anda : ";cin>>x; if(x==angka) { cout<<"Tebakan anda benar"<<endl; cout<<"Selamat!"; } else { cout<<"Maaf tebakan anda salah!"; } getch(); } Output contoh program if-else Percabangan if-else if-else : Merupakan struktur kontrol pecabangan yang memiliki banyak kondisi. Bentuk umum penulisannya adalah sebagai berikut. if(kondisi1) { statement; } else if(kondisi2) { statement; } else {
  • 3. statement; } /* Contoh program IF-ELSE IF-ELSE */ #include <iostream.h> #include <conio.h> main() { int x; int angka = 6; cout<<"Angka 1 - 10"<<endl; cout<<"Masukan Tebakan anda : ";cin>>x; if(x==angka) { cout<<"Tebakan anda benar"<<endl; cout<<"Selamat!"; } else if(x>angka) { cout<<"Tebakan anda terlalu besar"; } else { cout<<"Tebakan anda terlalu kecil"; } getch(); } Output contoh program if-else if-else Percabangan switch : Merupakan struktur kontrol pecabangan yang juga memiliki banyak kondisi, akan tetapi kondisi dalam percabangan switch ini bertipe numerik. Bentuk umum penulisannya adalah sebagai berikut. switch(kondisi) { case konstanta_1: statement; break; case konstanta_2: statement; break; default:
  • 4. statement; } NB : default berfungsi untuk menjalankan perintah default/umum/dasar apabila kondisi tidak sesuai dengan nilai case yang ada. /* Contoh program SWITCH */ #include <iostream.h> #include <conio.h> main() { int nilai; cout<<"Masukan Nilai anda 0 - 10 : ";cin>>nilai; cout<<"Anda masuk kategori "; switch(nilai) { case 10: case 9: cout<<"A"; break; case 8: case 7: cout<<"B"; break; case 6: case 5: cout<<"C"; break; case 4: case 3: case 2: case 1: case 0: cout<<"D"; break; default: cout<<"nilai anda tidak masuk kategori!"; } getch(); } Output contoh program switch