Learn Alice in FREE step-by-step lessons.
Module 12: Boolean Expressions: true or false
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.
Goals:
- Use Boolean expressions to make comparisons;
- Use the if/else control structure to create different paths or branches in the code;
- Perform tasks such as asking the user a question and tell if the answer is right;
- Gain a deeper understanding of objects in the Alice virtual world.
Objectives:
- 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.
Please study the material at each of the links below.
Ask if it is nighttime: The function getBooleanFromUser gives a choice of true or false.
Using Boolean Expressions in If Block: Ask if Thor is happy, he says ha-ha or boo-hoo
Pass or Fail: Compare a score to 65 and say "Pass" or "Fail"
Child or Adult? Comparing values: Compare age to determine if child or adult
Restaurant? Using AND: &&: Need restaurant with hamburgers and pizza
Teenager? Using AND: &&: You are a teenager if age >=13 AND age <= 19- discount? Using OR: ||: Children and Seniors pay $10 admission. Everyone else pays $15
- Panda says "Whee!" if turning fast: Panda says "Whee" if amountToTurn > 2.0
Who is taller? Compare height of 2: Use the function getHeight to find the taller of two animals
A Quiz Game: This video shows how to create a quiz and keep track of number right.- A True/False Question: Using if to tell the user the answer is right.
A drill on Boolean values: See if you can do these comparisons
The user is offered coffee and tea: See if you can add another choice
The seagull asks how many penguins there are.: See if you can add to this project
Self Study Questions: What have you learned?
Active Learning: A Quiz Game: Show what have you learned- Complete Step 5: Interactions for the final project.
Discussion questions: What do you think?
Glossary for boolean lesson
Full Glossary