Podcast
Questions and Answers
What should be the result of using an assignment operator instead of an equality operator in a conditional statement?
What should be the result of using an assignment operator instead of an equality operator in a conditional statement?
What does the break keyword do within a switch statement?
What does the break keyword do within a switch statement?
Which integral data types can be used with the switch statement for comparison?
Which integral data types can be used with the switch statement for comparison?
In the context of short circuit evaluation, what would happen if an OR expression has one true operand?
In the context of short circuit evaluation, what would happen if an OR expression has one true operand?
Signup and view all the answers
Which of the following statements about test expressions in selection statements is TRUE?
Which of the following statements about test expressions in selection statements is TRUE?
Signup and view all the answers
Study Notes
CSC 1060 Control Structures: Selection
-
Objectives:
- Use logical expressions in a program.
- Explain program flow.
- Implement selection control using switch statements.
- Format output using stream manipulators.
- Write data from a file using stream objects.
Agenda: Week 06
- Selection Tips
- Short Circuit Evaluation
- switch Statements
- Testing
- Streams (
, ) - Disconnected Model: File out
- TODO
- Resources for help
Selection Tips
-
NOT
(end test expressions) with;
- Test expressions come after
if
orelse if
, NEVERelse
. - All test expressions must evaluate to true or false.
- C++ allows integer values to evaluate to true or false, but it's not recommended.
- Do NOT use assignment for equality comparison.
- Be sure to test for branch, bounds, and error testing.
Short Circuit Evaluation
-
AND (&&):
- False, false = false
- False, true = false
- True, false = false
- True, true = true
-
OR (||):
- False, false = false
- False, true = true
- True, false = true
- True, true = true
Switch Selection
- Use the
switch
statement to select one of many code blocks to be executed. - The value of the expression is compared with the values of each case (checking equality).
- If there's a match, the associated code block is executed and
break
out of theswitch
block. - Otherwise, the
default
block is executed. - Only use integral data types (
int
,char
). - Use equality comparison only.
- The
break
keyword stops execution of more code and case testing inside theswitch
block. - The
default
keyword specifies code to run if no case matches.
Testing
- Branch testing: Include test cases for each part of the code, including conditional statements.
- Bounds testing: Include test cases for values on boundaries of logical expressions.
- Error testing: Include test cases for invalid data that users might enter.
Streams
- A stream object represents data flowing into or out of a program.
- Output (Insertion):
<<
- Console window output uses
std::cout
. - File output uses
std::ofstream
. -
std::cout
is of classstd::ostream
. -
std::ofstream
(file output) is of classstd::ofstream
.
Disconnected Model: File Output - 5 Steps
- Create output file stream object of type
std::ofstream
. - Open the file for writing.
- Check if the file opened successfully.
- Write to the file.
- Close the file stream to disconnect from writing.
- Only flat files (.txt, .dat, .csv) are allowed for CSC 1060.
- Do not include drives, directories, or folder paths in file names.
Creating the File Object: Step 1
- Create a
std::ofstream
object (e.g.,std::ofstream writeFile;
). - Include the
<fstream>
library.
Opening the File: Step 2
-
std::ofstream
default mode isios::out
. - Can also append to an existing file using
ios::app
.
Checking File Status: Step 3
- Check if the file opened successfully using methods like
!writeFile
,!writeFile.good()
,writeFile.fail()
.
Writing to a File: Step 4
- Format data as needed using manipulators like
std::fixed
andstd::setprecision
. - Delimit data for the file (e.g., newlines).
Closing the File: Step 5
- Use
writeFile.close()
to disconnect from the file.
Pre-work and upcoming week
- Earn pre-work grade by posting weekly discussion questions to D2L.
- Complete week 06 content module on D2L.
- Upcoming week 07 content.
Questions | Clarifications | Help
- Student office hours (by appointment, drop-in).
- Email: [email protected]
- On-campus tutoring information.
- 24/7 online tutoring (D2L).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore the key concepts of selection control structures in C++. This quiz will test your understanding of logical expressions, switch statements, and stream manipulations, all essential for program flow management. Enhance your coding skills and apply best practices in selection structures.