Logical Expressions
TheIF statements and the DO WHILE statements of the previous
chapters could only ask simple questions,
like COUNT < 10HUE >= 16Chapter Topics:
- Relational Expressions (review)
- Logical Expressions
ANDOperator in QBasic- Checking that a number is in range
- Syntax of Logical Expressions
- OR Operator in QBasic
- Comparison between
ANDandOR
QUESTION:
You have decided to bake some cookies (much cheaper than getting them at the Mall.) An interesting cookie recipe calls for 4 cups of flour and 1 cup of sugar. You look in your supplies and find 3 cups of flour and 2 cups of sugar.Can you bake cookies with the supplies that you have?
The question about whether you have enough supplies has two parts:
You need at least 4 cups of flour AND You need at least 2 cups of sugarSince you do not have enough flour, you don't have enough supplies.
QUESTION:
What if you had 9 cups of flour and 1 cup of sugar. Now could you follow the recipe?In order to bake cookies two things must be true:
- You must have at least 4 cups of flour.
- You must have at least 2 cups of sugar.
' Cookie Ingredients Checker ' PRINT "How much flour do you have" INPUT FLOUR PRINT "How much sugar do you have" INPUT SUGAR ' IF FLOUR >= 4 AND SUGAR >= 2 THEN PRINT "You have enough ingredients" ELSE PRINT "You do not have enough" END IF ' ENDThe
IF statement
is asking a question with two parts:
IF FLOUR >= 4 AND SUGAR >= 2 THEN ---------- ---------- flour part sugar partEach one of these parts is a relational expression (as in the previous chapters.) A relational expression looks at two numbers and gives you TRUE or FALSE.
QUESTION:
Say that you enter 9 for FLOUR and 1 for SUGAR. What answer (TRUE or FALSE) does each of the parts give you?FLOUR >= 4 ________________ SUGAR >= 2 ________________
Here is the program again:
' Cookie Ingredients Checker ' PRINT "How much flour do you have" INPUT FLOUR PRINT "How much sugar do you have" INPUT SUGAR ' IF FLOUR >= 4 AND SUGAR >= 2 THEN PRINT "You have enough ingredients" ELSE PRINT "You do not have enough" END IF ' END For you to have enough ingredients, both relational expressions must be TRUE. This is the role of the
AND between the two
relational expressions.
The AND requires that both
FLOUR >= 4 and
SUGAR >= 2 are true before the entire question is true. The entire question must be true in order for the true branch to execute.
The two part question is an example of a logical expression. A logical expression looks at the TRUE and FALSE answers of relational expressions. It will itself give you a TRUE or FALSE.
QUESTION:
Look at the program. How will the monitor screen look if the user types 6 forFLOUR and 4 for SUGAR?When execution gets to the
IF statement, it finds that
FLOUR >= 4 —— true, because 6 >= 4and
SUGAR >= 2 —— true, because 4 >= 2Since both sides are true, the two part question gives us TRUE.
AND is used in a logical expression to insist that there is a TRUE on both sides:
this side must be true AND this side must be true
If both sides are true, the entire AND expression is true.
If either side (or both) are false,
the entire AND expression is false.
AND is called a logical operator
because it combines two true/false values into
a single true/false value.
AND does is:
- true AND true = true -- This is the only true.
- false AND true = false
- true AND false = false
- false AND false = false
AND is used to check that every requirement is met.
QUESTION:
Look at the logical expression:FLOUR >= 4 AND SUGAR >= 2What will the expression give us if
FLOUR is 2
and SUGAR is 0?
No hay comentarios:
Publicar un comentario