Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Pertemuan 05 Selection Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:

Similar presentations


Presentation on theme: "1 Pertemuan 05 Selection Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:"— Presentation transcript:

1 1 Pertemuan 05 Selection Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:

2 2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan penggunaan perintah seleksi

3 3 Outline Materi The If…Then...Else Statement Nested If Statement The Select Case Statement

4 4 The If…Then...Else Statement The If…Then…Else statement enables a program to handle situations having two outcomes. The statement has three parts: 1.The condition. 2.The statements to perform for the first outcome. 3.The statements to perform for the second outcome. Syntax and Action of If…Then…Else –The If…Then…Else statement has the following syntax: If condition Then –Statementblock1 Else –Statementblock2 End If

5 5 The If…Then...Else Statement –Run Time: The Effect of the If…Then…Else Statement True: the computer executes the statements in statementblock1. False: the computer skips to Else and executes the statements in statementblock2. –Meta Statements Describe compound statement with a single phrase. –Problem Solving and Pseudocode Problem solving is the process of writing code to perform a required task. Pseudocode is an English-like outline of the logical for program code. –Ex. Request address from user Receive input in a textbox

6 6 The If…Then...Else Statement –Using Logical Expressions in If…Then…Else Statements The condition of an If…Then…Else statement may be a logical expression. –Ex. (YearsExperience>5) And (NumberOfLanguages>=3) –If…Then Sometimes only one of two outcomes requires processing. –If condition Then statementblock –End If

7 7 Nested If Statement If statements may contain one within another. If (X<Y) And (Y<Z) Then AscOrDesc = “ascending” Else If (X>Y) And (Y>Z) Then AscOrDesc = “descending” Else AscOrDesc = “neither ascending nor descending” End If

8 8 The Select Case Statement Can handle conditions with multiple outcomes. –Syntax and Action of Select Case Select Case testexpression Case expressionlist1 statementblock1 Case expressionlist2 statementblock2 … Case expressionlistN statementblockN Case Else statementblock End Select

9 9 The Select Case Statement –Run Time: The Effect of the Select Case Statement First the computer evaluates the test expression. Then it tries to match the resulting value. The search starts at the top expression list. The computer stops at the first match. The corresponding statement block is executed. Control resumes after the End Select. If no match occurs and a Case Else exists, then the Case Else block is executed. Then control resumes after the End Select. If no match occurs and no Case Else exists, then control resumes after the End Select. –Ranges Two variations –Use the keyword “To” »Ex. 25 To 37 –Use the keyword “Is” »Ex. Is >= 77

10 10 If versus Select Case If a decision has only two outcomes and the condition can be expressed as a single logical expression, then the If statement is easier to read. If a decision has multiple outcomes that depend on a single expression, then the Select Case is usually easier to read. If the outcomes depend on a number of conditions that may be independent, then embedded If statements are better.


Download ppt "1 Pertemuan 05 Selection Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:"

Similar presentations


Ads by Google