[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 06
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] <06> [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Mon Apr 16 14:12:34 PDT 2012

# cs201/06

## Previous -- if statements

[ 05.html ]

## Study pages 115-121 Booleans and Logic

### 3.5 Boolean Operations

You are familiar with addition and subtraction operations. George Boole discoved that similar operations could be used to describe logical thinking. His work is the theoretical basis of all digital circutry and computers.

Learn the three Boolean operators: && (and), ||(or), and !(not).

Note -- until I mastered Boolean Algebra my programs tended to be full of stupid errors. People would laugh at my code....

### Missing Fact

You can use the words "and", "or", "not" in place of "&&", "||", "!" if you wish

### Missing Technique -- Boolean input and output

You rarely need to output or input Boolean values. An exception is when you want to print out a truth table.

#### Output bool expressions

First you should add "#include <iomanip>" to the header files listed before the program starts. Second you indlude the input-output manipulator "boolalpha" before you first attempt to "cout" a boolean --- this means that instead of outputting "1" or "0" the program will output "true" or "false". Third, the expression is put in parentheses -- the compiler treats boolean operators as a lower priority than the "<<" operator... So you end up with
` 		cout << boolalpha << (p && !q && r) << ...`
for example.

Here [ printBool.cpp ] is an example program showing a bool being output before and after the boolalpha manipulator has been used.

#### Input bool variables

You never ask a user to supply "true" or "false" in real programs! It is better to ask a "Yes/No" question and accept "yes"/"no". Then you can convert the sting into a bool variable. For example:
` 		string input;`
` 		cout << "Are you male (y/n)? );`
` 		cin >> input;`
` 		bool male = ( input.substr(0,1) == "y" );`
This allows people to input "y", "yes", "yep",.... etc.

#### Exercise

How many ways can you write code like the above to allow the user to respond "Y", "Yes", "YES", ... ?

#### Example of bool IO

[ george2.cpp ]

### Common Error 3.4 Multiple Relational Operators

Beware writing
` 		if( a or b == 1)....`
when you need
` 		if( (a==1) or (b==1) ) ...`

### Advanced Topic 3.3 De Morgans Law

This topic is too important for sorting out complex conditions fo you to put it off.

### Random Fact 3.1 Artificial Intelligence

What do you think is "intelligence"?

## Exercise -- Calculate if a given year (2000 for example) is a leap year

This program [ 04ex.cpp ] is buggy. Why? Fix it!

## Project 2 Due

[ projects.html#P2 ]

## Review Questions

Do as many as you have time for.... and hand in one written/typed question+answer: Page 147, R3.10, 3.11, 3.12, 3.13, and 3.14.

## Quiz 2

This will ask you to trace a series of assignments involving arithmetic. A calculator may be helpful. You may refer to a single sheet of paper (8.5><11 inches).

[ lab03/ ]

## Next -- Loops

[ 07.html ]

# Abbreviations

1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
2. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
3. Function::programming=A selfcontained and named piece of program that knows how to do something.
4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
5. OOP::="Object-Oriented Programming", Current paradigm for programming.
6. Semantics::=Rules determining the meaning of correct statements in a language.
7. SP::="Structured Programming", a previous paradigm for programming.
8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
9. Q::software="A program I wrote to make software easier to develop",
10. TBA::="To Be Announced", something I should do.
11. TBD::="To Be Done", something you have to do.
12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
13. void::C++Keyword="Indicates a function that has no return".