CMPSC 201 EXAM 2 STUDY GUIDE
Relational operators - Answers - Are used to compare two values and return a true/false
value
==, <>, >=, <= , >, <
Logical Operators - Answers - -Are used to combine multiple condition and return a
single true/false value
&& (and), || (or), ! (not)
File I/O and Stream Variables - Answers - File I/O and Stream Variables
file
#include <fstream>
Declaring Variables in File I/O - Answers - Declare an object that will refer to the file.
datatype objectName;
ifstream inFile; // inFile is just an identifier
ofstream outFile; // outFile is just an identifier
Types of variable declarations in File I/O - Answers - Can declare multiple objects:
ifstream inFile, dataIn, fileIn;
ofstream outFile, dataOut, fileOut;
Stream Variables: initiation - Answers - -Must be initialized before it contains valid data
Initializing a stream means connecting it to a file
-The value of the stream variable can be thought of
as the file it is connected to
Nested 'if' Statements: Caution - Answers - -Proper use of braces
-C++ automatically matches an else with the closest if
Nested 'if' Statements - Answers - The "fix" is to use the braces or to use an empty
statement ( ; ) for the body of the else of the inside
Switches - Answers - switch (integer expression) {
case value1:
:
break;
case condition2:
:
break;
default:
,:
break;
Switches - Answers - -Expression switching should be a character, integer, or
enumerated value
-Remember to use break so that the execution won't fall through the entire switch
statement; (break allows the program execution to leave switch early)
Switch Example 1 - Answers - Write a switch statement that output East if dir = 'E'; West
if dir = 'W', South if dir = 'S', and North if dir ='N'
switch (dir) {
case 'E':
cout << "East";
break;
case 'W':
cout << "West";
break;
case 'S':
cout << "South";
break;
case 'N':
cout << "North";
break;
}
Switch Example 1 - Answers - If break is omitted after E, program will output East and
then West. Then it would follow to the break.
Switch: Default Case - Answers - The "default" case may be omitted. When present, it
contains the statements that are to be executed when no match is found.
Switches Continued - Answers - -The integer expression is evaluated and an attempt is
made to match it with constant expression.
-If a match is found, the statements associated with that specific constant expression
are executed.
Switches Continued - Answers - -The break statement ends the execution of statements
for each case.
-If the break statement is omitted for a case, execution will continue using the
statements for the next case until a break is encountered.
Switches: Note - Answers - -The default case does not have to be included. If the
default is not included and no cases match the expression, no statements will be
executed.
-Although the case comparison must be an integer or a character, ranges with equal
divisions can still use a switch, but manipulations are needed.
, Switches: Disadvantages - Answers - -Cannot easily do irregular ranges.
-Cannot match strings.
Switches: Mistakes - Answers - -Omitting the break statements once a match is found,
statements will be executed until break is encountered or last statement of switch is
executed.
-Not including all the possibilities; either the default statements will be executed or no
statements will be executed.
-Omitting the braces, { }
Enumeration Types - Answers - Sometimes, you want to limit a variable for a specific
purpose and should be able to have small number of values
-Values are defines as a list of constants of type 'int'
-Can be useful in conjunction with switch (since it is int)
Enumeration Types - Answers - enum Direction {NORTH, SOUTH, EAST, WEST};
You define an enumerated type name Direction, which only takes NORTH, SOUTH,
EAST, and WEST.
By default, the int value starts from 0 (NORTH = 0, SOUTH = 1...)
Enumeration Types - Answers - -enum DaysInMonth {JAN=31, FEB=28, MAR=30};
You can assign the value manually
-enum MixedConstant { FIVE=5, SIX, SEVEN};
SIX will take on the next int, which is 6; SEVEN takes the next int (7)
Using Enumerations to declare variables - Answers - enum Direction {NORTH, SOUTH,
EAST, WEST};
Direction windDirection = NORTH;
cout << windDirection << endl;
windDirection = EAST;
cout << windDirection << endl;
Using Enumerations to declare variables - Answers - The output will be
0
2
Enumeration Types: Do not use - Answers - -Variables of an enumerated type can only
take the constant, not the int value.
windDirection = WEST; // valid
windDirection = 3; // syntax error
Enumeration Types: Do not use - Answers - Output an enumerated variable will print
out the int value, not the constant name
cout << windDirection << endl; // output 3, not WEST
Relational operators - Answers - Are used to compare two values and return a true/false
value
==, <>, >=, <= , >, <
Logical Operators - Answers - -Are used to combine multiple condition and return a
single true/false value
&& (and), || (or), ! (not)
File I/O and Stream Variables - Answers - File I/O and Stream Variables
file
#include <fstream>
Declaring Variables in File I/O - Answers - Declare an object that will refer to the file.
datatype objectName;
ifstream inFile; // inFile is just an identifier
ofstream outFile; // outFile is just an identifier
Types of variable declarations in File I/O - Answers - Can declare multiple objects:
ifstream inFile, dataIn, fileIn;
ofstream outFile, dataOut, fileOut;
Stream Variables: initiation - Answers - -Must be initialized before it contains valid data
Initializing a stream means connecting it to a file
-The value of the stream variable can be thought of
as the file it is connected to
Nested 'if' Statements: Caution - Answers - -Proper use of braces
-C++ automatically matches an else with the closest if
Nested 'if' Statements - Answers - The "fix" is to use the braces or to use an empty
statement ( ; ) for the body of the else of the inside
Switches - Answers - switch (integer expression) {
case value1:
:
break;
case condition2:
:
break;
default:
,:
break;
Switches - Answers - -Expression switching should be a character, integer, or
enumerated value
-Remember to use break so that the execution won't fall through the entire switch
statement; (break allows the program execution to leave switch early)
Switch Example 1 - Answers - Write a switch statement that output East if dir = 'E'; West
if dir = 'W', South if dir = 'S', and North if dir ='N'
switch (dir) {
case 'E':
cout << "East";
break;
case 'W':
cout << "West";
break;
case 'S':
cout << "South";
break;
case 'N':
cout << "North";
break;
}
Switch Example 1 - Answers - If break is omitted after E, program will output East and
then West. Then it would follow to the break.
Switch: Default Case - Answers - The "default" case may be omitted. When present, it
contains the statements that are to be executed when no match is found.
Switches Continued - Answers - -The integer expression is evaluated and an attempt is
made to match it with constant expression.
-If a match is found, the statements associated with that specific constant expression
are executed.
Switches Continued - Answers - -The break statement ends the execution of statements
for each case.
-If the break statement is omitted for a case, execution will continue using the
statements for the next case until a break is encountered.
Switches: Note - Answers - -The default case does not have to be included. If the
default is not included and no cases match the expression, no statements will be
executed.
-Although the case comparison must be an integer or a character, ranges with equal
divisions can still use a switch, but manipulations are needed.
, Switches: Disadvantages - Answers - -Cannot easily do irregular ranges.
-Cannot match strings.
Switches: Mistakes - Answers - -Omitting the break statements once a match is found,
statements will be executed until break is encountered or last statement of switch is
executed.
-Not including all the possibilities; either the default statements will be executed or no
statements will be executed.
-Omitting the braces, { }
Enumeration Types - Answers - Sometimes, you want to limit a variable for a specific
purpose and should be able to have small number of values
-Values are defines as a list of constants of type 'int'
-Can be useful in conjunction with switch (since it is int)
Enumeration Types - Answers - enum Direction {NORTH, SOUTH, EAST, WEST};
You define an enumerated type name Direction, which only takes NORTH, SOUTH,
EAST, and WEST.
By default, the int value starts from 0 (NORTH = 0, SOUTH = 1...)
Enumeration Types - Answers - -enum DaysInMonth {JAN=31, FEB=28, MAR=30};
You can assign the value manually
-enum MixedConstant { FIVE=5, SIX, SEVEN};
SIX will take on the next int, which is 6; SEVEN takes the next int (7)
Using Enumerations to declare variables - Answers - enum Direction {NORTH, SOUTH,
EAST, WEST};
Direction windDirection = NORTH;
cout << windDirection << endl;
windDirection = EAST;
cout << windDirection << endl;
Using Enumerations to declare variables - Answers - The output will be
0
2
Enumeration Types: Do not use - Answers - -Variables of an enumerated type can only
take the constant, not the int value.
windDirection = WEST; // valid
windDirection = 3; // syntax error
Enumeration Types: Do not use - Answers - Output an enumerated variable will print
out the int value, not the constant name
cout << windDirection << endl; // output 3, not WEST