=====
CS 111 - Week 10 Lecture 1 - 2026-10-27
=====
=====
TODAY WE WILL:
=====
* announcements
* intro to our first C++ branching
statement: if statement
* prep for next class
=====
* should be working on Homework 8!
* at-least-first-attempts due by 11:59 pm on
Friday, October 30!
* MEET WITH YOUR ADVISOR(S)!
* CREATE or UPDATE your DARS plan!
* get ready to REGISTER for SPRING 2027 between
November 9-20!
* CS and SE majors:
* in Spring 2026, you should take:
CS 112 - CS Foundations 2
Math 253 - Discrete Math
* You CAN take Math 101T - Trigonometry
and Math 253 CONCURRENTLY
* You CAN ask to switch to another
ENGL 103 section if it conflicts
with a major requirement such as CS 112
or Math 253 that you need to
stay on-track in your major!
====
C++ branching statement NUMBER 1:
the if statememt!
====
* in Racket:
(cond
[bool-expr1 result1]
[bool-expr2 result2]
...
[else result-else]
)
* the analogous logic in C++ would be something
like this:
if (bool_expr1)
{
return result1;
}
else if (bool_expr2)
{
return result2;
}
...
else
{
return result_else;
}
* but, that's a "chain" of a simpler thing,
an if statement:
=====
SIMPLEST if statement:
=====
* SYNTAX:
if (bool_expr)
stmt;
* SEMANTICS of this:
if bool_expr is true, DO the statement following,
otherwise just continue without doing it.
* (see simplest-if.pdf posted with these notes,
and ALSO on Canvas, in Modules,
in the "C++ Transition Readings" section --
it includes a FLOW-CHART of its semantics)
* NOTE: ONLY ONE statement can follow the if (bool_expr)!
HOWEVER:
* C++ block:
* a set of curly braces with statement(s)
within it is a block
{
statement;
...
statement;
}
* AND: a block is considered a SINGLE statement
to C++, and can be put anywhere a statement can be
put!!
* since we often do more than one thing
for a C++ branching option,
we can take advantage of the C++ block
for an if's action:
if (bool_expr)
{
stmt;
...
stmt;
}
* I tend to always use:
if (bool_expr)
{
statement;
}
...so I don't FORGET to add { } if I later change it to:
if (bool_expr)
{
statement;
...
statement;
}
=====
* OPTIONALLY, you can have an else part with the statement
to be done if bool_expr is false
=====
* SYNTAX:
if (bool_expr)
{
stmt(s);
}
else
{
stmt(s);
}
* SEMANTICS of this:
if bool_expr is true, DO the statement1 following the if part,
otherwise DO the statement2 following the else part
* NOTE: you will NEVER do both! You will do
EITHER the if's statement OR the else's statement!!
* (see if-else.pdf posted with these notes,
and ALSO on Canvas, in Modules,
in the "C++ Transition Readings" section --
it includes a FLOW-CHART of its semantics)
=====
Can you have an if statement as part of an if statement?
As the if's statement? As the else's statement? In a block?
=====
* Sure! When we say "statement", that means any statement,
including another if statement!
(but every else HAS to be part of an if...!)
* SO: so many combinations are possible!
* And you want to choose one that works for what YOU want
to do!
* for example, if you have multiple choices and may choose 0 OR MORE
of them,
just use a sequence of if statements!
if (bool_expr1)
{
statement1;
}
if (bool_expr2)
{
statement2;
}
if (bool_expr3)
{
statement3;
}
* do you see that, in theory, depending on the bool_expr expressions
involved, you might do 0 or more of statement1, statement2, statement3?
=====
if-else-if pattern - when you are making EXACTLY 1 choice from 3 or more options!
=====
* if you want, though, to make exactly ONE from 3 or more
choices, here is a common accepted alternate indentation
for a chain of ifs --
* (I call this the if-else-if pattern)
if (bool_expr1)
{
statement1;
...
}
else if (bool_expr2)
{
statement2;
...
}
else if (bool_expr3)
{
statement3;
...
}
...
else
{
statement_for_else;
...
}
* SEMANTICS: the above is ANALOGOUS to BSL Racket's cond expression!
and behaves similarly!
...each bool_expr is attempted until one is true,
and that bool_expr's action/statement is done
and the rest are SKIPPED!
(cond
[bool_expr1 result_expr1]
[bool_expr2 result_expr2]
[bool_expr3 result_expr3]
...
[else result_for_else]
)
* (see if-else-if-pattern.pdf posted with these notes,
and ALSO on Canvas, in Modules,
in the "C++ Transition Readings" section --
it includes a FLOW-CHART of its semantics)
* and this approved alternate indentation is easier to type and read than:
if (bool_expr1)
{
statement1;
...
}
else
{
if (bool_expr2)
{
statement2;
...
}
else
{
if (bool_expr3)
{
statement3;
...
}
else
{
statement_for_else;
...
}
}
}
* you could consider this a choose-exactly-one
template for a C++ function body that is to select
exactly ONE of 3 or more options:
if ()
{
return ...;
}
else if ()
{
return ...;
}
...
else
{
return ...;
}