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

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

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

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

Compunet Corporation 1

Programming with Visual Basic .NET

Selection Structure If-Else

Week 4

Tariq Ibn Aziz

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

Compunet Corporation 2

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

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

Compunet Corporation 3

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

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

Compunet Corporation 4

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

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

Compunet Corporation 5

Writing Pseudocode for If and If/Else Selection Structures

Figure 4-4: Examples of the If and If/Else selection structures written in pseudocode

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

Compunet Corporation 6

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

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

Compunet Corporation 7

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

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

Compunet Corporation 8

Structure chartFind roots of

quadratic equation

Read incoefficients

Solve usingquadratic formula Print results

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

Compunet Corporation 9

Pseudocode

• Prompt user for inputs

• Read in a, b, c

• Calculate roots using quadratic eq.

• Print results

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

Compunet Corporation 10

FlowchartStart

Prompt user

Read a,b,c

x1 = [-b + sqrt(b2-4ac)]/2ax2 = [-b – sqrt(b2-4ac)]/2a

Print x1, x2

Stop

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

Compunet Corporation 11

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

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

Compunet Corporation 12

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

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

Compunet Corporation 13

Comparison Operators (continued)

Figure 4-7: Listing and examples of commonly used comparison operators (continued)

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

Compunet Corporation 14

Comparison Operators (continued)

Figure 4-7: Listing and examples of commonly used comparison operators

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

Compunet Corporation 15

IF Statement

• Visual Basic allows you to test conditions and perform different operations depending on the results of that test

If ( condition ) Then statement

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

Compunet Corporation 16

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

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

Compunet Corporation 17

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) Thenstatement1

Elsestatement2

End If

• if expression is true statement1 will be executed otherwise statement2.

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

Compunet Corporation 18

If-Else Statement Example

Public Module IfElseSub Main()

Dim d As Decimal =10.28If (d >= 0) Then

System.Console.WriteLine(d)Else

System.Console.WriteLine(-d)End If

End SubEnd Module

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

Compunet Corporation 19

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

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

Compunet Corporation 20

If-ElseIf Statement ExamplePublic Module NestedIFSub Main()

DIM Performance As Integer = 2DIM Salary As Integer = 2If 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 SubEnd Module

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

Compunet Corporation 21

If-ElseIf Statement ExamplePublic Module IfElseLadder Sub Main()DIM I As Integer = 1

If I < 0 ThenSystem.Console.WriteLine ("-ve num")

ElseIf I = 1 ThenSystem.Console.WriteLine ("One")

ElseIf I = 2 ThenSystem.Console.WriteLine ("Two")

ElseIf I = 3 ThenSystem.Console.WriteLine ("Three")

ElseSystem.Console.WriteLine ("> 3")

End IfEnd SubEnd Module

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

Compunet Corporation 22

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.VisualBasicPublic Module IIfFunctionSub Main()

Dim CheckIt As StringCheckIt = IIf (TestMe > 1000, "Large", "Small")System.Console.WriteLine ( CheckIt )

End SubEnd Module

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

Compunet Corporation 23

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.

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

Compunet Corporation 24

Select CaseImports System.ConsolePublic Module SelectCaseSub Main()

Dim Number As Integer = 8Select 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 SubEnd Module

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

Compunet Corporation 25

Relational & Boolean operator• Operator Action

AND AND(always evaluate both sides)

OR OR (always evaluate both sides)

XOR Exclusive OR

NOT NOT

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

Compunet Corporation 26

Relational & Boolean operator

P Q P AND Q P OR Q P XOR Q NOT P

False False False False False True

False True False True True True

True False False True True False

True True True True False False

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

Compunet Corporation 27

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 RBSub Main()

Dim A As Integer = 10Dim B As Integer = 8Dim C As Integer = 6Dim myCheck As BooleanmyCheck = A > B And B > C ' Returns True.System.Console.WriteLine (myCheck)myCheck = B > A And B > C ' Returns False.System.Console.WriteLine (myCheck)

End SubEnd Module

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

Compunet Corporation 28

Relational OperatorImports System.ConsolePublic Module RelationalOprSub Main()

Dim j AS Integer =6Dim k AS Integer =7Write ( "j = " )

WriteLine ( j )' 6Write ( "k = " )

WriteLine ( k ) ' 7WriteLine ( "Relational Operator") Write ( "j < k " )

WriteLine ( j < k )' True

Write ( "j <= k " )WriteLine ( j <= k )'TrueWrite ( "j = k " ) WriteLine ( j = k )'FalseWrite ( "j > k " )WriteLine ( j > k )'FalseWrite ( "j >= k " ) WriteLine ( j >= k )'FalseWrite ( "j <> k " )

WriteLine ( j <> k )'TrueEnd Sub

End Module

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

Compunet Corporation 29

Boolean OperatorImports System.ConsolePublic Module LogicalOpSub Main()

Dim j AS Integer =6Dim k AS Integer =7Write ( "j = " )WriteLine ( j )' 6Write ( "k = " )WriteLine ( k ) ' 7WriteLine ( "Relational Operator") Write ( "j AND k " )'110 & 111WriteLine ( j AND k )

Write ( "j OR k " )

' 110 or 111WriteLine ( j OR k )Write ( "j XOR k " )

' 110 xor 111WriteLine ( j XOR k ) Write ( " NOT k " )

WriteLine ( NOT k )'1000End Sub

End Module