=====
CS 111 - Week 11 Lecture 1 - 2025-11-04
=====
=====
TODAY WE WILL:
=====
* announcements
* intro to the C++ switch statement
* intro to local variables and cin
* [did not start intro to cin yet, though]
* prep for next class
=====
* should be working on Homework 9!
* at-least-first-attempts due by 11:59 pm on Friday, November 7!
* if not yet done for Spring 2026 advising:
MEET WITH YOUR ADVISOR(S)!
* RE-CREATE your DARS plan!
* get ready to REGISTER for SPRING 2026 between November 10-21!
* 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!
=====
UPCOMING SCHEDULE NOTES
=====
* EXAM 2 is THURSDAY, NOVEMBER 13
* we'll review for that in-class on Thursday, November 6
* yes! clicker questions! and an exam review handout!
* Friday, November 7 - there IS a lab exercise!!!
* and at least 1st attempts at Homework 9 due by
11:59 pm on Friday, November 7
* final improved versions of problems from Homeworks 7-9
are due by 11:59 pm on MONDAY, NOVEMBER 10
so that example solutions to these can be available
on Canvas at 12:01 am on TUESDAY, NOVEMBER 11
* Tuesday, November 11 - Humboldt's Veterans Day Holiday - campus
is closed
* Wednesday, November 12 - LA-run Exam 2 Review Sessions
* 9:00 am - 10:50 am - Kai and Enrique
* 3:00 pm - 4:50 pm - Ambrose and Raul
* LOCATIONS TO BE ANNOUNCED
* 4.5 clicker bonus points for attending and participating
in one or both of these
* (OK to come to part of one if you class conflicts, etc.,
as long as you participate...)
* Thursday, November 13 - EXAM 2
* Friday, November 14 - there WILL be a lab...!
=====
conditional statements in C++, part 2: the switch 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;
* what kinds of situations, then?
* you are making exactly one choice from 2 or more
options
* and those choices are between options that can be
represented as either int, char, bool or an
integer-based type --
really, between ENUMERATED-style data you can represent
as a C++ integer-based type!
* syntax:
switch(int_or_char_or_bool_expr)
{
case value1:
statement1;
statement2;
...
break;
case value2:
statement3;
statement4;
...
break;
...
default:
statementX;
statementY;
...
}
* semantics:
* the switch's int_or_char_or_bool_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)
=====
start C++ local variables and mutation
=====
* we know about named constants,
and parameters,
and function names --
all are examples of IDENTIFIERS,
names a programmer chooses!
* here's another kind of identifier:
you can declare an identifier within a block
(that's not a named constant) and that is a LOCAL variable;
...it is LOCAL to that block,
it only has meaning in that block.
(its SCOPE is just that block, from where it is declared
until the block's closing } )
desired_type local_var_name;
* you can, if you choose, initialize it when you declare it
desired_type local_var_name = desired_expr;
* you can also use an ASSIGNMENT STATEMENT to give it a value
or CHANGE its current value (!!)
local_var_name = expr;
* if local_var_name had a value before this,
that value is REPLACED by expr !
* in so-called imperative programming,
such mutation -- deliberately changing a
local variable's value -- can be used to
help CONTROL what a program does;
MORE on that later!
* IMPORTANT: C++'s language definition does NOT specify
what is in a local variable that has been declared but
not given a value -- so different compilers treat this
DIFFERENTLY
========
CS 111 COURSE STYLE STANDARD: always give a value to a
local variable before you USE it!
========
* but you can see a first example of a local variable
in the 3rd version of describe_grade in today's example
program