CS 111 - Week 9 Lecture 1 - 2016-10-18
* In Racket, recall the cond expression we used for
conditional expressions (for branching):
(cond
[bool-expr1 result-expr1]
[bool-expr2 result-expr2]
...
[else result-expr-else]
)
* the following has similar/analogous behavior
in C++:
if (bool_expr1)
{
return result_expr1;
}
else if (bool_expr2)
{
return result_expr2;
}
...
else
{
return result_expr_else;
}
* the above is actually a chain of the
following smaller statement:
if (bool_expr)
{
statement;
...
statement;
}
* if bool_expr is true, then do the statements following --
otherwise, don't! just go on;
optionally, this can have an else:
if (bool_expr)
{
statement1;
...
}
else
{
statement2;
...
}
* if bool_expr is true, then do statement1...;
otherwise, do statement2,...;
NEVER do BOTH statement1... and statement2...!
do one OR the other;
* (and we sketched the classic if-statement
FLOW CHARTS on the white board...)
* confession time:
the ACTUAL syntax is:
if (bool_expr)
statement;
...exactly ONE statement in the if branch!
BUT, happily, a BLOCK,
{
statement;
...
statement;
}
...can be put anywhere a statement can go
* so, yes, if your if or else ONLY has
1 statement to do, you don't HAVE to
use curly braces,
BUT you do (CLASS STYLE) need to indent
that statement by at least 3 spaces:
if (bool_expr)
statement;
* AND yes, it is also class style
that the curly braces are each on their
own line,
indented even with the if or else,
and their statement(s) indented
within by at least 3 spaces
* NOTE that you HAVE to have ( ) around
the if part's boolean expression!!!!!!!!!!!!!!!!!!!!!!!