When you have a choice, there are often multiple choices that you can make and what you do will depend on your choice:
What do I want to make for dinner?
The actions and costs depend on your choice:
How much will I study for the ECE 150 examination?
Almost no decision you ever make has a simple yes-no answer. "Are you going to class?" You could answer this with "yes" or "no", but you could also answer it with "yes, but I'll be 10 minutes late" or "I'll go for five minutes, but if I know the material, I'll leave."
For example, there is always the question: "Are you a hipster?". For this, Sunny Nihilist created the following flow chart:
For a computer, it would be too difficult to create a mechanism whereby multiple different blocks of code could be run based on specific responses to particular problems. Instead, a computer reduces each possible question to one of "yes" or "no", or "true" or "false".
For example, consider the following engineering solution:
At each point, there is a yes-no question being answered, and depending on the response, a different path is followed. In this case, each path has two questions.
Going back to the hipster question, we could require a yes-no answer to each, in which case:
For a computer, rather than referring to yes-no questions, we refer to these as a Boolean-valued statement or a conditional statement. All conditional operators return either true (1) or false (0).
Now, in C++, we have already seen that the result of a conditional statement is either 1 or 0; however, for clarity of reading, the C++ programming language includes two additional keywords, true, which evaluates to 1, and false, which evaluates to 0. You can try this out at repl.it:
// Pre-processor include directives #include <iostream> // Function declarations int main(); // Function definitions int main() { std::cout << " true = " << true << std::endl; std::cout << "false = " << false << std::endl; return 0; }
Thus, from here on in, when we are discussing Boolean-valued operators or Boolean-valued functions (functions that return true or false), we will use true and false and you must remember to remember that true is 1 and false is 0.
The sine function is a real-valued function, for it always returns a real value. The greatest-common divisor function is an integer-valued function, for it always returns an integer. A Boolean value is either true or false, and thus a Boolean-valued function is a function that returns either true or false.
A statement is an expression in C++ that evaluates to some type of value. Thus, in each of the following:
Definition: Boolean-valued
Taking on a value of either true or false; some
times denoted as 1 or 0 or yes or no.
Definition: Boolean-valued statement
A statement in C++ that evaluates to a Boolean value. A Boolean-valued
variable is a Boolean-valued statement, but so is the result of any
other Boolean-valued operator, including:
==,
!=,
>=,
<=,
> and
<.
Another game is Pictionary, a game where one player is given a word and is required draw that word. The player wins if the other players guess that word. A naive way of playing Pictionary is quite straight-forward:
This is not very good, as it really doesn't allow for significant hope to get a better solution:
Let's change the decision to keep going if the question is true:
Now, if you've made five improvements to your image and they still have not guessed it, perhaps it's a good idea to start from scratch: