Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Expressions and If statements

Similar presentations


Presentation on theme: "Boolean Expressions and If statements"— Presentation transcript:

1 Boolean Expressions and If statements

2 TRUE or FALSE All boolean expressions evaluate to the value of either:
Examples : see next slide

3 Examples Examples: 3>2 (evaluates to TRUE) 2>3 (evaluates to FALSE)

4 Operators and Operands
In the expression 3 > 2 The “>” sign is called an “operator” 3 and 2 are called “operands” The value is TRUE In the expression The “+” sign is called an “operator” The value is 5

5 Numerical Operators vs Relational Operators
The following are numerical operators * / The following are relational operators > < = <= >= <>

6 Relational Operators Symbol > < = >= <= <>`
Meaning greater than Less than Equal to Greater than OR equal to Less than OR equal to Is not equal to Examples 2>3 is false, 3>2 is true 2<3 is true, 3<2 is false 2=2 is true, 2=3 is false 2>=2 is true, 2>=3 is false 3>=2 is false 2<=2 is true, 2<=3 is true 3<=2 is false 2<>2 is false, 2<>3 is true

7 IF statement

8 How can we use this? Dim age As Integer
Given the following application, the message “You may vote” will appear only if the age is 18 or more. NOTHING will be displayed if the age is less than 18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 18 Then MessageBox.Show("You may vote.") End If End Sub see NEXT SLIDE for results

9 VS Example Continued Either something happens or it doesn’t.
NOTHING HAPPENS

10 Nothing happens if expression is FALSE
The program on the previous slide WILL NOT DISPLAY ANYTHING if the age is less than 18.

11 Else section

12 Alternate actions with ELSE
Alternate actions may be specified with an optional “Else” section Either the If section will execute OR the Else section will execute BUT NOT BOTH Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 18 Then MessageBox.Show("You may vote.") Else MessageBox.Show("You may eat ice cream.") End If End Sub

13 Example Continued IF section executes Else section executes VS

14 Syntax

15 If without and Else If BOOLEAN EXPRESSION Then statement1 statement2 statement3 etc. End If

16 If with Else If BOOLEAN EXPRESSION Then statement1 statement2 statement3 etc. Else statement1 statement2 statement3 End If

17 Nested If Statements

18 Nested If Statements The statements in the IF and ELSE sections can be other If statements. Example: If BOOLEAN EXPRESSION Then statement1 statement2 etc. If ANOTHER BOOLEAN EXPRESSION Then statement3 statement4 etc. End If statement5 statement6 etc. End If

19 Nesting can be in If, Else or both
The nesting can be done in the If or Else sections (or both) Example: If BOOLEAN EXPRESSION Then statement1 statement2 etc. Else statement1 statement2 etc. If ANOTHER BOOLEAN EXPRESSION Then statement3 statement4 etc. End If statement5 statement6 etc. End If

20 You must have and End If for each Nested If
Example 2 If BOOLEAN EXPRESSION Then If BOOLEAN EXPRESSION Then If BOOLEAN EXPRESSION Then Statement1 … End IF End IF End IF EXAMPLE 1 If BOOLEAN EXPRESSION Then Statement1 … If BOOLEAN EXPRESSION Then Statement1 … If BOOLEAN EXPRESSION Then Statement1 … End IF Statement1 … End IF Statement1 … End IF

21 Several Unconnected Ifs

22 When NOT to use Else -4 (minus 4)
Multiple conditional sections that are independent of each other should NOT use else Example: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num As Integer num = Integer.Parse(TextBox1.Text) If num > 0 Then MessageBox.Show("positive") End If If num Mod 2 = 0 Then MessageBox.Show("even") End Sub -4 (minus 4) Does NOT display “positive” DOES display “even”.

23 ElseIf

24 ElseIf ElseIf sections can be used to specify multiple alternate courses of action ElseIf is ONE WORD (no space) The first boolean expression to evaluate to TRUE causes that section to be executed. All other sections are then skipped. Example on next slide …

25 ElseIf Example If age >= 65 Then
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim age As Integer age = Integer.Parse(TextBox1.Text) If age >= 65 Then MessageBox.Show("You may collect social " & _ "security, drink and vote.") ElseIf age >= 21 Then MessageBox.Show("You may drink and vote.") ElseIf age >= 18 Then MessageBox.Show("You may vote.") Else MessageBox.Show("You may eat ice cream.") End If End Sub

26 Backus Naur Form (BNF)

27 What is BNF “Meta-language”
Used to show the syntax of another language

28 Basic Symbols in BNF The meta-symbols of BNF are:
::= meaning "is defined as" | meaning "or" < > angle brackets used to surround names of constructs.

29 [ Optional item ] optional items are enclosed in brackets, [ and ],
example: <if_statement> ::= if <boolean_expression> then <statement_sequence> [ else <statement_sequence> ] end if

30 { repetitive item } repetitive items (zero or more times) are enclosed in curly braces, { and }, example: <if_statement> ::= if <boolean_expression> then <statement_sequence> { elseIf <boolean_expression> then <statement_sequence> } [ else <statement_sequence> ] end if ;

31 One character literals (e.g. “;”)
Literals of only one character are surrounded by quotes (") to distinguish them from meta-symbols (e.g. < and [ ) example: <comment> ::= “’”<any text><end of line>

32 Select Case

33 Syntax (BNF form) { Case <expressionlist>
Select [ Case ] <testexpression> { Case <expressionlist> [ <statements> ] } [ Case Else [ <statements> ] ] End Select

34 Boolean Operators: AND, OR, NOT


Download ppt "Boolean Expressions and If statements"

Similar presentations


Ads by Google