Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.

Similar presentations


Presentation on theme: "Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz."— Presentation transcript:

1 Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz

2 Compunet Corporation2 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

3 Compunet Corporation3 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, If/ElseIf/Else, and Case

4 Compunet Corporation4 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

5 Compunet Corporation5 Writing Pseudocode for If and If/Else Selection Structures Figure 4-4: Examples of the If and If/Else selection structures written in pseudocode

6 Compunet Corporation6 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

7 Compunet Corporation7 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

8 Compunet Corporation8 Structure chart Find roots of quadratic equation Read in coefficients Solve using quadratic formula Print results

9 Compunet Corporation9 Pseudocode Prompt user for inputs Read in a, b, c Calculate roots using quadratic eq. Print results

10 Compunet Corporation10 Flowchart Start Prompt user Read a,b,c x1 = [-b + sqrt(b 2 -4ac)]/2a x2 = [-b – sqrt(b 2 -4ac)]/2a Print x1, x2 Stop

11 Compunet Corporation11 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

12 Compunet Corporation12 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

13 Compunet Corporation13 Comparison Operators (continued) Figure 4-7: Listing and examples of commonly used comparison operators (continued)

14 Compunet Corporation14 Comparison Operators (continued) Figure 4-7: Listing and examples of commonly used comparison operators

15 Compunet Corporation15 IF Statement Visual Basic allows you to test conditions and perform different operations depending on the results of that test If ( condition ) Then statement

16 Compunet Corporation16 IF Statement Executing Statements if a Condition Is True Imports System.DateTime Public Module SingleIF Sub Main() Dim MyDate As Date = #2/13/1973# If MyDate < Now Then MyDate = Now System.Console.WriteLine (MyDate) End Sub End Module

17 Compunet Corporation17 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 statement1 Else statement2 End If if expression is true statement1 will be executed otherwise statement2.

18 Compunet Corporation18 If-Else Statement Example Public Module IfElse Sub Main() Dim d As Decimal =10.28 If (d >= 0) Then System.Console.WriteLine(d) Else System.Console.WriteLine(-d) End If End Sub End Module

19 Compunet Corporation19 If-ElseIf 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

20 Compunet Corporation20 If-ElseIf Statement Example Public Module NestedIF Sub Main() DIM Performance As Integer = 2 DIM Salary As Integer = 2 If Performance = 1 Then System.Console.WriteLine (Salary + Salary * 0.1) ElseIf Performance = 2 Then System.Console.WriteLine (Salary + Salary * 0.09) ElseIf Performance = 3 Then System.Console.WriteLine (Salary + Salary * 0.07) Else System.Console.WriteLine (Salary + Salary * 0) End If End Sub End Module

21 Compunet Corporation21 If-ElseIf Statement Example Public Module IfElseLadder Sub Main() DIM I As Integer = 1 If I < 0 Then System.Console.WriteLine ("-ve num") ElseIf I = 1 Then System.Console.WriteLine ("One") ElseIf I = 2 Then System.Console.WriteLine ("Two") ElseIf I = 3 Then System.Console.WriteLine ("Three") Else System.Console.WriteLine ("> 3") End If End Sub End Module

22 Compunet Corporation22 IIf Function This example uses the IIf function to evaluate the TestMe parameter of the CheckIt procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small". Imports Microsoft.VisualBasic Public Module IIfFunction Sub Main() Dim CheckIt As String CheckIt = IIf (TestMe > 1000, "Large", "Small") System.Console.WriteLine ( CheckIt ) End Sub End Module

23 Compunet Corporation23 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.

24 Compunet Corporation24 Select Case Imports System.Console Public Module SelectCase Sub Main() Dim Number As Integer = 8 Select Number ' Evaluate Number. Case 1 To 5 ' Number between 1 and 5, inclusive. WriteLine ("Between 1 and 5") ' The following is the only Case clause that’s True. Case 6, 7, 8 ' Number between 6 and 8. WriteLine ("Between 6 and 8") Case 9 To 10 ' Number is 9 or 10. WriteLine ("Greater than 8") Case Else ' Other values. WriteLine ("Not between 1 and 10") End Select End Sub End Module

25 Compunet Corporation25 Relational & Boolean operator OperatorAction AND AND(always evaluate both sides) OR OR (always evaluate both sides) XOR Exclusive ORNOT

26 Compunet Corporation26 Relational & Boolean operator PQP AND QP OR QP XOR QNOT P False True FalseTrueFalseTrue False True False True False

27 Compunet Corporation27 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 myCheck As Boolean myCheck = A > B And B > C ' Returns True. System.Console.WriteLine (myCheck) myCheck = B > A And B > C ' Returns False. System.Console.WriteLine (myCheck) End Sub End Module

28 Compunet Corporation28 Relational Operator Imports System.Console Public Module RelationalOpr Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) WriteLine ( j ) ' 6 Write ( "k = " ) WriteLine ( k ) ' 7 WriteLine ( "Relational Operator") Write ( "j < k " ) WriteLine ( j < k ) ' True Write ( "j <= k " ) WriteLine ( j <= k )'True Write ( "j = k " ) WriteLine ( j = k )'False Write ( "j > k " ) WriteLine ( j > k )'False Write ( "j >= k " ) WriteLine ( j >= k )'False Write ( "j <> k " ) WriteLine ( j <> k ) 'True End Sub End Module

29 Compunet Corporation29 Boolean Operator Imports System.Console Public Module LogicalOp Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) WriteLine ( j )' 6 Write ( "k = " ) WriteLine ( k )' 7 WriteLine ( "Relational Operator") Write ( "j AND k " ) '110 & 111 WriteLine ( j AND k ) Write ( "j OR k " ) ' 110 or 111 WriteLine ( j OR k ) Write ( "j XOR k " ) ' 110 xor 111 WriteLine ( j XOR k ) Write ( " NOT k " ) WriteLine ( NOT k ) '1000 End Sub End Module


Download ppt "Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz."

Similar presentations


Ads by Google