EZ Math Movie, Tutorials, Decisions, And and Or


Compound Boolean statements with 'and'

Suppose you wanted to see if an (x, y) coordinate was in the first quadrant of an (x, y) graph. That would mean that both the x-coordinate and the y-coordinate would be positive, or greater than zero. Note the word 'and'.

To say 'and' in a Boolean statement, we type: &&.

So, an if statement for checking if an (x, y) point is in the first quadrant would look this way:

if((x > 0) && (y > 0))
{
  print('In first quadrant');
}

Boolean statements like this are said to be 'compound' Boolean statements.

True and true is true.

For a compound Boolean statement that uses && (and) both parts have to be true for the whole statement to be true. In other words, for the print command in our previous example to execute both x and y must be greater than zero, not just one of them. Both the first and second parts of the Boolean statement must be true for the whole statement to be true.

So:

Compound statements with 'or'

Sometimes we want to take action if one thing or another thing is true, but not necessarily only when both things true at the same time. Note the word 'or'. In a Boolean statement for 'or' we type: ||. Those are two vertical bars, one after the other. The vertical bar is a character on your keyboard, usually a shift-backslash.

So, suppose a certain game is won if you collect more than 7 jewels or more than 5 coins. That could be programed this way:

if((numberOfJewels > 7) || (numberOfCoins > 5))
{
  print('You won!');
}

True or false is true.

The previous compound Boolean statement uses || (or), and only one part must be true for the whole statement to be true. So, for the previous example to print 'You won!' either the number of jewels is greater than 7 or the number of coins is greater than 5. Only one, not both, need be true. Of course, if both happen to be true, then the whole statement is true. The only way for such a statement to be false is if both parts are false.

So:

Running the example program

The example program shows how to use Boolean expressions in pairs within if, else statements. Both && (and) and || (or) examples are shown.

When Boolean statements are combined this way they are called compound Boolean statements.


The example program

0ptions
Angle measurement: Degrees Radians

Go on input

Bounds:
Sorry, apparently this browser does not support HTML 5 canvas tag graphics.
Printer

Edit: Runtime          View: Starter Suggestion

To runtime:

Storage:

Editor width: Normal Wide


		

	


		

	


		

	

And....

Next tutorial: Not


All contents of EZ Math Movie are copyrighted: Copyright 2011, Edward A. Zobel. All rights reserved.



Other sites you may find interesting

Zona Land Education Like Math and Physics?

Zona Land Education offers explanations, diagrams, and animations on these topics.
And it is all presented in a clear and casual manner.

Here is the link: zonalandeducation.com




EZ Programing Demos Want to learn about programing computers?

EZ Programing Demos has an animated, slow motion, interactive programing language demonstrator.
The site presents basic ideas at a beginner's level.

Here is the link: ezprogramingdemos.com



Custom Search

Name:
Domain:

Name
0


Name:
Name
0