===== CS 111 - Week 11 Lecture 1 - 2024-11-05 ===== ===== TODAY WE WILL: ===== * Announcements * Intro to C++ switch statement * Intro to local variables (and maybe cin?) * Prep for next class * should be working on Homework 9, at-least-1st-attempts due by 11:59 pm on Friday, November 8 * important upcoming schedule notes! SUBJECT TO CHANGE depending on cs50 IDE status...!!! ===== *** UPDATED these on Wednesday, November 6, after considering cs50 IDE issues plus other factors *** ===== * Thursday, November 7 - review for Exam 2 * there will again be a handout of review suggestions * you will again be able to make a handwritten notes page that you can submit a photo of for bonus credit and use during the exam * Friday, November 8 - WILL be a lab exercise (on switch statements) * at-least-first-attempts at Homework 9 due by 11:59 pm on Friday * TUESDAY, November 12 * graded Exam 1s will be returned during class (so you can look over them before taking Exam 2!) * will continue discussing LOCAL VARIABLES and related topics * ALSO: have your final versions of Homeworks 7-9 submitted by 11:59 pm on TUESDAY, November 12, * WEDNESDAY, November 13 * ...so example solutions can be posted for Exam 2 study purposes by 12:01 am on WEDNESDAY, November 13 * Raul, CS 111's Learning Assistant, will give an EXAM 2 REVIEW SESSION on WEDNESDAY, November 13, in BSS 302 from 3:00 - 5:00 pm * THURSDAY, November 14 - * Exam 2 study guide *bonus* is due by 9:00 am on Canvas * Exam 2 given in class (in GH 218) from 9:00 - 10:20 am ==== intro to the C++ switch statement ==== * a special-purpose branching statement * possibly the fact that it can be implemented very efficiently helps explain why it exists in the form it does... * when should you consider using this? * when making exactly 1 choice from 2 or more choices * AND those choices are between options you represent as an integer-based type * C++ integer-based types include: * int * char * bool * enumerated types * SYNTAX: switch (int_or_char_or_bool_expr) { case value1: statement1; statement2; ... break; case value2: statement 3; ... break; ... default: statement 4; ... } * SEMANTICS: * the switch's int_or_char_or_bool_expr is evaluated * then it is compared to each case's value until one is found that's EQUAL to it and at that point, it starts executing that case's statements and continues until: * a break is reached, which causes a jump to the end of the switch's block * a return is reached, which causes the function to be ended and the specified value returned * the end of the switch is reached * 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! * see example: describe_grade * 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; ===== * 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! ===== CS 111 CLASS STYLE STANDARD ===== * you may ONLY use a break statement in switch statements!!!!!!!! * (and you are NOT allowed to use continue statements in CS 111) ==== local (non-parameter) variables ==== * a 4th use for identifiers! * strictly speaking, parameters are local variables, because their scope is local to the function body; BUT assume for this course that when I say "local variables", I mean local (non-parameter) variables! * local variables are declared INSIDE a block: { ...INSIDE a set of curly braces... } and their SCOPE (the place they have meaning) is JUST within that block * so far we have been using a functional style of programming... (not using local variables whose values change) But -- local variables CAN be given values, and they can have those values CHANGED.... ...the fancy word for this is MUTATION * in a style of programming called imperative programming, you might find it useful to have a local (non-parameter) variable whose value can change during a function... * think of a local variable not like in math so much, but as the name of a location in memory that I can put different values into over time (it only has ONE value at a time) useful mental model: think of a local variable as a box that can hold one value of that variable's type * you can change what's in it -- that REPLACES the previous value * WHY might you do this? * to help control a function! (a variable whose value keeps track of, say, how much you have done, so you know when to stop) * to hold a value the function has just obtained from the user or a file (or etc.) * to hold a "running" result * etc.! (there are also more!) * and sometimes you just want to give a name to some computation that's not quite a function that you want to reuse a few times in a function ==== * how do you declare a (non-parameter) local variable? INSIDE a block: { } ...you put a type and then the desired variable name: { int quant; // quant is a local variable of type int string next_movie; // next_movie is a local variable of type string double movie_price; // movie_price is a local variable of type double bool found_it; // found_it is a local variable of type bool } now -- STYLE RULE!!!!!!! - you should NOT assume these have a value until you GIVE them one -- you are supposed to INITIALIZE them before you USE them. * (it is NOT part of the C++ language definition WHAT their value is if not yet initialized -- some COMPILERS may or may not put values in, but they are NON-standard, and NOT to be depended upon!) * at least 3 ways to initialize a local variable: 1. I can assign a value when I declare it (using = followed by any expression of a compatible type { int quant = 0; // quant now has the value 0 string next_movie; double movie_price; bool found_it; } 2. I can assign a value to it using an assignment statement. loc_var = compat_expr; // loc_var has the value of compat_expr { int quant = 0; // quant now has the value 0 string next_movie; next_movie = "This Week's Feature"; // next_movie now has value of // a string version of // "This Week's Feature" double movie_price; bool found_it; } 3. I can read a value into it using interactive input, for example using the cin object and >> operator { int quant = 0; // quant now has the value 0 string next_movie; next_movie = "This Week's Feature"; // next_movie now has value of // a string version of // "This Week's Feature" double movie_price; cout << "What is the movie price? "; cin >> movie_price; // what user enters is converted // a double value and becomes // movie_price's value bool found_it; }