CSC 1060 Week 06 More Selection and File Output PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
Lecture notes on control structures in C++ programming. Topics include selection, switch statements, testing, streams for file input/output, and code examples.
Full Transcript
CSC 1060 CONTROL STRUCTURES: SELECTION OBJECTIVES AGENDA: WEEK 06 Use logical expressions in a 1. Selection Tips program. 2. Short Circuit Evaluation Explain program flow. 3. switch St...
CSC 1060 CONTROL STRUCTURES: SELECTION OBJECTIVES AGENDA: WEEK 06 Use logical expressions in a 1. Selection Tips program. 2. Short Circuit Evaluation Explain program flow. 3. switch Statements Implement selection control 4. Testing using switch statements. 5. Streams Format output using stream manipulators. 6. Disconnected Model: File out 7. TODO To write data from a file using stream objects. 8. Resources for help SELECTION TIPS Do NOT end (test expressions) with ; if (test expression) ; Test (expressions) come after if, or if( test expression ) else if(test expression) else if, NEVER else else (test expression) All test (expressions) must evaluate to true or false C++ does allow int values to evaluate to Non-zero values are true (5, -5) true and false, but it isn't recommended! 0 is false Do NOT use = assignment for == if( num = 0 ) NO comparison evaluation – 0 is false equality comparison if( num == 0) comparison evaluated Make sure to test for branch, bounds, and error testing SHORT CIRCUIT EVALUATION AND (&&) False OR (||) True False True True True False False False 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 is a match, the associated block of code is executed and break out of the switch block. else, the default block is executed. Compare only using integral data types: int, char Compare each case using equality comparison only! SWITCH SELECTION: HTTPS://WWW.W3SCHOOLS.COM/CPP/CPP_SWITCH.ASP The break keyword When C++ reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing. The default keyword The default keyword specifies some code to run if there is no case match Complete exercises 1 and 2 from w3schools. TESTING Now that your programs can make decisions and execute different code, you must put more effort into thoroughly testing your code. Branch testing your test plan should include all the test cases needed to ensure that each part of your code is tested, including the statements within each if or if-then-else statement. Bounds testing your test plan should include all the test cases needed to check the values that are on the boundaries in the logical expressions in each conditional statement found in your program. Error testing your test plan should include test cases for invalid data that your users might enter. You program should handle this error data - stating that the program crashes is not a correct test plan! STREAMS: HOW DATA GETS MOVED A stream object represents data flowing either into your program or out from your program. Output (Insertion)