WEEK 6 · LAB 2
Lab 2 — 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 5: 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 lab02.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. Submission
- Your completed lab02.cpp source file.
- One or two screenshots showing your test results.
- A short answer to each question below, in English or Chinese.
Two Short Questions
- Why should a score of 60 use the Pass branch?
- When does the else branch run?