[Section not yet complete...]
A condition is an expression that evaluates to true or false. I'd argue that it's the most important fundamental building block of any program, as it's where decisions are made.
Here is a really basic examples:
if (true) {
// this line will get executed
}
if (false) {
// this line will not be run
}
The expression is what is contained in the parentheses, and there are countless ways of creating an expression, as the only requirement is that they can evaluate to true or false. It may also be useful to point out here that false can be expressed as 0 (zero), and true as any non-zero value, which also means that there is a lot of flexibility in what can be used as an expression.
For a standard expression, there are two types of comparison operators:
- Logical operators - to test two or more boolean values to one another
- Relational operators - to compare two or more values, returning a boolean result
There are two logical operators: AND (&&), and OR (||). Comparing two booleans with AND will only return true if both sides evaluate to true. So: (1 && 1) would return a true, (1 && 0), or any other combination would return false.
Here is another simple example:
if (true && true); // evaluates as TRUE
if (false && true); // evaluates as FALSE
if (false && false); // evaluates as FALSE
if (true || true); // evaluates as TRUE
if (false || true); // evaluates as TRUE
if (false || false); // evaluates as FALSE
null statement
operator precedence: (), &&, ||, %, /, *, +, -
cout << (x==1) ? "one" : "not one";
int x;
switch (x) {
case 1: // do this
break;
case 2:
case 3: // do these
break;
default: // else, do this
}