.Open cs201/06 . Previous -- if statements .See ./05.html .Open 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 .Open 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 " 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 .As_is cout << boolalpha << (p && !q && r) << ... for example. Here .See ./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: .As_is string input; .As_is cout << "Are you male (y/n)? ); .As_is cin >> input; .As_is 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 .See ./george2.cpp .Close . Common Error 3.4 Multiple Relational Operators Beware writing .As_is if( a or b == 1).... when you need .As_is if( (a==1) or (b==1) ) ... . Common Error 3.5 Confusing && with || . 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"? .Close . Exercise -- Calculate if a given year (2000 for example) is a leap year This program .See ./04ex.cpp is buggy. Why? Fix it! . Project 2 Due .See ./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). . Lab 03 -- Simple Graphics .See ./lab03/ . Next -- Loops .See ./07.html .Close