Boolean Expressions: true or false

The goal of this lesson is to learn to use Boolean expressions to make comparisons.
You will learn to use the if/else control structure to create different paths or branches in the code.

Sometimes a programmer would like one statement, or group of statements to execute only if certain conditions are true. There may be a different statement, or group of statements that are to be executed when the condition is false. An expression that can be evaluated to true or false is called a Boolean expression. (Named after a mathematician named Boole who developed Boolean algebra.)

In this module, you will use Boolean Expressions. Boolean expressions are ones that can be evaluated as true or false.
You will use if/else control structures to control which code to execute.
You will perform tasks such as asking the user a question and tell if the answer is right.
Your Guessing Game program will ask the user a series of questions. You will use Boolean expression to determine if the answer is right or wrong and tell the user his score at the end of the quiz.
In order to do this you will:
  • Write code to Input answers of various types from the user.
  • Apply if/else control structures to tell the user if his answer is right or wrong.
  • Implement a counter to display the score at the end.
  • Use the function getBooleanFromUser to get true or false answers.
  • Write code to compare values and take appropriate actions.
  • Be able to use AND and OR in Boolean expressions.
  • Use the function getHeight to find the taller of two animals.

Print the lesson

Please study the material at each of the links below.

  1. Ask if it is nighttime: The function getBooleanFromUser gives a choice of true or false.
  2. Pass or Fail: Compare a score to 65 and say "Pass" or "Fail"
  3. Child or Adult? Comparing values: Compare age to determine if child or adult
  4. Teenager? Using AND: &&: You are a teenager if age >=13 AND age <= 19
  5. Panda says "Whee!" if turning fast: Panda says "Whee" if amountToTurn > 2.0
  6. Who is taller? Finding larger of 2: Use the function getHeight to find the taller of two animals
  7. A Quiz Game: This video shows how to create a quiz and keep track of number right.
  8. A True/False Question: Using if to tell the user the answer is right.
  9. A drill on Boolean values: See if you can do these comparisons
  10. Self Study Questions
  11. Discussion Questions

Active Learning

Create a guessing game or quiz.

Like us on Facebook:


OER: Open Educational Resource
Creative Commons License Author: Janet E. Joy; Publisher: Zebra0.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Creative Commons Attribution-NonCommercial 4.0 International License
Last modified: May 17 2017 14:08:28.