1 of 12

Control Structures and Conditionals :3

Ft. Catherine, Nicole, Julia and Maria

2 of 12

Agenda for Today

  • Attendance
  • The If Statement
  • Inequalities
  • Boolean Operators
  • Else
  • If Structures
  • Applications
  • Challenge Problem

3 of 12

The If Statement

  • Basis of all decision making in programming
    • It’s like asking yes or no questions
  • Controls the flow of the execution of the program
  • Contains a block of code, only executed if the condition of the if statement is true

Ex:

The print statement will not execute since the condition in the if statement is false.

For general use:

4 of 12

Inequalities

== (equality)

  • Determines if two values are equivalent

!= (inequality)

  • Determines if two values are not equivalent

> (greater than)

  • Determines if the value on the left is greater than the value on the right

< (less than)

  • Determines if the value on the left is less than the value on the right

>= (greater than or equal to)

  • Determines if the value on the left is greater than or equal to the value on the right

<= (less than or equal to

  • Determines if the value on the left is less than or equal to the value on the right

5 of 12

Note About Inequalities

  • The inequality symbols on the previous slide only work with primitive data types:
    • int, char, float, boolean, etc. (only == and != work on boolean)
  • This does not include Strings
  • == will only compare if the Strings are stored in the same memory location
  • To ensure that the Strings are compared properly, use the equals() method

Instead of:

Do:

Instead of:

Do:

6 of 12

Boolean Operators

  • Boolean operators (logical operators) are used to combine simple conditions into more complex expressions

&& (AND)

  • True if both expressions are true
  • False if either one is false

|| (OR)

  • True if either expression is true
  • False if both of them are false

! (NOT)

  • Inverts/negates the Boolean value of an expression

*You can find the key for OR above the enter key

7 of 12

Operator Precedence and De Morgan’s Laws

Order of precedence:

  1. ! (NOT)
  2. && (AND)
  3. || (OR)
  4. You can use parentheses to indicate precedence
  5. It’s like BEDMAS for boolean operators

De Morgan’s Laws

They are:

  1. !(A || B) = !A && !B
  2. !(A && B) = !A || !B
  3. A bit trippy at first but if you think really hard about it, they makes sense

8 of 12

Boolean Operators Examples

If a = 5, b = 2, and c = 4, are the following true or false?

!(a != 4) && b > 0 || c >= 4 && a > b

!(a != 4) && (b > 0 || c >= 4 && a > b)

!(a != 4) && b > 0 || !(c >= b) && a > b

(!(a != 4) || b > 0 || !(c >= b)) && a > b

True

True

False

False

9 of 12

Else

  • Can be used to extend an if structure
  • If the condition is false, the statements after the keyword else will be executed
  • Can be used as:
    • Else if - to check another condition if the previous one was false
    • Else - if all conditions are false, the program defaults to this

General Example:

10 of 12

If Structures

  • We can chain together a bunch of if-then-else statements to create a structure

Ex:

Output:

a is greater than 3

  • Even though a can satisfy more than one expression, once a condition is met, it skips the rest of the if structure

This part is skipped because the previous condition was satisfied

11 of 12

Applications

Animation

  • check if an object has reached a boundary

12 of 12

Challenge Problem

Write a program that lists the numbers from 1 to 100, but:

  • For multiples of 3, write “Fizz”
  • For multiples of 5, write “Buzz”
  • For multiples of both, write “FizzBuzz”