80 likes | 198 Vues
This guide delves into selection structures in Visual Basic, focusing on If-Else and Select Case statements. Selection structures are essential for controlling the flow of a program based on specific conditions. The If statement evaluates conditions and executes corresponding actions, while Select Case provides a more concise way to handle multiple conditions. We examine practical programming examples that award honors based on GPA thresholds. Learn how to implement these structures effectively in your Visual Basic applications!
E N D
Definición • La estructura de selección la utilizamos cuando necesitamos programar el computador para que, dependiendo del cumplimiento o no de ciertas condiciones dentro del programa, se ejecuten o no instrucciones.
If Blocks • If condition Then action1Else action2End If
Flowchart general N Is the condition true? T Execute action2 Execute action1
ElseIf clauses If condition1 Then action1ElseIf condition2 Then action2ElseIf condition3 Then action3Else action4 End If
EjemploprogramaciónElseIf Public Class Form1 Private Sub Button1_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click DimgpaAs Double Dim honors As String gpa = CDbl(TextBox1.Text) If gpa >= 3.9 Then honors = "summa cum laude..." ElseIfgpa >= 3.6 Then honors = "magna cum laude..." ElseIfgpa >= 3.3 Then honors = "cum laude..." ElseIfgpa >= 2.0 Then honors = "........." Else honors = "You don't graduated..." End If TextBox2.Text = gpa & " " & honors End Sub End Class
Select Case Block Select Case selectorCasevalueList 1 action1CasevalueList 2 action2CaseElse action of last resortEnd Select
EjemploprogramaciónSelect Case Public Class Form1 Private Sub Button1_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click DimgpaAs Double Dim honors As String gpa = CDbl(TextBox1.Text) Select Case gpa Case Is >= 3.9 honors = "summa cum laude..." Case Is >=3.6 honors = "magna cum laude..." Case Is >= 3.3 honors = "cum laude..." Case Is >=2.0 honors = "........." Case Else honors = "You don't graduated..." End Select TextBox2.Text = gpa & " " & honors End Sub End Class