• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 18 pages
Other

Cmpsc 201 Exam 2 Study Guide

Document preview thumbnail
Preview 3 out of 18 pages

CMPSC 201 EXAM 2 STUDY GUIDE

Content preview

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

Document information

Uploaded on
June 15, 2026
Number of pages
18
Written in
2025/2026
Type
Other
Person
Unknown
$15.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
GEEKA
3.8
(380)
Sold
2242
Followers
1451
Items
63861
Last sold
11 hours ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions