0% menganggap dokumen ini bermanfaat (0 suara)
28 tayangan16 halaman

Percabangan dalam JavaScript

Ringkasan dokumen tersebut adalah: (1) Percabangan JavaScript digunakan untuk mengatur aliran informasi berdasarkan hasil komputasi, (2) Terdapat dua jenis percabangan yaitu if..else dan switch, (3) If..else digunakan untuk mengevaluasi kondisi dan mengeksekusi kode tertentu jika kondisi terpenuhi atau tidak.

Diunggah oleh

Vi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PPTX, PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
28 tayangan16 halaman

Percabangan dalam JavaScript

Ringkasan dokumen tersebut adalah: (1) Percabangan JavaScript digunakan untuk mengatur aliran informasi berdasarkan hasil komputasi, (2) Terdapat dua jenis percabangan yaitu if..else dan switch, (3) If..else digunakan untuk mengevaluasi kondisi dan mengeksekusi kode tertentu jika kondisi terpenuhi atau tidak.

Diunggah oleh

Vi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PPTX, PDF, TXT atau baca online di Scribd

Percabangan

JavaScript
Percabangan JavaScript

Untuk membuat suatu halaman yang dinamis dan interaktif, perancang halaman

Web membutuhkan perintah-perintah yang dapat mengatur aliran dari informasi.

Berdasarkan hasil komputasi yang telah dilakukan, JavaScript akan membuat

keputusan jalur mana yang akan dieksekusi. Pada dasarnya dalam JavaScript

terdapat dua macam pernyataan percabangan yaitu if..else dan switch


If. Else (Percabangan JavaScript)
If (kondisi)
Pernyataan ini digunakan untuk menguji
{
sebuah kondisi dan kemudian //pernyataan1 diekseskusi
//bila kondisi terpenuhi
mengeksekusi pernyataan tertentu bila
}
kondisi tersebut terpenuhi, dan Else
{
mengeksekusi pernyataan lain bila
//pertaan2 diekseskusi

kondisi tersebut tidak terpenuhi. //bila kondisi tidak terpenuhi


}
If. Else (Percabangan JavaScript)
if (kondisi)
 Kondisi adalah ekspresi JavaScript yang {
mana hasil evaluasinya memiliki nilai //pernyataan1 dieksekusi
//bila kondisi1 terpenuhi
Boolean true atau false }
Else if (kondisi2)
{
 Untuk kasus yang melibatkan lebih //pernyataan2 dieksekusi
//bila kondisi1 tidak terpenuhi
banyak kondisi, maka kita dapat }
Else
meletakkan pernyataan if lain {
//pernyataan3 dieksekusi
setelah else //bila kondisi2 tidak terpenuhi
}
Contoh Percabangan
JavaScript
Contoh Kasus
 Perusahaan Travel ”Bintang Abadi” mempunyai armada dengan tujuan :

Apabila seorang pemesan sudah menjadi anggota/member Travel Bintang Abadi


maka akan
mendapatkan diskon 10 %
Buat Program dengan menggunakan Javascript untuk menyelesaikan masalah tersebut.
Dengan ketentuan sebagai berikut :

Input : Nama pemesan


Tujuan
Jumlah Tiket
Member

Output : Harga Tiket


Sub Total
Diskon
Total Bayar
Contoh Studi Kasus di File Word
Percabangan Majemuk JavaScript

 Percabangan majemuk adalah suatu percabangan yang dapat melibatkan lebih

dari 1 kondisi didalam percabangannya. Biasanya percabangan sepert ini

menggunakan operator tambahan seperti AND, OR dan sebagainya.


Contoh Studi Kasus di File Word
SWITCH JAVASCRIPT

Selain menggunakan if..else, percabangan juga dapat ditangani dengan perintah

switch. Dengn kata lain pernyataan switch digunakan untuk menyederhanakan

pernyataan if..else yang terlalu banyak.


Contoh Studi Kasus di File Word
LATIHAN
 Buatlah halaman seperti berikut, yang mencakup semua materi yang terdapat pada modul ini. Perusahaan

Travel Bintang Abadi memiliki armada dengan tujuan Jakarta, Solo dan Surabaya. Setiap tujuan memiliki kelas

Eksekutif, Bisnis dan Ekonomi.

Diskon 10% diberikan apabila pemesan tiket merupakan anggota Travel Bintang Abadi.
Input : Nama Pemesan, Tujuan, Kelas, Banyak Tiket dan Status Member/Bukan
Output : Harga Tiket, Subtotal, Diskon dan Total Bayar
TAMPILAN

Common questions

Didukung oleh AI

Logical operators such as AND (&&) and OR (||) in JavaScript are used within conditional structures to form compound branching. They allow the combination of multiple conditions within a single if statement. An AND operator ensures that all combined conditions must be true for the block to execute, while an OR operator requires only one of the conditions to be true. This enables more complex decision-making within scripts, allowing for sophisticated control over program execution paths.

Nested if..else statements allow the evaluation of multiple conditions where each subsequent condition is contingent on the falsification of the preceding ones. This structure enables checking several layers of conditions efficiently. For example, after checking the primary condition with an initial if statement, further specific conditions can be checked using nested if statements within the else block, thus allowing for detailed assessment and actions based on multiple criteria, as seen in complex decision-making tasks.

When choosing between if..else and switch statements in JavaScript, developers should consider the complexity and readability of their code, the nature and number of conditions, and the variable type being evaluated. If conditions are based on a single variable's discrete values, and there are numerous branches, a switch statement may be cleaner and more efficient. Conversely, for conditions involving complex logic or ranges of values, if..else statements may be more suitable due to their flexibility.

JavaScript offers two primary mechanisms for controlling program flow: the if..else statement and the switch statement. The if..else statement allows for the execution of certain code blocks based on the Boolean evaluation of conditions. Multiple conditions can be handled by combining multiple if..else statements. The switch statement simplifies managing multiple conditional branches typically governed by the same variable.

The switch statement simplifies complex conditional logic by allowing multiple conditions to be evaluated on a single variable's value, which can reduce the complexity and redundancy associated with numerous if..else statements. With switch, each case corresponds to a particular value of the variable in question, effectively organizing the logic into neat branches, making it more readable and maintainable compared to chaining many if..else conditions.

A potential use case for an optimal switch statement would be a scenario where a program assigns output based on a user's selection from a fixed set of categories or types, such as menu options or user roles. For instance, in an application where different user types—like guest, member, or admin—lead to differing levels of access or functionality, a switch statement can efficiently replace a long series of if..else checks by matching the user type directly to a case block corresponding to their assigned tasks.

In Bintang Abadi Travel's JavaScript program, a customer's membership status is crucial as members are eligible for a 10% discount on their ticket purchase. The program takes input such as the name, destination, number of tickets, and membership status, calculates the subtotal, applies the discount (if applicable), and determines the total amount payable. This approach not only demonstrates the handling of input and conditional logic but also illustrates real-world usage of JavaScript's dynamic functionalities.

The else block in a compound if..else statement serves as a default path, catching any cases not explicitly addressed by previous conditions. This ensures that the program handles all possible inputs systematically, preventing logical gaps in execution flow. It is crucial in maintaining control over how an application reacts when conditions are unmet, thereby enhancing the robustness and reliability of the script.

Nested if..else statements in JavaScript become non-ideal when they create deeply nested structures leading to hard-to-read and maintain code. They may also lead to increased cognitive load and potential for errors. Alternatives include using switch statements if the logic involves multiple cases revolving around a single variable, or refactoring the code into separate functions that handle specific logical branches. Moreover, logical operators can sometimes simplify chains of conditions effectively.

Switch statements improve code readability by neatly organizing conditions in a way that is easy to follow and understand. Each case within a switch statement explicitly defines how the program responds to a specific value, reducing the complexity and cognitive overload associated with interpreting multiple if..else conditions. This structure also visually separates each condition and its corresponding actions, making the logic clearer and reducing the likelihood of errors during code maintenance.

Anda mungkin juga menyukai