===== CS 111 - Week 15 Lecture 1 - 2024-12-10 ===== ===== TODAY WE WILL: ===== * announcements * intro C++'s shorthand operators * intro C++'s "classic" for loop * prep for next class ===== * Should be working on Homework 12! * at-least-1st-attempts due by 11:59 pm Friday, Dec. 13 ===== * Please take the NSF Student Perceptions survey (link is in Canvas) * Please consider completing the CS 111 Course Evalutions (see link next to Modules in Canvas) ===== UPCOMING SCHEDULE NOTES ===== * Thursday, Dec 12: review for Final Exam * Friday, Dec 13: Week 15 Lab Exercise! * get at-least-1st attempts at Homework 12 in by 11:59 pm Friday Dec 13 * Sunday, Dec 15: 11:59 pm - any IMPROVED parts of Homeworks 10-12 are DUE * Monday, Dec 16: 12:01 am - EXAMPLE SOLUTIONS for Homeworks 10-12 will be available on Canvas * Tuesday, Dec 17: ******************* 8:00 AM!!!!!!!!! ********************** in GH 218 is the CS 111 Final Exam! ====== C++ shorthand operators ====== * not vital/required -- but they are convenient! (and frequently used!) * we end up changing local variables based on their previous value a lot!! blah = blah desired_op; ...these features are based on: gee, can I avoid repeating that local variable on BOTH sides of the assignment statment? * and another two are just for the common action of increasing something by 1 (or decreasing it by 1) int count = 0; * I can increase this by one using: count = count + 1; * ALSO -- C++ has += count += 1; same as: count = count + 1; int amount = 100; amount += count; same as: amount = amount + count; * also has *= double total = 100.0; total *= 0.18; same as: total = total * 0.18; * also has -= amount -= count; same as: amount = amount - count; * also has /= total /= 3.0; same as: total = total / 3.0; ===== Increment and Decrement operators ++ and -- ===== * MOST common thing that gets done to numeric variables? ...adding 1 to them; * ++ is an operator that adds 1 to a thing! (that can be assigned to) it can be PREFIX or POSTFIX ...you can put it BEFORE the thing or AFTER the thing int count = 0; count++; // count is now 1 ++count; // count is now 2 and -- is an operator that subtracts 1 from a thing! (that can be assigned to) count = 10; count--; // count is now 9 --count; // count is now 8 * but note: these are also expressions, they can be used on the RHS of an assignment statement with OTHER operators, so: what is the value of these as *expressions*? name++ - the value of this expression is name BEFORE it is increased by 1 ++name - the value of this expression is name AFTER it is increased by 1 ===== intro to C++ "classic" for loop ===== * consider this common "pattern" for a while loop used in a count-controlled style: int count = 0; while (count < num_reps) { do things; ... count++; } * count-controlled loops are COMMON, and it is TOOOO easy to forget to update the counter in the loop or to forget to initialize it before the loop; ...SO: the for statement is a specialized repetition statement especially useful for count-controlled loops * OK, it also happens to give the compiler useful additional info the compiler can use to generate more-effecient code! syntax: for (<init-part>; <bool_expr-part>; <update-part>) statement; * like while and if statements, only ONE statement can be in a for loop body -- BUT that single statement can be a block doing more (but still considered a single statement by the compiler) semantics: when you reach a for loop: 1. the <init-part> part is done 2. then the <bool_expr-part> is checked; if true, do the for's body, then do the <update-part> part, then return to 2. if false, go to 3. 3. go to next statement after the loop So.... // this is ALMOST identical to the while earlier for (int count = 0; count < num_reps; count++) { do things; ... } * the ONE difference: if you declare a variable in the <init> part, its scope is JUST the body of the for loop * for the for loop above, if you tried to use count AFTER the for loop body, it would be considered an undeclared variable! * if you want/need the value later, declare your loop counter variable BEFORE the for loop: int count; for (count = 0; count < num_reps; ++count) { do things; ... } // now count is still part of the scope after the // for loop body, and can be used * see refactored version of get_smallest now using a for loop in 111lect15-1.cpp