C++ Basics
Variables, numbers, text, functions, decisions, vectors, loops and structs: the C++ every robot lesson builds on. Each step is a short program that's compiled and checked the moment you run it, with examples taken from the robot arm you'll program next.
Start the lesson0/8 steps done
What you'll learn
- Write, compile and run a C++ program with main and std::cout
- Store values in typed variables, and avoid integer division
- Build readable messages with std::format
- Write functions with typed parameters and return values
- Make decisions with if, else if and else
- Keep values in a std::vector and loop over it
- Group values into structs and pass them by const reference
Before you start
No programming experience needed: this lesson starts from your very first line of C++. Everything runs in your browser, with nothing to install. The first visit downloads a C++ compiler and the simulator, about 55 MB, and later visits load from your browser's cache.
Steps
- 1Hello, robotCompile and run your first C++ program, print to the console and store values in variables.mainstd::coutTypes and variablesCommentsOpen
- 2Numbers and mathsDo arithmetic with ints and doubles, avoid integer division and convert degrees to radians.int and doubleInteger division<cmath>constOpen
- 3Working with textJoin strings, ask a string its length and build readable messages with std::format.std::stringMember functionsstd::formatNumber formattingOpen
- 4FunctionsPackage a calculation as a function with typed parameters, and return its result.Return typesParametersreturnvoidOpen
- 5Making decisionsCompare values, and choose what to do with if, else if and else.boolComparisonsif / else if / else&& and ||Open
- 6VectorsKeep many values in one std::vector, read items by index and add new ones.std::vectorIndexingpush_backstd::ranges::countOpen
- 7LoopsRepeat work for every item in a vector: add up totals and find the closest ball.Range forCounting forRunning totalsBest so farOpen
- 8Structs and referencesGroup values into your own types with struct, and pass them to functions without copying.structMembersReferencesconst &Open