← C++

C++ · 02 / 24

Variables, decisions, loops and functions

Calculate transport trips and divide the problem into reusable instructions.

  • Small
  • Basics
  • 45 min

Prerequisites: Install C++ and run your first program

In this tutorial
  1. Read the inputs and function
  2. Understand integer arithmetic
  3. Observe repetition
  4. Exercise and solution
  5. Project context and sources

Download learning package

Setup and verification

GCC with C++20 and CMake 3.25 or newer. Python 3 is also required for the common verification entry point. Extract the learning package into a new folder and run python3 run_checks.py there. The script copies the included files into a temporary learning directory. It verifies the example and then the supplied solution there, preserving the original files. Required packages are downloaded on the first run.

Verified on 2026-10-21 on Linux x86_64. The console example and exercise solution are executed. Expected output is shown in the article.

A warehouse needs to move seven parcels using a vehicle that carries three parcels per trip. Variables give these values names instead of repeating literals throughout the code. int stores whole numbers here. const states that a value remains unchanged after initialization.

A function is a named operation. Parameters provide input and return supplies a result. if selects a branch and a loop repeats work. The example models whole parcels only. Weight, volume and different goods are intentionally outside this small teaching model.

Package contents and individual files

01Read the inputs and function

parcels is the parcel count and capacity is the number carried on one trip. deliveriesNeeded receives copies of both numbers. Checking for zero before division prevents division by zero and maps empty or invalid input to zero trips. This is a simplified teaching policy, not the finished application’s validation.

main.cpp · cppOpen file
#include <iostream>
int deliveriesNeeded(int parcels, int capacity) {
  if (parcels <= 0 || capacity <= 0)
    return 0;
  return parcels / capacity + (parcels % capacity != 0 ? 1 : 0);
}
int main() {
  const int parcels = 7;
  const int capacity = 3;
  const int trips = deliveriesNeeded(parcels, capacity);
  for (int trip = 1; trip <= trips; ++trip)
    std::cout << "Trip " << trip << '\n';
  std::cout << "trips=" << trips << '\n';
}

Run / check

g++ -std=c++20 main.cpp -o lesson
./lesson

02Understand integer arithmetic

Seven divided by three produces two for int. The remainder operator % produces one, so one parcel is left. The conditional expression adds one extra trip. Six parcels leave no remainder. No floating-point calculation is needed.

03Observe repetition

The for loop starts at trip one, continues through trips inclusive, and increments trip after each output. Its condition is checked before every iteration. Compile and run as in lesson 1. Change the parcel count to six and then zero and compare the output.

Expected result

Trip 1 Trip 2 Trip 3 trips=3

Your exercise

Calculate trips for ten parcels with capacity four. Explain why the loop prints no trip lines for zero parcels.

Show the solution and explanation

Ten divided by four is two with remainder two, so a third trip is required. For zero parcels the function returns zero. The initial loop condition 1 <= 0 is false and the body is never entered. Also test twelve parcels with capacity four, which requires exactly three full trips.

solution/main.cpp · cppOpen file
#include <iostream>
int deliveriesNeeded(int parcels, int capacity) {
  if (parcels <= 0 || capacity <= 0)
    return 0;
  return parcels / capacity + (parcels % capacity != 0 ? 1 : 0);
}
int main() {
  if (deliveriesNeeded(10, 4) != 3 || deliveriesNeeded(0, 4) != 0 ||
      deliveriesNeeded(12, 4) != 3)
    return 1;
  const int parcels = 7;
  const int capacity = 3;
  const int trips = deliveriesNeeded(parcels, capacity);
  for (int trip = 1; trip <= trips; ++trip)
    std::cout << "Trip " << trip << '\n';
  std::cout << "trips=" << trips << '\n';
}

Project context and sources

The first six examples are standalone teaching programs. Lessons 7 through 9 use the complete FactoryForge source snapshot. Code, exercises and verified simplifications are explained separately. The project page provides a source ZIP while the GitHub repository is not yet published.