CodeTheRobot

C++ Beginner · Lesson 03 of 22

Basic syntax, variables and types; auto basics

Statements, int/double/bool/char, const, initialization and auto.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain basic syntax, variables and types; auto basics in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A type tells the compiler what a value means and how much storage it needs: int for whole numbers, double for fractions, bool for true/false, char for one character. Use brace initialization ({}) because it refuses silent narrowing, such as squeezing a double into an int.

auto lets the compiler deduce a variable's type from its initial value. Use it when the type is obvious or very long, but write the type yourself when it documents intent. Use const for any value that must not change.

Guided lesson

Work through these steps

  1. 1.

    Read the basic syntax, variables and types; auto basics example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

const int encoder_ticks{2048};
// A read-only whole number: encoder counts for one full turn (brace initialization).
const double radians{1.5708};
// A read-only fraction: a quarter turn in radians.
const auto ticks_per_radian = encoder_ticks / radians;
// auto deduces double, because int divided by double gives double.
bool moving{false};
// A true/false flag, starting as false.
char axis{'X'};
// A single character, written with single quotes.
// int bad{radians};
// Would not compile: braces refuse to squeeze a double into an int (this is the safety net).

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Values in memory

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

motor speed030x1070x1410x1890x1c40x2060x2420x2880x2c*p += 10VALUES IN MEMORY

Demo for Basic syntax, variables and types; auto basics. Expected result: 325.949

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <iostream>

int main() {
  const int encoder_ticks{2048};
  const double full_turn_rad{6.283185307179586};
  // TODO: declare ticks_per_rad with auto: encoder_ticks divided by full_turn_rad
  std::cout << ticks_per_rad << '\n';
  return 0;
}

Expected output

325.949
Stuck? Show the full solution (try it yourself first)
#include <iostream>

int main() {
  const int encoder_ticks{2048};
  const double full_turn_rad{6.283185307179586};
  const auto ticks_per_rad = encoder_ticks / full_turn_rad;
  std::cout << ticks_per_rad << '\n';
  return 0;
}

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Convert 2,048 encoder ticks per turn into ticks per degree using named const values. Print the result, then try int bad{1.5}; and read the error. Replace two obvious types with auto.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.