0 day streak
Code library

Beginner C++ exercises

Read these Beginner C++ snippets line by line — each one comes with a written breakdown of what the code does and why.

C++ · Beginner

Iterating with For Loops

A for loop repeats a block of code a specific number of times. It is ideal when you know exactly how many iterations are needed.

Control FlowSyntax
Open snippet →
C++ · Beginner

The Standard Namespace (std::)

C++ uses namespaces to organize code and avoid naming conflicts. Most standard features are prefixed with 'std::' to indicate they belong to the Standard Library.

SyntaxBest Practices
Open snippet →
C++ · Beginner

Using Code Comments

Comments are notes for developers that are completely ignored by the compiler. They are essential for documenting what the code does.

SyntaxBest Practices
Open snippet →
C++ · Beginner

Iterating with While Loops

A while loop continues to execute as long as its condition is true. It is useful when the number of repetitions depends on a dynamic state.

Control FlowSyntax
Open snippet →
C++ · Beginner

Understanding Variables and Data Types

This program demonstrates five fundamental C++ data types. Variables are containers that store values. int stores whole numbers, double stores decimal numbers, char stores single characters, bool s…

Data TypesSyntax
Open snippet →
C++ · Beginner

Making Decisions with If-Else Statements

If-else statements allow your program to make decisions based on conditions. The code checks the score variable against multiple thresholds. If the first condition is false, it moves to the next el…

Control FlowSyntax
Open snippet →
C++ · Beginner

Creating Reusable Code with Functions

Functions are reusable blocks of code that perform specific tasks. The add function takes two integers, adds them, and returns the result. The printMessage function takes a string and displays it b…

FunctionsSyntax
Open snippet →
C++ · Beginner

Working with Arrays

Arrays store multiple values of the same type in contiguous memory. This example creates an integer array with 5 elements. Arrays are indexed starting from 0, so numbers[0] is the first element. Th…

Arrays & ListsData Types
Open snippet →
C++ · Beginner

Understanding Pointers and Memory Addresses

Pointers store memory addresses rather than values. The asterisk (*) declares a pointer type. The ampersand (&) gets the memory address of a variable. Dereferencing (*ptr) accesses the value stored…

Memory ManagementData TypesSyntax
Open snippet →
C++ · Beginner

Repeating Actions with Loops

Loops let you repeat code multiple times without writing it again. The for-loop has three parts: start (i = 1), condition (i <= 5), and update (i++). It runs as long as the condition is true.

Control FlowSyntax
Open snippet →
C++ · Beginner

Enumerations for Named Constants

An enum creates a custom type with named constant values. By default, Monday is 0, Tuesday is 1, and so on. This makes code more readable than using magic numbers.

Data TypesSyntax
Open snippet →
C++ · Beginner

Menu Selection with Switch

The switch statement checks a variable against multiple possible values. Each case runs code if it matches. The break prevents falling through to the next case. Default handles any unmatched value.

Control Flow
Open snippet →
C++ · Beginner

User Input with cin

cin (console input) reads user input from the keyboard. The >> operator extracts values and stores them in variables. It automatically handles type conversion for numbers.

Syntax
Open snippet →
C++ · Beginner

Grouping Data with Structs

Structs allow you to group related variables of different types under a single name. They are the foundation for creating custom data types. Each variable within a struct is called a 'member'. You…

OOPData Types
Open snippet →
C++ · Beginner

Boolean Logic with Logical Operators

Logical operators let you combine multiple boolean conditions. && (AND) returns true only if ALL conditions are true. || (OR) returns true if ANY condition is true. ! (NOT) inverts a boolean value.…

Control Flow
Open snippet →
C++ · Beginner

Constants with const and constexpr

Constants are values that cannot change during program execution. Use const for values computed at runtime that won't change. Use constexpr for values that can be computed at compile time - these a…

Best Practices
Open snippet →
C++ · Beginner

References as Aliases

A reference is an alias (alternative name) for an existing variable. Unlike pointers, references cannot be null and must be initialized when created. Use the & symbol in both declaration and functi…

Memory ManagementSyntax
Open snippet →
C++ · Beginner

Introduction to Namespaces

A namespace is a named scope that groups related code elements together, preventing naming conflicts. In larger projects, two different parts might define a function with the same name; namespaces…

SyntaxBest Practices
Open snippet →
C++ · Beginner

Defining Your First Class

A class is a blueprint for creating objects. It bundles data (variables) and functions (methods) together. The public keyword makes members accessible from outside the class. Objects are instances…

OOP
Open snippet →
C++ · Beginner

Constructors and Initialization

A constructor is a special method called automatically when an object is created. It initializes the object's data. Private members can only be accessed by the class itself, protecting internal dat…

OOPSyntax
Open snippet →
C++ · Beginner

Basic File Input and Output

C++ provides fstream for file operations. ofstream creates and writes to files, while ifstream reads from files. Always check if a file opened successfully using is_open(), and close files when don…

SyntaxBest Practices
Open snippet →
C++ · Beginner

The Scope Resolution Operator

The :: operator accesses items from specific scopes. Use namespace::item to reach a namespaced variable, or ::variable to access the global scope. This allows precise control over which variable yo…

Syntax
Open snippet →
C++ · Beginner

Pointers and Memory Addresses

Pointers are variables that store memory addresses instead of actual values. The asterisk (*) declares a pointer type, the ampersand (&) gets the address of a variable, and dereferencing with * acc…

Memory ManagementSyntax
Open snippet →