Lesson 4 of 20Getting Started18 min

Operators and Expressions

Operators combine values to create expressions. You will use them for calculations, comparisons, assignments, and compound conditions.

Arithmetic Operators

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remaindera % b
Arithmetic example
#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 3;

    cout << "a + b = " << a + b << endl;
    cout << "a - b = " << a - b << endl;
    cout << "a * b = " << a * b << endl;
    cout << "a / b = " << a / b << endl;
    cout << "a % b = " << a % b << endl;

    return 0;
}

Comparison and Logical Operators

GroupOperatorsPurpose
Comparison== != < > <= >=Compare two values
Logical&& || !Combine or reverse conditions
Assignment= += -= *= /=Assign or update values
Important

Integer division removes the decimal part. For example, 10 / 3 produces 3. Use a double value when you need a decimal result.

Practice task

Build a rectangle calculator that accepts width and height, then displays area and perimeter.

Lesson Summary

Perform calculations, comparisons, assignments, and logical tests.