C++ Programming

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

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

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.

Starter Code

Download .cpp
#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

Two Short Questions

  1. Why should a score of 60 use the Pass branch?
  2. When does the else branch run?