. 13 Logic and Conditions . Previous -- Control Structures .See ./12.html .Open Prepare . 5.8 Logical Operators VITAL! . 5.9 Confusing the Equality (==) and Assignment (=) Operators All too common. . 5.10 Structured Programming Summary Review. . 5.11 Software Engineering Case Study: Identifying Objects’ States and Activities in the ATM System . 5.12 Wrap-Up Review chapter .Close Prepare . Deliver a question .Open Questions and Answers . Is there an if-if structure Example .As_is if(if(.....) ).... No. However this is OK .As_is if(....) .As_is { .As_is if(....) .As_is ... .As_is } . If you repeat a loop does it restart Yes. . If you use return in a loop does it terminate the loop Yes. . When combining small programs into a large one do you modify the variables Yes. You also can put the parts between {braces} and then refactor out the shared variables: .As_is { declare shared .As_is { declare local only .As_is } .As_is { declare local only .As_is } .As_is { declare local only .As_is } .As_is } The best way to combine small programs is to make them into functions and call them as needed.... coming up next. . Explain boolalpha This is only used in quizzes, exams, and examples. It is part of and uses namespace std. So you may need .As_is using std::boolalpha; Normally, when you output a bool it displays as either 1 or 0. 1 is true and 0 is false. After boolalpha you get a truncated form of "false" and/or "true". Use it when you want to display bool values/conditions as "t" or "f". This is rare -- mainly: debugging, classroom exercises, lab work, examples in books, ... Mostly it is better to show spell out what the condition means with something like this .As_is cout << .... << (single? " Single " : " Married ") << ...; . Are break and continue only used on loops The "break" is used with switch. The continue only with loops. . When to use negation rather than the negated relation When you want the opposite of a complex condition .As_is if ( ! (x>3 or y <2) ) Note -- the "!"/"not" operator doesn't cost anything much in time or space3 and saves brain power.... which is more expensive. When the condition is not based on relations you can not just change == to != etc.. For example .As_is if(!(n%p)){ /*n is not divisible by p*/} As an example in the lab you met: .As_is while(cin >> value){....} which reads each `value` until the end of file (or when some one types a vlaue that doesn't fit). The `cin>>value` is a combined command and condition. You can test for the presence of data and also get the first one: .As_is if(!(cin>>value)) .As_is { .As_is cout << "No data! \n"; .As_is return 1; .As_is } .As_is double max=value; double min=value; double sum=value; .As_is double sumsquares=value*value; .As_is while(cin>>value) // proces the other values .As_is { .As_is ... .As_is } . Why can we only combine the structures using nesting and stacking Because it is not clear what it means.... and because C++ syntax won't let you do it. . Why are nonzero values treated as true The designers of C thought that zero ( in binary: all the bits a zero) was clearly the way to encode "false". They threfore decided that all other values (which would have a non-zero bit somewhere in them) should be "true" in a condition. It turns out that this was a smart choice making code simpler and faster in a lot of programs. The alternative was to choose a value (say one(1)) to mean "true" and add a run time error if ever a condition was neither 1 or 0. This would make a lot of programs more complex and a bit slower. . bool forms of negative numbers These are treat as "true" because they are nonzero. . When can you use logical operators && || ! In any condition -- in if, if-else, while, for, ..., and do-while. You can also use then to give values assigned to bool variables (flags): .As_is bool flag = false; .As_is ... .As_is flag = (n>2)&& !(n%p); . Can you write the exclusive or function in C++ Yes. The `exclusive or` is true when two Boolean variables have different values. And C++ is happy to apply '==' and '!=' to bools: .As_is bool a, b, c; .As_is .... .As_is c = (a != b); "Believe it or not." . Is using && || ! vs and or not just a personal preference These days yes. In some older compilers we don't have and/or/not. . Is bool like truth tables Yes. Indeed one way to design a logical (bool) expression is to write out a truth table first and then code it using the rows that are true... . Why logical negation It saves thinking! . Why can't the compiler reject assignments in conditions Because many programmers, in the last 30 years, have put assignments in conditions with care and great success. A classic maneuver is .As_is while ( (data = cin.get()) != EOF ).... . Explain UML State Machine Diagrams State Machine diagrams are elaborated from the "finite state machines" invented by the very first computer scientists to model brains, circuits, and computers. They are used in software engineering, in networking, and in computer science theory. They show how the `state` of something changes under the influence of `events`. For example, a door is either open or closed. These are the two states for a door (we ignore "half open") for this example. Suppose we are talking about an elevator door, then the events happen when buttons are pushed. There are buttons labelled "open" and "closed". As a result we get the first diagram below that describes how the door behaves. Here is another example. I have a ball point pen. If the refill is "out" I can write with it. If the refill nib is "in" it is safe to put in my pocket. In this case there is a single type event "click" when I click the button on top. Sometimes the nib comes out and sometimes it goes in.... See the second state machine below. State machines can have more than 2 states. They can have any finite number. In my inlaws house there was a lamp in the bathroom. It had a pull chain. It had for states: .List Light off, heat off Light on, heat off Light off, heat on Light on, Heat on .Close.List By pulling the chain you could get to any one of these for states -- see the third state machine below. .Image StateMachines.png [Three state machines] For a couple of more practical examples try .See ../samples/uml0.html#State State machines are a marvelous way to analyse dynamic problesm, complex protocols, and design intricate solutions that work. . Explain lvalues and rvalues Ever since the ancient language of BCPL (1970s) we have distinguished the kind of value that can appear on the left of an assignment from the kind of value that is normally needed on the right hand side of an assignment. .As_is lvalue = rvalue; We say that a variable has an lvalue and an rvalue. The lvalue is the address in the computer that is given to the variable by the compilation process. It indicates where data is found, and where it is put. This is the value that is needed on the left of an assignment. This is also what is needed in cin statements -- we need a place to put things. A variable has an address of a piece of storage or memory. This piece of memory holds the `rvalue` of the variable. When we put a `lvalue` on the right of an assignment we get the data that is in the memory -- not the address. This also happens in cout statements where we need a value to be printed. So far the only `rvalues` you have met have been ints, doubles, floats, strings, and so on. None of these is the address of some data. So it is not a valid `lvalue`. You can not compile .As_is 3.14159 = 17; for example. It is trying to change a constant! So this is forbidden. In C++ we have an operator that explicitly gives the lvalue of a variable where we would normally get its rvalue. .As_is cout << & variable; In CS202 you meet a kind of varaible that stores addresses -- and these can get confusing.... they are called pointers. .Close . Exercises . Next .See ./14.html .See ./lab07/