Variable Initialization
In the previous exercise we learned to declare variables. Now let's look at how to assign values to them - how to initialize them.
We can perform initialization in two ways:
1. Declaration and initialization on two lines:
int number; // First we declare
number = 42; // Then we initialize
2. Declaration and initialization on one line:
int number = 42; // Declaration and initialization at once
Examples of initializing different types of variables:
int whole = 42; // Whole number
double decimal = 3.14; // Decimal number
char character = 'A'; // One character
bool truth = true; // Boolean value
If we only declare a variable without initialization, its value is not defined and may contain a random value. Therefore it's a good practice to always initialize variables when declaring them.
Instructions
Before the output, initialize the variable number
to the value 100
.
Start programming for free
3/6