Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Structures Relational and Logical Operators; If Statements.

Similar presentations


Presentation on theme: "Selection Structures Relational and Logical Operators; If Statements."— Presentation transcript:

1 Selection Structures Relational and Logical Operators; If Statements

2 An If statement allows a program to decide on a course of action based on whether a condition is true or false If condition Then action1 Else action2 End If

3 Relational Operators

4 Relational Operator Examples 1 <= 1 1 < 1 “car” < “cat” “Dog” < “dog”

5 Logical Operators And –condition1 And condition2 is true if both condition1 and condition2 are true otherwise it evaluates to false Or –condition1 Or condition2 is true if either condition1 or condition2 (or both) are true otherwise it evaluates to false Not –Not condition1 is true if condition1 is false, and is false if condition1 is true

6 Logical Operator Examples Assume n = 4 and answ = “Y” –(2 < n) And (n < 6) –(2 < n) Or (n = 6) –Not (n < 6) –(answ = “Y”) And (answ = “y”) –(answ = “Y”) Or (answ = “y”) –Not (answ = “y”)

7 If Statements An If statement allows a program to decide on a course of action based on whether a condition is true or false If condition Then action1 Else action2 End If

8 If Statement Example Private Sub cmdComputeTip_Click() Dim cost As Single, tip As Single cost = Val(InputBox("Enter cost of meal:")) tip = cost * 0.15 If tip < 1 Then tip = 1 End If picTip.Print "Leave "; FormatCurrency(tip); " for the tip." End Sub

9 If Statement Example Private Sub cmdQuestion_Click() Dim name As String 'Ask for first Ronald McDonald name = UCase(InputBox("Who was the first Ronald McDonald?")) picAnswer.Cls If name = "WILLARD SCOTT" Then picAnswer.Print "Correct." Else picAnswer.Print "Nice try." End If End Sub

10 Nested If Example Private Sub cmdComputeBalance_Click() Dim balance As Single, amount As Single balance = Val(InputBox("Current balance:")) amount = Val(InputBox("Amount of withdrawal:")) If (balance >= amount) Then balance = balance - amount picBalance.Print "New balance is "; FormatCurrency(balance) If balance < 150 Then picBalance.Print "Balance below $150" End If Else picBalance.Print "Withdrawal denied." End If End Sub

11 Private Sub cmdFindLarger_Click() Dim Num1 As Integer Dim Num2 As Integer Num1 = Val(txtFirstNumber.Text) Num2 = Val(TxtSecondNumber.Text) If Num1 > Num2 Then picResult.Print "The larger number is"; Num1 ElseIf Num2 > Num1 Then picResult.Print "The larger number is"; Num2 Else picResult.Print "The two numbers are equal" End If End Sub ElseIf Example


Download ppt "Selection Structures Relational and Logical Operators; If Statements."

Similar presentations


Ads by Google