WEEK 9 · LAB 3
Lab 3 — Branching Statements and Logical Operators
Use an if else statement to decide whether a score is a pass.
I. Objectives
- Use an if else statement to choose between two outputs.
- Write a test condition and check the boundary value.
Before the Lab
Week 8: bool values, relational and logical operators, and if else. Read score >= 60 as a condition that is true for a passing score.
II. Environment
- A computer with Windows, macOS, or Linux.
- Use OnlineGDB in C++ mode, or the C++ environment demonstrated in class.
- Save your program as lab03.cpp. Use one source file for this lab.
Open OnlineGDB ↗III. Tasks and Requirements
Complete the TODOs in the starter code, then compare your result with the expected output. The starter is intentionally unfinished.
Task 1 — Read the two branches
- Find the output statements for Pass and Fail. Run the starter with 50 and then 80. Both runs display Fail because false is a placeholder condition.
Task 2 — Complete the if condition
- At TODO 1, replace false with a condition that is true when score is at least 60. Keep the supplied if else structure.
Task 3 — Test both branches and the boundary
- Use an integer score from 0 to 100. Test 0, 59, 60, and 100. The first two should display Fail; the last two should display Pass.
#include <iostream>
int main() {
int score = 0;
std::cin >> score;
// TODO 1: Replace false with the test condition.
if (false) {
std::cout << "Pass" << '\n';
} else {
std::cout << "Fail" << '\n';
}
return 0;
}
Expected Output
Expected output for input 60 after completing the condition.
Pass
IV. Lab Report Submission
Submit one lab report for this lab, in English or Chinese. Include the following content in the report.
- Student information: name, student ID, class, lab number, and lab title.
- Objectives and environment: briefly describe the lab objectives and the C++ environment you used.
- Procedure and code: explain how you completed the tasks and include the complete code from lab03.cpp in the report.
- Tests and results: record the test inputs and results, include one or two screenshots, and explain whether the results match the expected output.
- Answers and reflection: answer the two questions below and briefly describe what you learned or a problem you solved.
Questions for the Report
- Why should a score of 60 use the Pass branch?
- When does the else branch run?