Please send questions to st10@humboldt.edu .

conditionals in C++: (branching in C++)
---------------------------------------

*   in Racket, we had:

(cond
    [boolean-expr1 result1]
    [boolean-expr2 result2]
    ...
    [else result-else]
)

*   in C++, you can get analogous behavior to Racket's cond
expression with:

if (bool_expr1)
    return result1;
else if (bool_expr2)
    return result2;
...
else
    return result_else;

*   Each of the result-statements, above, can ONLY be
    a single statement -- 
    BUT because any single statement
    can be replaced with a block:

{
    statement;
    ...
    statement;
}

    ...that is considered to be one statement to the C++ compiler,
    this can ALSO be written as:

if (bool_expr1)
{
    return result1;
}
else if (bool_expr2)
{
    return result2;
}
...
else
{
    return result_else;
}

*   that's really a useful combination of a simpler
    statement -- here's the simplest if statement:

if (bool_expr)
    statement;

    *   IMPORTANT: you HAVE to have a set of parentheses
        AROUND the if-statement's boolean expression!

    *   and only ONE statement can follow it (but 
        you can make more look like one by putting
	a block instead, as noted earlier)

    *   semantics: if the boolean expression is
        true, do the statement;

if (value < 10)
    return true;

if ((val > 10) and (val < 100))
    return "sure, why not?";

*   OPTIONALLY, you can have an else clause -- its statement
    (or block) is ONLY done if the if's condition is false;

if (bool_expr)
    statement1;
else
    statement2;

    *   if bool_expr is true, statement1 is done;
        otherwise, statement2 is done;

*   and, again, only ONE statement can follow the if-clause or
    else clause!
    BUT a block:
{
   statement;
   ...
   statement;
}

    IS considered a single statement by the C++ compiler

*   SO -- some people choose to just always put:

if (bool_expr)
{
    statement1;
}
else
{
    statement2;
}

...so they don't forget to put the blocks ({ }) if they 
decide to add another statement to one of the options
later...

*   IMPORTANT: REQUIRED COURSE STYLE:
    *   the statement for the if and the statement
        for the else are required to be on a separate
	line than the if and else, and indented by
	at least 3 spaces

    *   if you put a block, it is expected to be
        written as seen above -- the { is on
	the line AFTER the if or else, by itself, lined
	up with the if or else,

	its statements are indented by at least 3
	spaces from the {,

	and the } is on its own line, lined up with the
	{.

        (not sure what I mean above? ASK ME, or just
	write your if statements as you see in
	the posted in-class examples! I'm following
	this style in all of the if-statements
	I'm showing you...)

*   EXAMPLE:

Remember this absolute value function from Racket?

(define (abs-value num)
   (cond
       [(< num 0) (* -1 num)]
       [else      num]
   )
)

Here's what it can look like in C++:

double abs_value(double num)
{
    if (num < 0)
        return -1 * num;
    else
        return num;
}

(or, if you prefer:)

double abs_value(double num)
{
    if (num < 0)
    {
        return -1 * num;
    }
    else
    {
        return num;
    }
}

*   also see the spiciness function example;

switch statement:
-------------------

*  C++ also has a 
   special-purpose multi-way branch statement

switch(int_or_char_or_bool_expression)
{
    case value1:
        statement1;
        statement2;
        ...
        break;

    case value2:
        statement1;
        ...
        break;

    ...

    default:
        statement1;
        ...
        statementn;
}

*   the semantics/meaning of this: compare the switch
    expression with each of the case values, in turn --
    
    as soon as one matches, do its statements;

    BUT here is the weird part: it KEEPS GOING, doing
    EACH case's actions, until a 
break;
    "breaks" you out of the switch statement;

    *   (but, notice that a return also ends
        a function -- so it can have a similar
	effect, effectively...!)

*   example (created after class): 
    see function describe_grade.cpp