CSC 1060 Control Structures: Selection
5 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What should be the result of using an assignment operator instead of an equality operator in a conditional statement?

  • The program will not perform the intended comparison. (correct)
  • The program will execute the block of code correctly.
  • The program will evaluate to false if the variable equals zero.
  • The program will produce a syntax error.
  • What does the break keyword do within a switch statement?

  • It resets the switch statement for another evaluation.
  • It allows execution to continue to the next case statement.
  • It exits the current case and returns to the main program.
  • It ends the switch block and stops further code execution within it. (correct)
  • Which integral data types can be used with the switch statement for comparison?

  • Int and char (correct)
  • String and char
  • Long and boolean
  • Float and double
  • In the context of short circuit evaluation, what would happen if an OR expression has one true operand?

    <p>The second operand will not be evaluated.</p> Signup and view all the answers

    Which of the following statements about test expressions in selection statements is TRUE?

    <p>All test expressions must evaluate to true or false.</p> 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 or else if, NEVER else.
    • 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 the switch 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 the switch 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 class std::ostream.
    • std::ofstream (file output) is of class std::ofstream.

    Disconnected Model: File Output - 5 Steps

    1. Create output file stream object of type std::ofstream.
    2. Open the file for writing.
    3. Check if the file opened successfully.
    4. Write to the file.
    5. 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 is ios::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 and std::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.

    Quiz Team

    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.

    More Like This

    Use Quizgecko on...
    Browser
    Browser