=====
CS 111 - Week 14 Lecture 2 - 2025-12-04
=====
=====
TODAY WE WILL:
=====
* announcements
* C++ shorthand operators!
* "classic" for loop!
* intro to file i/o
* prep for next class
=====
C++ shorthand operators!
=====
* these are more syntactic sugar -- they just make certain
common tasks easier/more convenient
* with local variables, you may be noticing we do a
lot of this:
int count = 0;
count = count + 1;
sum_so_far = 0;
while ....
{
sum_so_far = sum_so_far + next_val;
=====
Compound assignment operators: += -= *= /=
=====
* these let you "smush" these operations when they
are updating a local variable (or changeable location)
based on their current value
// these mean the SAME thing
count = count + 1;
count += 1;
sum_so_far = sum_so_far + next_val;
sum_so_far += next_val;
count = count - 1;
count -= 1;
quant = quant * 47;
quant *= 47;
quant = quant / 2;
quant /= 2;
=====
Increment and Decrement operators: ++ --
=====
* and the MOST common value one adds to a memory location
is 1...
SO C++ has a shorthand operator for that
(and for - 1 as well)
increment operator: ++
decrement operator: --
* FUN FACT: these have BOTH prefix AND POSTFIX
versions -- can write them either BEFORE or
AFTER the desired thing to change!
int quantity = 10;
// these each have the same EFFECT on quantity:
quantity++;
++quantity;
quantity += 1;
quantity = quantity + 1;
// and these also:
quantity--;
--quantity;
quantity -= 1;
quantity = quantity - 1;
* BUT: important note to explore more next week:
* compound expressions using ++ and --
CAN be within larger compound expressions --
there IS a difference between prefix and postfix
versions of these in THIS case,
and we'll briefly discuss these next week;
=====
"classic" for loop
=====
* you can do pretty much anything repetition-related
with a while loop;
* BUT -- loops controlled by a counter -- a local variable
counting something are REALLLLY common, right?
so, C++ provides a for loop that is very convenient for
these situations (and is also very efficient under the
hood)
* consider this common "pattern" for a while loop
used in a count-controlled style:
int count = 0;
while (count < num_reps)
{
// thing(s) to repeat
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-efficient code!
* a classic for-loop doing (almost) the same thing as the while
statement above looks like this:
for (int count = 0; count < num_reps; count++)
{
// thing(s) to repeat
}
* you can think of the 3 "parts" starting a for loop
as:
for (init_part; bool_expr; update_part)
* 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)
* "classic" for-statement semantics:
when you reach a "classic" for-statement:
1. the init_part is done
2. then the bool_expr is evaluated;
if bool_expr is true,
do the for's body,
then do the update_part,
then return to step 2.
else if bool_expr is false, go to step 3.
3. go to next statement after the for's body
* SIDE NOTE:
* if you declare a local variable in a for-loop's
init_part, its scope is JUST the for-loop's body;
(it is NOT visible/usable AFTER the loop)
* so if you WANT to use the local counting variable after
your for loop, declare it BEFORE the loop:
int count;
for (count = 0; count < num_reps; count++)
{
// action(s) to repeat
}
// this local variable count IS still visible here
* see refactored version of sum_array now using a "classic"
for loop and some of the shorthand operators in 111lect14-2.cpp.
=====
intro to stream-based file input/output in C++
=====
* we have been using the stream objects cin and cout
from the C++ iostream library to do interactive input
and screen output
* The C++ iostream library declares and sets up/opens
these stream objects for us,
connecting the cin input stream object to standard input,
and connecting the cout output stream object to
standard output.
* C++ also has a library fstream to support stream-based
file input/output --
this lets us declare and set up and use input file stream
objects for reading from files (in the same way that we
read from standard input),
and declare and set up and use output file stream
objects for writing to files (in the same way that we
write to standard output).
* SO: to do this:
* Step 1: to use this fstream library,
#include it in the .cpp file whose function(s) use it!
(*before* the using namespace std;)
#include <fstream>
* STEP 2: declare the needed file stream object
The iostream library could assume many users want
standard input and output, and could declare and create
the objects cin and cout accordingly
BUT the fstream library can't know how many files you want
to do things to/with -- you've got to declare your
file stream objects yourself;
* IF you want to write TO a file,
(like cout writes to the screen/standard output),
you declare an OUTPUT file stream object to connect to
this file to do this output
The type of this object: ofstream
ofstream my_fout;
* IF you want to read FROM a file,
(like cin reads from the keyboard/standard input),
you declare an INPUT file stream object to connect to
this file to do this input
The type of this object: ifstream
ifstream my_fin;
* STEP 3: connect your file stream object to a file
* fstream objects (whether they are ifstream objects or
ofstream objects) have a method open
to connect the file stream TO a desired file,
and to open that stream for the desired action
One version of the open method expects one argument,
a name of a desired file written as a string,
(a full path or its name from the current folder)
and it tries to connect the calling file stream to
that file
* for an ofstream, calling open creates a file with its
argument name if that file does not yet exist,
(and connects to the existing file and DELETES its
current contents if it does exist!!),
so the stream is ready to write its 1st character
my_fout.open("desired_output.txt");
* for an ifstream, calling open connects the ifstream
to that file so the stream is ready to read its
1st character:
my_fin.open("desired_input.txt");
* STEP 4: use your open-and-connected file stream object
* ONCE you have an open ofstream,
write to the file it is connected to
like you write screen output using cout!
my_fout << "Howdy" << endl;
* ONCE you have an open ifstream,
read from the file it is connected to
like you read interactive input using cin!
int quant;
my_fin >> quant;
* STEP 5: close your file stream object when you are
done
* WHEN you are done with a file stream object,
it is considered GOOD PRACTICE to close that file stream
with its close method (which expects no arguments)
my_fin.close();
my_fout.close();
(and you HAVE to do this if you wish to connect a
different file stream to the same file later in your
function, since a file can only have one file stream
object connected to it at a time)
* The fstream library provides more goodies, but this is
enough to get started with C++ file input and output!
* see a little example writing to and reading from a file
in 111lect14-2.cpp