=====
CS 111 - Week 10 Lecture 2 - 2026-10-28
=====
=====
TODAY WE WILL:
=====
* announcements
* intro to the C++ switch 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!
=====
another C++ branching statement:
switch statement
=====
* more-specialized branching statement!
* you can handle any branching-type situation using
some combination of if statements --
BUT!!!! probably because it can be implemented very
efficiently in C++, C++ also supports a 2nd, more special-purpose
conditional statement, that works well for a specialized but
pretty common set of situations;
* consider a strange setting with
single-letter character letter grades
and their descriptions, as follows:
'A' or 'a': Excellent
'B' or 'b': Very Good
'C' or 'c': Acceptable
'T' or 't': Try Again
* WHEN you are branching based on integer-based data
* that is, type int or type char or type bool
or a C++ enumerated type
AND are choosing between 3 or more options,
a switch statement can be nice!
* syntax:
switch(int_based_expr)
{
case val1:
case val4:
statement(s);
break;
case val2:
statement(s);
break;
case val3:
statement(s);
break;
...
default:
statement(s);
}
* semantics:
* the switch's int_based_expr is evaluated,
and it is compared to each case's value in turn
until it is found to be == to one of them
at that point, that case's statements are done
and it "falls through" keeping doing statements
until a break is encountered,
at which point you leave the switch's block and continue
at the next statement
(of course, remember that a return always ends
a function... so if a return happens to be encountered,
that will end the entire function at that point...)
* if NO cases' values match, the default section's actions
are done (if there is a default section)
no default, and none match?
...the switch will do nothing!
* it will be like an if with no else whose boolean
expression is false.
* NOTE -- if two values have the SAME action,
can use that "falling through" behavior to
good effect!
// desired_statement1 and desired_statement2 will be
// done for a switch expression of 'a' OR 'A'
case 'a':
case 'A':
desired_statement1;
desired_statement2;
break;
=====
* see example: describe_grade
=====
=====
* common switch statement errors to
avoid:
* remember the colon after the case's
value!
case 'A':
* remember the switch expression
HAS to be "integer based" --
for example, it CANNOT be double, it CANNOT be string!
* especially when the switch's actions
are not return, remember those break
statements! (else can get errors
from the "falling through" action...)
* avoid putting logical conditions
in the case's values...!
the resulting true or false will
be compared to the switch expression,
in that case!
* (and since bool type is integer-based in
C++, you won't get an error, just a
comparison different than what you intend!)
=====
CS 111 CLASS STYLE STANDARD
=====
* you may ONLY use break statements within
a switch statements!!!!!!!
* (and you are NOT allowed to use continue statements in CS 111)