User:Roylei

harold notes (since I've pretty much made this tradition in my lessons). So...

- Firstly, hopefully you have read the lessons to date. You should be well versed in simple input/output as well as variables.

- Secondly, hopefully the lessons are teaching you some valuable skills in C++. Once again, it is awesome to see people adding on to the lessons.

As the name implies, conditional statements specify whether another statement or block of statements should be executed or not. These are often called "selection constructs". The two general types are "if...then" and the "switch...case" construct. Note that there is no looping involved here, but that conditionals are involved in loops.

Comparison Operators
The conditions tested are specified using comparison operators. These operators cause the immediate statement in which they are contained to return a boolean value of either true or false. (Note that in certain circumstances they may evaluate to 0 or 1; be careful combining conditional statements with arithmetic)

The following comparison operators are available:
 * Equality: ==, or Inequality: != of any primitive data type (int, char, float, bool, etc.) These are binary operators (take two operands) and are specified using infix notation (which means that the operator goes in between the two operands).
 * Greater-than: >, >= or Less-than: <, <= Also binary operators using infix notation. Use only with numeric data types; there are specific functions for comparing other data types.
 * Negation: ! is a unary operator, and prefixes the operand.

Examples: evaluates to true. evaluates to true. evaluates to false. evaluates to false. evaluates to true. evaluates to false. is invalid.

The if...then Conditional
There are two variations to the if...then conditional. Let us take a simple case and explain it line-by-line.

Explanation:
 * 1) This is the first line.  Note that there is space between the keyword if and the opening parenthesis.  Inside the parentheses is the condition; in this case it is a test for equality.  It is good practice to use the same type arguments (not comparing floating-point values to characters).  Note also the left curly brace {.  This symbol denotes a block of multiple lines of code.  Without it, the conditional would only refer to the statement immediately following it.  It is good practice to always use the braces.
 * 2) This comment represents the body of the conditional statement.
 * 3) The right curly brace is essential; it matches the opening brace on line 1 and signals the end of the if body.
 * 4) This line is optional.  If there is a sort of "default" behavior that should be carried out, it would be placed here.  The else clause does not belong by itself, only directly following an if clause.
 * 5) This is the body of the else clause.
 * 6) This curly brace is also essential; it matches the opening brace on line 4 and signals the end of the else body.

The body of the else block can be another if statement. This is known as "nested conditionals" because the conditionals are indeed nested; that is, placed inside of one another.

The switch...case Construct
This tests an input variable for equality with any number of cases and then executes the corresponding code. The syntax follows:

Bold text

Again, here is a line-by-line explanation:
 * Line 1: The opening line is very similar to the if construct, described above. The only difference is that the switch...case construct only uses int values and character constants or character literals.  For this reason, the switch-case can come in handy but it is limited to these circumstances.
 * Line 2: For each expected value, code a line such as this one. The 1 means that if a does contain an integer value equal to 1, execution will begin following the colon on this line.  Execution will continue blindly, so,
 * Line 4: contains a break statement that forces execution outside of the switch block.
 * Line 8: The default block is similar to the else block in a normal conditional. It is chosen if none of the previous cases matched the value of the condition variable.

Note, the break statement can be under-used to the programmer's advantage. If multiple values of the condition variable would lead to executing the same code, the cases can be listed one after another like this:

The Conditional Statement
There is one more conditional statement, and it is truly a contraction of the if...then construct, however, this one is quite compact and can fit on one line or even be embedded within a larger expression. This is the syntax:

The entire expression evaluates to the expression given in the codeIfTrue and codeIfFalse statements. If those are complete statements, then that code is executed. It is also possible to specify a single value for each of those, and assign this conditional statement to a variable, like this:

As you can see, this makes simple conditionals all the simpler, but can complicate readability. Use sparingly.

See if you can come up with a few usages of the conditional constructs you have just learned.