knowt ap exam guide logo

Boolean Expressions and If Statements

If Statements

  • An if statement is just like it sounds. It’s a conditional statement that is used in Java to help control the flow of the program.

  • For example, your parents tell you, you can only watch a movie if you finish cleaning your room.

  • Cleaning your room is the condition for you to be able to watch the movie.

  • You won't get to go if you don’t clean your room.

  • The else statement in the example below is used for what results to produce when the if condition isn’t being satisfied.

    • Ex:

int num1 = 4, num2 = 5;

if( num1 == num2)

System.out.print(“The numbers are the same.”);

else

System.out.print(“The numbers aren’t the same”);
  • Note: The boolean operator( ==), isn’t the same as the assignment operator(=).

  • A boolean operator asks a question, while an assignment operator executes a command. The boolean operator in this example determines if num1 is equal to num2.

  • The assignment statement doesn’t produce other results.

    Boolean Expressions

  • Values that can be compared using boolean statements:

    • ==(equal)

    • !=(not equal)

    • <(less than)

    • <=(less than or equal to)

    • >(greater than)

    • >=(greater than or equal to)

  • The boolean statement produces a truth value based on the comparison that it conducts.

  • Sometimes, however, we might want to create a more complicated condition. This is referred to as a compound condition. They include at least one boolean operator.

  • Common Compound Conditions:

    • &&- logical and

    • ||- logical or

    • !- logical not

    • == - is equal to

    • != - is not equal to

  • Ex: Consider the following boolean expression, (X && !Y) || (!X && Y). Which of the following conditions will always cause the expression to evaluate to true.

    • Answer: || indicate or for both of the expressions. Note that for or, only 1 of the expressions has to be true. Both X and Y have to be true in their conditions for the expression within the brackets to be true. This rules out just the value of just one variable changing because both are impacted. The values of X and Y can’t match other. This means that X and Y have to have the opposite truth values.

  • Ex: What will be the truth value of (! p && ! q) || (p || q)?

    • Answer: Let’s start off with an expression on the right It will simplify to !p && !q. It doesn’t always evaluate to true or false, because we don’t know the values of p and q, and it’s contingent on this. The correct answer is that the expression evaluates to false whenever p and q have opposite values.

  • The way that if statements work and function applies to all control statements! Make sure you really understand how it works!

  • DeMorgan’s Laws are used to help simplify Boolean expressions.

  • Truth Table for combinations of Boolean Conditions:

A

B

A&&B

A||B

!A

!B

!(A&&B)

!A||!B

T

T

T

T

F

F

F

F

T

F

F

T

F

T

T

T

F

T

F

T

T

F

T

T

F

F

F

F

T

T

T

T

ES

Boolean Expressions and If Statements

If Statements

  • An if statement is just like it sounds. It’s a conditional statement that is used in Java to help control the flow of the program.

  • For example, your parents tell you, you can only watch a movie if you finish cleaning your room.

  • Cleaning your room is the condition for you to be able to watch the movie.

  • You won't get to go if you don’t clean your room.

  • The else statement in the example below is used for what results to produce when the if condition isn’t being satisfied.

    • Ex:

int num1 = 4, num2 = 5;

if( num1 == num2)

System.out.print(“The numbers are the same.”);

else

System.out.print(“The numbers aren’t the same”);
  • Note: The boolean operator( ==), isn’t the same as the assignment operator(=).

  • A boolean operator asks a question, while an assignment operator executes a command. The boolean operator in this example determines if num1 is equal to num2.

  • The assignment statement doesn’t produce other results.

    Boolean Expressions

  • Values that can be compared using boolean statements:

    • ==(equal)

    • !=(not equal)

    • <(less than)

    • <=(less than or equal to)

    • >(greater than)

    • >=(greater than or equal to)

  • The boolean statement produces a truth value based on the comparison that it conducts.

  • Sometimes, however, we might want to create a more complicated condition. This is referred to as a compound condition. They include at least one boolean operator.

  • Common Compound Conditions:

    • &&- logical and

    • ||- logical or

    • !- logical not

    • == - is equal to

    • != - is not equal to

  • Ex: Consider the following boolean expression, (X && !Y) || (!X && Y). Which of the following conditions will always cause the expression to evaluate to true.

    • Answer: || indicate or for both of the expressions. Note that for or, only 1 of the expressions has to be true. Both X and Y have to be true in their conditions for the expression within the brackets to be true. This rules out just the value of just one variable changing because both are impacted. The values of X and Y can’t match other. This means that X and Y have to have the opposite truth values.

  • Ex: What will be the truth value of (! p && ! q) || (p || q)?

    • Answer: Let’s start off with an expression on the right It will simplify to !p && !q. It doesn’t always evaluate to true or false, because we don’t know the values of p and q, and it’s contingent on this. The correct answer is that the expression evaluates to false whenever p and q have opposite values.

  • The way that if statements work and function applies to all control statements! Make sure you really understand how it works!

  • DeMorgan’s Laws are used to help simplify Boolean expressions.

  • Truth Table for combinations of Boolean Conditions:

A

B

A&&B

A||B

!A

!B

!(A&&B)

!A||!B

T

T

T

T

F

F

F

F

T

F

F

T

F

T

T

T

F

T

F

T

T

F

T

T

F

F

F

F

T

T

T

T