</>
CodeLearn Pro
Start Learning Free โ†’
โš™๏ธ SystemsAdvanced10 ExamplesWith Output

Learn C++ โ€” 10 Examples
with Console Output

Each example below teaches one C++ concept. Read the code, press Run โ€” See Output, and the console output appears instantly. Copy any example with the copy button.

๐Ÿ“–Read the code
โ–ถ๏ธPress Run to see output
๐Ÿ“‹Copy to use it
1

Hello, World!

Every programmer starts here. This program prints a message to the screen. It uses cout โ€” which stands for "console output" โ€” to display text.

๐Ÿ“š Teaches: Basic program structure, #include, main function, cout

example1.cppC++
1#include
2using namespace std;
3 
4int main() {
5 cout << "Hello, World!" << endl;
6 return 0;
7}
2

Variables and Data Types

Variables are containers that store values. C++ requires you to declare the type of each variable before using it โ€” int for whole numbers, double for decimals, string for text.

๐Ÿ“š Teaches: int, double, string, variable declaration, cout

example2.cppC++
1#include
2#include
3using namespace std;
4 
5int main() {
6 int age = 20;
7 double price = 9.99;
8 string name = "Alice";
9 
10 cout << "Name: " << name << endl;
11 cout << "Age: " << age << endl;
12 cout << "Price: $" << price << endl;
13 
14 return 0;
15}
3

Getting User Input

cin reads input from the user (keyboard). The >> operator sends the input into your variable. This is the opposite of cout <<.

๐Ÿ“š Teaches: cin, >> operator, reading input, storing in variables

example3.cppC++
1#include
2#include
3using namespace std;
4 
5int main() {
6 string name;
7 int age;
8 
9 cout << "Enter your name: ";
10 cin >> name;
11 
12 cout << "Enter your age: ";
13 cin >> age;
14 
15 cout << "Hello, " << name << "!" << endl;
16 cout << "You are " << age << " years old." << endl;
17 
18 return 0;
19}
4

If / Else Conditions

Conditions let your program make decisions. If a condition is true, one block of code runs. If it is false, another block runs instead.

๐Ÿ“š Teaches: if, else if, else, comparison operators, conditions

example4.cppC++
1#include
2using namespace std;
3 
4int main() {
5 int score = 75;
6 
7 if (score >= 90) {
8 cout << "Grade: A" << endl;
9 } else if (score >= 75) {
10 cout << "Grade: B" << endl;
11 } else if (score >= 60) {
12 cout << "Grade: C" << endl;
13 } else {
14 cout << "Grade: F" << endl;
15 }
16 
17 return 0;
18}
5

For Loop

A for loop repeats a block of code a set number of times. You set a starting value, a condition to keep looping, and how to update the counter each time.

๐Ÿ“š Teaches: for loop, counter variable, loop body, iteration

example5.cppC++
1#include
2using namespace std;
3 
4int main() {
5 // Print numbers 1 to 5
6 for (int i = 1; i <= 5; i++) {
7 cout << "Count: " << i << endl;
8 }
9 
10 cout << "Done!" << endl;
11 return 0;
12}
6

While Loop

A while loop keeps running as long as a condition is true. It is useful when you do not know in advance how many times to repeat.

๐Ÿ“š Teaches: while loop, condition, infinite loop prevention

example6.cppC++
1#include
2using namespace std;
3 
4int main() {
5 int number = 1;
6 
7 while (number <= 5) {
8 cout << number << " x 2 = " << number * 2 << endl;
9 number++; // increase by 1 each time
10 }
11 
12 return 0;
13}
7

Functions

A function is a reusable block of code that does one specific job. You define it once and call it as many times as you need. Functions make your code organised and avoid repetition.

๐Ÿ“š Teaches: function definition, parameters, return type, calling functions

example7.cppC++
1#include
2using namespace std;
3 
4// Function that adds two numbers
5int add(int a, int b) {
6 return a + b;
7}
8 
9// Function that greets a person
10void greet(string name) {
11 cout << "Hello, " << name << "!" << endl;
12}
13 
14int main() {
15 int result = add(10, 5);
16 cout << "10 + 5 = " << result << endl;
17 
18 greet("Bob");
19 greet("Alice");
20 
21 return 0;
22}
8

Arrays

An array stores multiple values of the same type in one variable. You access each value using its index number, starting from 0.

๐Ÿ“š Teaches: array declaration, indexing from 0, array size, looping through arrays

example8.cppC++
1#include
2using namespace std;
3 
4int main() {
5 // Array of 5 scores
6 int scores[5] = {88, 92, 75, 96, 81};
7 
8 cout << "All scores:" << endl;
9 for (int i = 0; i < 5; i++) {
10 cout << "Score " << i + 1 << ": " << scores[i] << endl;
11 }
12 
13 return 0;
14}
9

Classes and Objects

A class is like a blueprint. An object is something built from that blueprint. Classes bundle together related data (attributes) and actions (methods) into one package โ€” this is the core idea of Object-Oriented Programming.

๐Ÿ“š Teaches: class, object, attributes, methods, constructor, OOP basics

example9.cppC++
1#include
2#include
3using namespace std;
4 
5class Dog {
6public:
7 string name;
8 string breed;
9 int age;
10 
11 // Method โ€” an action the Dog can do
12 void bark() {
13 cout << name << " says: Woof!" << endl;
14 }
15 
16 void info() {
17 cout << name << " is a " << breed;
18 cout << " aged " << age << endl;
19 }
20};
21 
22int main() {
23 Dog myDog; // create an object
24 myDog.name = "Rex";
25 myDog.breed = "Labrador";
26 myDog.age = 3;
27 
28 myDog.info();
29 myDog.bark();
30 
31 return 0;
32}
10

Simple Calculator

Putting it all together โ€” this program uses variables, conditions and functions to build a working calculator. It shows how the concepts from all previous examples combine into a real program.

๐Ÿ“š Teaches: switch statement, combining concepts, real program structure

example10.cppC++
1#include
2using namespace std;
3 
4double calculate(double a, char op, double b) {
5 if (op == '+') return a + b;
6 if (op == '-') return a - b;
7 if (op == '*') return a * b;
8 if (op == '/') return a / b;
9 return 0;
10}
11 
12int main() {
13 double num1 = 20;
14 double num2 = 4;
15 
16 cout << num1 << " + " << num2 << " = ";
17 cout << calculate(num1, '+', num2) << endl;
18 
19 cout << num1 << " - " << num2 << " = ";
20 cout << calculate(num1, '-', num2) << endl;
21 
22 cout << num1 << " * " << num2 << " = ";
23 cout << calculate(num1, '*', num2) << endl;
24 
25 cout << num1 << " / " << num2 << " = ";
26 cout << calculate(num1, '/', num2) << endl;
27 
28 return 0;
29}

Quick Reference

C++ Cheat Sheet

The most important C++ syntax at a glance. Bookmark this section.

SyntaxWhat it does
#include <iostream>Include the input/output library
using namespace std;Use the standard namespace (saves typing std::)
int main() { }Main function โ€” every program starts here
cout << "text"Print text to the console
cin >> variableRead input from the keyboard
endlEnd the line (like pressing Enter)
int x = 5;Declare an integer variable
double x = 3.14;Declare a decimal number variable
string s = "hello";Declare a text variable
if (x > 0) { }Run code only if condition is true
for (int i=0; i<n; i++)Loop n times
while (condition) { }Loop while condition is true
int fn(int a) { }Define a function that returns int
return 0;Exit main โ€” 0 means success
class Name { };Define a class (object blueprint)
// commentSingle-line comment (ignored by compiler)