Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Constant | Variables
Introduction to C++

bookConstant

In the previous chapters, we printed some expressions. However, what to do if we need to store these expressions for future usage? In C++ (like in other programming languages) there are containers to keep data - variables. For each type of data (numbers, strings, characters) there is a type of variable.

Let’s take a look at the most often used types:

 const double x = 42;

The variable x will always be 42 and nobody can change it. The following code would cause an error:

 const int x = 10;
 x = 15;

You should declare variables as constants if you are sure that they will probably never change:

 const int monthPerYear = 12;
const char firstAlphabetLetter = 'A';
question-icon

Define the variable secondsPerMinute as const varible:

;

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 8

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 3.33

bookConstant

Sveip for å vise menyen

In the previous chapters, we printed some expressions. However, what to do if we need to store these expressions for future usage? In C++ (like in other programming languages) there are containers to keep data - variables. For each type of data (numbers, strings, characters) there is a type of variable.

Let’s take a look at the most often used types:

 const double x = 42;

The variable x will always be 42 and nobody can change it. The following code would cause an error:

 const int x = 10;
 x = 15;

You should declare variables as constants if you are sure that they will probably never change:

 const int monthPerYear = 12;
const char firstAlphabetLetter = 'A';
question-icon

Define the variable secondsPerMinute as const varible:

;

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 8
some-alt