Programming with Visual Basic NET Selection Structure IfElse
Programming with Visual Basic. NET Selection Structure If-Else Week 4 Tariq Ibn Aziz Compunet Corporation 1
Objectives • Write pseudocode for the selection structure • Create a flowchart to help you plan an application’s code • Write an If. . . Then. . . Else statement • Write code that uses comparison operators and logical operators Compunet Corporation 2
The Selection Structure • Condition: expression evaluating to true or false • Selection (decision) structure – Chooses one of two paths based on a condition • Example – If employee works over 40 hours, add overtime pay • Four selection structures in Visual Basic – If, If/Else, and Case Compunet Corporation 3
Writing Pseudocode for If and If/Else Selection Structures • If selection structure – Contains only one set of instructions – Instructions are processed if the condition is true • If/Else selection – Contains two sets of instructions – True path: instruction set following true condition – False path: instruction set following false condition Compunet Corporation 4
Writing Pseudocode for If and If/Else Selection Structures Figure 4 -4: Examples of the If and If/Else selection structures written in pseudocode Compunet Corporation 5
Flowcharting the If and If/Else Selection Structures • Flowchart – Set of standardized symbols showing program flow • • Oval: the start/stop symbol Rectangle: the process symbol Parallelogram: the input/output symbol Diamond: selection/repetition symbol Compunet Corporation 6
Flowcharting the If and If/Else Selection Structures (continued) Figure 4 -5: Examples of the If and If/Else selection structures drawn in flowchart form Compunet Corporation 7
Structure chart Find roots of quadratic equation Read in coefficients Solve using quadratic formula Compunet Corporation Print results 8
Pseudocode • • Prompt user for inputs Read in a, b, c Calculate roots using quadratic eq. Print results Compunet Corporation 9
Flowchart Start Prompt user Read a, b, c x 1 = [-b + sqrt(b 2 -4 ac)]/2 a x 2 = [-b – sqrt(b 2 -4 ac)]/2 a Print x 1, x 2 Stop Compunet Corporation 10
Coding the If and If/Else Selection Structures • Syntax – If condition Then statement block for true path [Else statement block for false path] End If • condition must be a Boolean expression • The Else clause is optional Compunet Corporation 11
Comparison Operators • Comparison (relational) operators: – Test two items for equality or types of non-equality • Rules for comparison operators – Cause an expression to evaluate to true or false – Have lower precedence than arithmetic operators – Are evaluated from left to right • Example: 5 -2 > 1 + 2 3 > 3 False Compunet Corporation 12
Comparison Operators (continued) Figure 4 -7: Listing and examples of commonly used comparison operators (continued) Compunet Corporation 13
Comparison Operators (continued) Figure 4 -7: Listing and examples of commonly used comparison operators Compunet Corporation 14
IF Statement • Visual Basic allows you to test conditions and perform different operations depending on the results of that test If ( condition ) Then statement Compunet Corporation 15
IF Statement • Executing Statements if a Condition Is True Imports System. Date. Time Public Module Single. IF Sub Main() Dim My. Date As Date = #2/13/1973# If My. Date < Now Then My. Date = Now System. Console. Write. Line (My. Date) End Sub End Module Compunet Corporation 16
If-Else Statement • You can use If. . . Then. . . Else with the Else statement to define two blocks of executable statements. One block is executed if the condition is True, the other if it is False. If (expression) Then statement 1 Else statement 2 End If • if expression is true statement 1 will be executed otherwise statement 2. Compunet Corporation 17
If-Else Statement Example Public Module If. Else Sub Main() Dim d As Decimal =10. 28 If (d >= 0) Then System. Console. Write. Line(d) Else System. Console. Write. Line(-d) End If End Sub End Module Compunet Corporation 18
If-Else. If Statement • Visual Basic tests the conditions in the order they appear in the If. . . Then. . . Else statements tests until it encounters a True condition or an Else statement, at which point it executes the corresponding statement block. Execution then branches to the end of the If. . . Then. . . Else block Compunet Corporation 19
If-Else. If Statement Example Public Module Nested. IF Sub Main() DIM Performance As Integer = 2 DIM Salary As Integer = 2 If Performance = 1 Then System. Console. Write. Line (Salary Else. If Performance = 2 Then System. Console. Write. Line (Salary Else. If Performance = 3 Then System. Console. Write. Line (Salary Else System. Console. Write. Line (Salary End If End Sub End Module Compunet Corporation + Salary * 0. 1) + Salary * 0. 09) + Salary * 0. 07) + Salary * 0) 20
If-Else. If Statement Example Public Module If. Else. Ladder Sub Main() DIM I As Integer = 1 If I < 0 Then System. Console. Write. Line Else. If I = 1 Then System. Console. Write. Line Else. If I = 2 Then System. Console. Write. Line Else. If I = 3 Then System. Console. Write. Line Else System. Console. Write. Line End If End Sub Compunet Corporation End Module ("-ve num") ("One") ("Two") ("Three") ("> 3") 21
IIf Function • This example uses the IIf function to evaluate the Test. Me parameter of the Check. It procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small". Imports Microsoft. Visual. Basic Public Module IIf. Function Sub Main() Dim Check. It As String Check. It = IIf (Test. Me > 1000, "Large", "Small") System. Console. Write. Line ( Check. It ) End Sub End Module Compunet Corporation 22
Select Case • This example uses the Select Case statements to write a line corresponding to the value of the variable Number. The second Case statement contains the value that matches the current value of Number, so the statement that writes "Between 6 and 8" is executed. Compunet Corporation 23
Select Case Imports System. Console Public Module Select. Case Sub Main() Dim Number As Integer = 8 Select Number ' Evaluate Number. Case 1 To 5 ' Number between 1 and 5, inclusive. Write. Line ("Between 1 and 5") ' The following is the only Case clause that’s True. Case 6, 7, 8 ' Number between 6 and 8. Write. Line ("Between 6 and 8") Case 9 To 10 ' Number is 9 or 10. Write. Line ("Greater than 8") Case Else ' Other values. Write. Line ("Not between 1 and 10") End Select End Sub End Module Compunet Corporation 24
Relational & Boolean operator • Operator AND OR XOR NOT Action AND(always evaluate both sides) OR (always evaluate both sides) Exclusive OR NOT Compunet Corporation 25
Relational & Boolean operator P Q P AND Q P OR Q P XOR Q NOT P False False True False True True False Compunet Corporation 26
Relational & Boolean operator • This example uses the And operator to perform a logical conjunction on two expressions. The result is a Boolean value Public Module RB Sub Main() Dim A As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim my. Check As Boolean my. Check = A > B And B > C ' Returns True. System. Console. Write. Line (my. Check) my. Check = B > A And B > C ' Returns False. System. Console. Write. Line (my. Check) End Sub Compunet Corporation End Module 27
Relational Operator Imports System. Console Public Module Relational. Opr Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) Write. Line ( j )' 6 Write ( "k = " ) Write. Line ( k ) ' 7 Write. Line ( "Relational Operator") Write ( "j < k " ) Write. Line ( j < k )' True Write ( "j <= k " ) Write. Line ( j <= k )'True Write ( "j = k " ) Write. Line ( j = k )'False Write ( "j > k " ) Write. Line ( j > k )'False Write ( "j >= k " ) Write. Line ( j >= k )'False Write ( "j <> k " ) Write. Line ( j <> k )'True End Sub End Module Compunet Corporation 28
Boolean Operator Imports System. Console Public Module Logical. Op Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) Write. Line ( j )' 6 Write ( "k = " ) Write. Line ( k ) ' 7 Write. Line ( "Relational Operator") Write ( "j AND k " ) '110 & 111 Write. Line ( j AND k ) Write ( "j OR k " ) ' 110 or 111 Write. Line ( j OR k ) Write ( "j XOR k " ) ' 110 xor 111 Write. Line ( j XOR k ) Write ( " NOT k " ) Write. Line ( NOT k )'1000 End Sub End Module Compunet Corporation 29
- Slides: 29