Conditionals and Boolean Expressions - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Conditionals and Boolean Expressions

Description:

If the if clause didn't execute, the else clause will! Has format: if ( boolean value ... Accept anyone not named Bob' or named Fred' ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 30
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Conditionals and Boolean Expressions


1
Conditionals and Boolean Expressions
2
Overview
  • Flow Diagram
  • Boolean definition
  • Relational Operators
  • Testing equality of Strings
  • if statements
  • else statements
  • switch and Select statements

3
A Flow Diagram
Start
Blame it on someone else
Lay low and keep hidden
Did you break anything?
Yes
Yes
No
Have your parents asked about it?
Yes
Are you lying?
No
Are you passing your classes?
No
No
Ask for
Yes
End
4
The Boolean
  • A boolean is a something that resolves to true or
    false
  • You can get booleans several different ways
  • Simple
  • Complex
  • These booleans will be used (later) to make
    decisions to change your programs behavior

5
Relational Operators(for primitive data types)
  • Relational Operators
  • gt greater than
  • lt less than
  • equals ( in VB)
  • ! Not (Not in VB)
  • ! not equal (ltgt in VB)
  • gt greater than or equal
  • lt less than or equal
  • Notice that all of these return us a true or
    false value!

6
Relational Operator Examples
  • Literal Example
  • 5 gt 3 // true
  • 6 ! 6 // false
  • true (4 lt 2) // false and redundant
  • c ! b // true
  • !false // true
  • Variable Example
  • int num1 5
  • short num2 7
  • boolean result num2 gt num1
  • //Note result is now true

7
String equality in Java
  • String is not a primitive type, so doesnt
    work (most of the time)
  • Must use the .equals( ) function
  • For example, if you had
  • String myString Java
  • then
  • myString.equals (Coffee)
  • returns false and
  • myString.equals (Java)
  • returns true

8
and Operators
  • These operators check for multiple conditions
  • (AND in VB) needs both the left and the right
    to be true in order to return true
  • AndAlso in VB shortcut
  • (OR in VB) needs either one (or both) to be
    true to return true
  • Examples
  • ( (6 gt 5) ( c b) ) // false
  • ( (6 gt 5) ( 7 lt 9) ) // true
  • ( (6 gt 5) ( c b) ) // true
  • ( (6 gt 6) ( c b) ) // false

9
Now Lets Do Something!
  • Using the if statement, we can decide whether or
    not to execute some code!
  • Has format
  • if (ltboolean valuegt)
  • // all the code thats in here will only execute
  • // if and only if the boolean above is true
  • // VB
  • If ltboolean valuegt Then
  • End If

10
if Example
  • int start 5
  • int end 19
  • if (start lt end )
  • System.out.println ( A )
  • // end if
  • System.out.println ( B )
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start lt end Then
  • Console.WriteLine (A)
  • End If
  • Console.WriteLine (B)

11
if Example
  • int start 5
  • int end 19
  • if (start lt end )
  • System.out.println ( A )
  • // end if
  • System.out.println ( B )
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start lt end Then
  • Console.WriteLine (A)
  • End If
  • Console.WriteLine (B)

Output
A
B
12
if Example(Part II)
  • int start 5
  • int end 19
  • if (start gt end )
  • System.out.println ( A )
  • // end if
  • System.out.println ( B )
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start gt end Then
  • Console.WriteLine (A)
  • End If
  • Console.WriteLine (B)

13
if Example(Part II)
  • int start 5
  • int end 19
  • if (start gt end )
  • System.out.println ( A )
  • // end if
  • System.out.println ( B )
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start gt end Then
  • Console.WriteLine (A)
  • End If
  • Console.WriteLine (B)

Output
B
14
The else statement
  • if has a counterpart the else statement
  • If the if clause didnt execute, the else clause
    will!
  • Has format
  • if (ltboolean valuegt)
  • // statements that execute if the boolean is
    true
  • else
  • // statements that execute if the boolean is
    false
  • If ltboolean valuegt Then
  • // statements that execute if the boolean is
    true
  • Else
  • // statements that execute if the boolean is
    false
  • End If
  • Notice only one set of statements executes, no
    matter what!

15
else Example
  • int start 5
  • int end 19
  • if (start gt end )
  • System.out.println ( A )
  • // end if
  • else
  • System.out.println ( B )
  • // end else
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start gt end Then
  • Console.Writeln ( A )
  • Else
  • Console.Writeln ( B )
  • End If

16
else Example
  • int start 5
  • int end 19
  • if (start gt end )
  • System.out.println ( A )
  • // end if
  • else
  • System.out.println ( B )
  • // end else
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start gt end Then
  • Console.Writeln ( A )
  • Else
  • Console.Writeln ( B )
  • End If

Output
B
17
else Example
  • int start 5
  • int end 19
  • if (start lt end )
  • System.out.println ( A )
  • // end if
  • else
  • System.out.println ( B )
  • // end else
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start lt end Then
  • Console.Writeln ( A )
  • Else
  • Console.Writeln ( B )
  • End If

18
else Example
  • int start 5
  • int end 19
  • if (start lt end )
  • System.out.println ( A )
  • // end if
  • else
  • System.out.println ( B )
  • // end else
  • Dim start As Integer 5
  • Dim end As Integer 19
  • If start lt end Then
  • Console.Writeln ( A )
  • Else
  • Console.Writeln ( B )
  • End If

Output
A
19
else ifs
  • Selecting one from many
  • As soon as one is true, the rest are not
    considered
  • Has format
  • if (ltboolean valuegt)
  • else if (ltboolean valuegt)
  • else if (ltboolean valuegt)
  • else

If ltboolean valuegt Then stuff ElseIf ltboolean
valuegt Then stuff ElseIf ltboolean valuegt
Then stuff Else stuffEnd If
20
Combining into else ifs(Selecting one from
many)
  • int start 5 int middle 8 int end 19
  • if (start lt middle )
  • System.out.println ( A )
  • // end if
  • else if (start lt end)
  • System.out.println ( B )
  • // end else if
  • Dim start As Integer 5
  • Dim middle As Integer 8
  • Dim end As Integer 19
  • If start lt middle Then
  • Console.WriteLine ( A )
  • ElseIf start lt end
  • Console.WriteLine ( B )
  • End If

21
Combining into else ifs(Selecting one from
many)
  • int start 5 int middle 8 int end 19
  • if (start lt middle )
  • System.out.println ( A )
  • // end if
  • else if (start lt end)
  • System.out.println ( B )
  • // end else if
  • Dim start As Integer 5
  • Dim middle As Integer 8
  • Dim end As Integer 19
  • If start lt middle Then
  • Console.WriteLine ( A )
  • ElseIf start lt end
  • Console.WriteLine ( B )
  • End If

Output
A
22
else catch-all
  • int start 5 int middle 8 int end 19
  • if (start gt middle )
  • System.out.println ( A )
  • // end if
  • else if (start gt end)
  • System.out.println ( B )
  • // end else if
  • else
  • System.out.println ( C )
  • Dim start As Integer 5
  • Dim middle As Integer 8
  • Dim end As Integer 19
  • If start gt middle Then
  • Console.WriteLine ( A )
  • ElseIf start gt end
  • Console.WriteLine ( B )
  • Else

23
else catch-all
  • int start 5 int middle 8 int end 19
  • if (start gt middle )
  • System.out.println ( A )
  • // end if
  • else if (start gt end)
  • System.out.println ( B )
  • // end else if
  • else
  • System.out.println ( C )
  • Dim start As Integer 5
  • Dim middle As Integer 8
  • Dim end As Integer 19
  • If start lt middle Then
  • Console.WriteLine ( A )
  • ElseIf start lt end
  • Console.WriteLine ( B )
  • Else

Output
C
24
Switch/Select statements
  • Simplify the else-if statements for primitive
    data types
  • Trying to find a match (or range in VB)
  • In Java, once a match is found, it will keep
    going until it sees the break keyword (telling it
    to stop)
  • Has format
  • switch (ltprimitive variablegt)
  • case ltvaluegt
  • case ltvaluegt

Select Case ltprimitive variablegt Case
ltvaluegt Case ltvaluegt To ltvaluegt End Select
25
Switch/Select Example
  • int myInt 1
  • switch (myInt)
  • case 1 System.out.println (A)
  • case 2 System.out.println (B)
  • case 3 System.out.println (C)
  • //end switch
  • Dim myInt As Integer 1
  • Select Case myInt
  • Case 1
  • Console.WriteLine (A)
  • Case 2
  • Console.WriteLine (B)
  • Case 3
  • Console.WriteLine (C)
  • End Select

26
Better Switch Example
  • int myInt 1
  • switch (myInt)
  • case 1 System.out.println (A)
  • break
  • case 2 System.out.println (B)
  • break
  • case 3 System.out.println (C)
  • break
  • //end switch
  • // In this case, only A prints out, because the
    breaks
  • // stop the flow

27
The default/else keyword
  • Its the catch-all
  • Format
  • // Java, C
  • switch (ltprimitive variablegt)
  • case ltvaluegt
  • case ltvaluegt
  • default
  • VB
  • Select Case ltprimitive variablegt
  • Case ltvaluegt
  • Case ltvaluegt
  • Case Else
  • End Select

28
Example
  • int myInt 5
  • switch (myInt)
  • case 1 System.out.println (A)
  • break
  • case 2 System.out.println (B)
  • break
  • default System.out.println (C)
  • //end switch
  • Dim myInt As Integer 5
  • Select Case myInt
  • Case 1
  • Console.WriteLine (A)
  • Case 2
  • Console.WriteLine (B)
  • Case Else
  • Console.WriteLine (C)
  • End Select

29
Discussion
  • Algorithm to
  • Accept all numbers greater than 50
  • Accept all numbers except the number 17
  • Accept anyone with the name Bob, except Bob
    Saggett
  • Accept anyone not named Bob or named Fred
Write a Comment
User Comments (0)
About PowerShow.com